PowerShell - leap, raindrops, reverse-string, two-fer

This commit is contained in:
Xevion
2021-11-26 02:28:45 -06:00
parent e02f2ceabf
commit 3bd08f4c92
29 changed files with 569 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
{
"blurb": "Given a year, report if it is a leap year.",
"authors": [
"spuder"
],
"contributors": [
"gyssels",
"kchenery"
],
"files": {
"solution": [
"LeapYear.ps1"
],
"test": [
"LeapYear.tests.ps1"
],
"example": [
".meta/LeapYear.example.ps1"
]
},
"source": "JavaRanch Cattle Drive, exercise 3",
"source_url": "http://www.javaranch.com/leap.jsp"
}

View File

@@ -0,0 +1 @@
{"track":"powershell","exercise":"leap","id":"0a3fd49cec7f4abab206a09b5accdf20","url":"https://exercism.org/tracks/powershell/exercises/leap","handle":"Xevion","is_requester":true,"auto_approve":false}

1
powershell/leap/.version Normal file
View File

@@ -0,0 +1 @@
1

33
powershell/leap/HELP.md Normal file
View File

@@ -0,0 +1,33 @@
# Help
## Running the tests
To run the tests run the command `Invoke-Pester` from within the exercise directory.
## Submitting your solution
You can submit your solution using the `exercism submit LeapYear.ps1` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [PowerShell track's documentation](https://exercism.org/docs/tracks/powershell)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you are having trouble, you can use one of the following resources:
- [Powershell Documentation][powershell docs]
[Add more resources]: TODO
[powershell docs]: https://docs.microsoft.com/en-us/powershell/

View File

@@ -0,0 +1,9 @@
function Test-LeapYear {
param( [int]$year )
if ((($year % 4 -eq 0) -and !($year % 100 -eq 0)) -or ($year % 400 -eq 0)) {
return $true
}
return $false
}

View File

@@ -0,0 +1,21 @@
BeforeAll {
. ".\LeapYear.ps1"
}
Describe "LeapYear Tests" {
It "Year not divisible by 4: common year" {
Test-LeapYear(2015) | Should -Be $false
}
It "Year divisible by 4, not divisible by 100: leap year" {
Test-LeapYear(1996) | Should -Be $true
}
It "Year divisible by 100, not divisible by 400: common year" {
Test-LeapYear(2100) | Should -Be $false
}
It "Year divisible by 400: leap year" {
Test-LeapYear(2000) | Should -Be $true
}
}

44
powershell/leap/README.md Normal file
View File

@@ -0,0 +1,44 @@
# Leap
Welcome to Leap on Exercism's PowerShell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given a year, report if it is a leap year.
The tricky thing here is that a leap year in the Gregorian calendar occurs:
```text
on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
```
For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap
year, but 2000 is.
## Notes
Though our exercise adopts some very simple rules, there is more to
learn!
For a delightful, four minute explanation of the whole leap year
phenomenon, go watch [this youtube video][video].
[video]: http://www.youtube.com/watch?v=xX96xng7sAE
## Source
### Created by
- @spuder
### Contributed to by
- @gyssels
- @kchenery
### Based on
JavaRanch Cattle Drive, exercise 3 - http://www.javaranch.com/leap.jsp

View File

@@ -0,0 +1,22 @@
{
"blurb": "Convert a number to a string, the content of which depends on the number's factors.",
"authors": [
"gyssels"
],
"contributors": [
"kchenery"
],
"files": {
"solution": [
"Raindrops.ps1"
],
"test": [
"Raindrops.tests.ps1"
],
"example": [
".meta/Raindrops.example.ps1"
]
},
"source": "A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division.",
"source_url": "https://en.wikipedia.org/wiki/Fizz_buzz"
}

View File

@@ -0,0 +1 @@
{"track":"powershell","exercise":"raindrops","id":"5b35a1ecc3b94181bc46ed1119958c44","url":"https://exercism.org/tracks/powershell/exercises/raindrops","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1 @@
0

1
powershell/raindrops/0 Normal file
View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,33 @@
# Help
## Running the tests
To run the tests run the command `Invoke-Pester` from within the exercise directory.
## Submitting your solution
You can submit your solution using the `exercism submit Raindrops.ps1` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [PowerShell track's documentation](https://exercism.org/docs/tracks/powershell)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you are having trouble, you can use one of the following resources:
- [Powershell Documentation][powershell docs]
[Add more resources]: TODO
[powershell docs]: https://docs.microsoft.com/en-us/powershell/

View File

@@ -0,0 +1,35 @@
# Raindrops
Welcome to Raindrops on Exercism's PowerShell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation).
The rules of `raindrops` are that if a given number:
- has 3 as a factor, add 'Pling' to the result.
- has 5 as a factor, add 'Plang' to the result.
- has 7 as a factor, add 'Plong' to the result.
- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number.
## Examples
- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
- 34 is not factored by 3, 5, or 7, so the result would be "34".
## Source
### Created by
- @gyssels
### Contributed to by
- @kchenery
### Based on
A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz

View File

@@ -0,0 +1,40 @@
Function Get-Raindrops() {
<#
.SYNOPSIS
Given a number convert it to Pling, Plang, Plong if it has factors of 3, 5 or 7.
.DESCRIPTION
Convert a number to a string, the contents of which depend on the number's factors.
- If the number has 3 as a factor, output 'Pling'.
- If the number has 5 as a factor, output 'Plang'.
- If the number has 7 as a factor, output 'Plong'.
- If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.
.PARAMETER Rain
The number to evaluate
.EXAMPLE
Get-Raindrops -Rain 35
This will return PlangPlong as it has factors of 5 and 7
.EXAMPLE
Get-Raindrops -Rain 12121
This will return 12121 as it does not contain factors of 3, 5 or 7 so the value is passed through.
#>
[CmdletBinding()]
Param(
[int]$Rain
)
[String] $result = ""
if ($Rain % 3 -eq 0) { $result += "Pling" }
if ($Rain % 5 -eq 0) { $result += "Plang" }
if ($Rain % 7 -eq 0) { $result += "Plong" }
if ($result.Length -gt 0) { return $result }
return $Rain.ToString()
}

View File

@@ -0,0 +1,34 @@
BeforeAll {
. ".\Raindrops.ps1"
}
Describe "Test Get-Raindrops" {
It "Given the number <Number> it should output <Result>" -TestCases @(
@{ Number = 1; Result = "1" },
@{ Number = 3; Result = "Pling" },
@{ Number = 5; Result = "Plang" },
@{ Number = 7; Result = "Plong" },
@{ Number = 6; Result = "Pling" },
@{ Number = 9; Result = "Pling" },
@{ Number = 10; Result = "Plang" },
@{ Number = 14; Result = "Plong" },
@{ Number = 15; Result = "PlingPlang" },
@{ Number = 21; Result = "PlingPlong" },
@{ Number = 25; Result = "Plang" },
@{ Number = 27; Result = "Pling" },
@{ Number = 35; Result = "PlangPlong" },
@{ Number = 49; Result = "Plong" },
@{ Number = 52; Result = "52" },
@{ Number = 105; Result = "PlingPlangPlong" },
@{ Number = 3125; Result = "Plang" },
@{ Number = 12121; Result = "12121" }
) {
Param(
[int]$Number,
[string]$Result
)
Get-Raindrops -Rain $Number | Should -BeExactly $Result
}
}

View File

@@ -0,0 +1,22 @@
{
"blurb": "Reverse a string",
"authors": [
"gyssels"
],
"contributors": [
"kchenery"
],
"files": {
"solution": [
"ReverseString.ps1"
],
"test": [
"ReverseString.tests.ps1"
],
"example": [
".meta/ReverseString.example.ps1"
]
},
"source": "Introductory challenge to reverse an input string",
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
}

View File

@@ -0,0 +1 @@
{"track":"powershell","exercise":"reverse-string","id":"675925dac0bc4ca59b00bd1f0515381e","url":"https://exercism.org/tracks/powershell/exercises/reverse-string","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,33 @@
# Help
## Running the tests
To run the tests run the command `Invoke-Pester` from within the exercise directory.
## Submitting your solution
You can submit your solution using the `exercism submit ReverseString.ps1` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [PowerShell track's documentation](https://exercism.org/docs/tracks/powershell)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you are having trouble, you can use one of the following resources:
- [Powershell Documentation][powershell docs]
[Add more resources]: TODO
[powershell docs]: https://docs.microsoft.com/en-us/powershell/

View File

@@ -0,0 +1,26 @@
# Reverse String
Welcome to Reverse String on Exercism's PowerShell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Reverse a string
For example:
input: "cool"
output: "looc"
## Source
### Created by
- @gyssels
### Contributed to by
- @kchenery
### Based on
Introductory challenge to reverse an input string - https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

View File

@@ -0,0 +1,11 @@
Function Get-ReverseString {
[CmdletBinding()]
Param(
[Parameter(Position=1, ValueFromPipeline=$true)]
[string]$Forward
)
$x = $Forward.ToCharArray()
[array]::Reverse($x)
return -join $x
}

View File

@@ -0,0 +1,21 @@
BeforeAll {
. ".\ReverseString.ps1"
}
Describe "Get-ReverseString Tests" {
It "Given <Forward> it outputs <Reverse>" -TestCases @(
@{ Forward = ""; Reverse = "" },
@{ Forward = "PowerShell"; Reverse = "llehSrewoP" },
@{ Forward = "robot"; Reverse = "tobor" },
@{ Forward = "Ramen"; Reverse = "nemaR" },
@{ Forward = "I'm hungry!"; Reverse = "!yrgnuh m'I" },
@{ Forward = "racecar"; Reverse = "racecar" }
) {
Param(
[string]$Forward,
[string]$Reverse
)
Get-ReverseString -Forward $Forward | Should -BeExactly $Reverse
}
}

View File

@@ -0,0 +1,23 @@
{
"blurb": "Create a sentence of the form \"One for X, one for me.\"",
"authors": [
"gyssels"
],
"contributors": [
"cmccandless",
"kchenery",
"sjwarner"
],
"files": {
"solution": [
"TwoFer.ps1"
],
"test": [
"TwoFer.tests.ps1"
],
"example": [
".meta/TwoFer.example.ps1"
]
},
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}

View File

@@ -0,0 +1 @@
{"track":"powershell","exercise":"two-fer","id":"ee6acfe67bf6423e8ceab007e73d57ac","url":"https://exercism.org/tracks/powershell/exercises/two-fer","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,33 @@
# Help
## Running the tests
To run the tests run the command `Invoke-Pester` from within the exercise directory.
## Submitting your solution
You can submit your solution using the `exercism submit TwoFer.ps1` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [PowerShell track's documentation](https://exercism.org/docs/tracks/powershell)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you are having trouble, you can use one of the following resources:
- [Powershell Documentation][powershell docs]
[Add more resources]: TODO
[powershell docs]: https://docs.microsoft.com/en-us/powershell/

View File

@@ -0,0 +1,47 @@
# Two Fer
Welcome to Two Fer on Exercism's PowerShell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
`Two-fer` or `2-fer` is short for two for one. One for you and one for me.
Given a name, return a string with the message:
```text
One for name, one for me.
```
Where "name" is the given name.
However, if the name is missing, return the string:
```text
One for you, one for me.
```
Here are some examples:
|Name |String to return
|:-------|:------------------
|Alice |One for Alice, one for me.
|Bob |One for Bob, one for me.
| |One for you, one for me.
|Zaphod |One for Zaphod, one for me.
## Source
### Created by
- @gyssels
### Contributed to by
- @cmccandless
- @kchenery
- @sjwarner
### Based on
https://github.com/exercism/problem-specifications/issues/757

View File

@@ -0,0 +1,32 @@
Function Get-TwoFer(){
<#
.SYNOPSIS
"Two-fer" is short for two for one. One for you and one for me.
.DESCRIPTION
If the given name is "Alice", the result should be "One for Alice, one for me."
If no name is given, the result should be "One for you, one for me."
.PARAMETER Name
The name to use.
.EXAMPLE
Get-TwoFer
Will return: One for you, one for me
.EXAMPLE
Get-TwoFer -Name Alice
Will return: One for Alice, one for me
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false, Position=0)]
[string]$Name
)
# if ($Name -eq $null -or $Name.Length -eq 0) { $Name = 'you'}
if ([string]::IsNullOrEmpty($Name)) { $Name = 'you' }
return "One for $($Name), one for me"
}

View File

@@ -0,0 +1,18 @@
BeforeAll {
. ".\TwoFer.ps1"
}
Describe "Get-TwoFer Tests" {
It "Given <Name> expects <Expected>" -TestCases @(
@{ Name = $null; Expected = "One for you, one for me" },
@{ Name = ""; Expected = "One for you, one for me" },
@{ Name = "Alice"; Expected = "One for Alice, one for me" }
) {
Param(
$Name, $Expected
)
Get-TwoFer -Name $Name | Should -Be $Expected
}
}