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,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
}
}