Solve raindrops
This commit is contained in:
parent
35a2ab0b14
commit
27979e5ec0
8 changed files with 364 additions and 0 deletions
42
rust/raindrops/.exercism/config.json
Normal file
42
rust/raindrops/.exercism/config.json
Normal file
|
@ -0,0 +1,42 @@
|
|||
{
|
||||
"authors": [
|
||||
"EduardoBautista"
|
||||
],
|
||||
"contributors": [
|
||||
"ashleygwilliams",
|
||||
"ClashTheBunny",
|
||||
"coriolinus",
|
||||
"cwhakes",
|
||||
"eddyp",
|
||||
"EduardoBautista",
|
||||
"efx",
|
||||
"ErikSchierboom",
|
||||
"IanWhitney",
|
||||
"kytrinyx",
|
||||
"leoyvens",
|
||||
"lutostag",
|
||||
"mkantor",
|
||||
"nfiles",
|
||||
"petertseng",
|
||||
"rofrol",
|
||||
"stevejb71",
|
||||
"stringparser",
|
||||
"xakon",
|
||||
"ZapAnton"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"src/lib.rs",
|
||||
"Cargo.toml"
|
||||
],
|
||||
"test": [
|
||||
"tests/raindrops.rs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.rs"
|
||||
]
|
||||
},
|
||||
"blurb": "Convert a number into its corresponding raindrop sounds - Pling, Plang and Plong.",
|
||||
"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"
|
||||
}
|
1
rust/raindrops/.exercism/metadata.json
Normal file
1
rust/raindrops/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"track":"rust","exercise":"raindrops","id":"19130808c799452d9acbf7b54cde3aa0","url":"https://exercism.org/tracks/rust/exercises/raindrops","handle":"averywinters","is_requester":true,"auto_approve":false}
|
8
rust/raindrops/.gitignore
vendored
Normal file
8
rust/raindrops/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
**/*.rs.bk
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||
Cargo.lock
|
4
rust/raindrops/Cargo.toml
Normal file
4
rust/raindrops/Cargo.toml
Normal file
|
@ -0,0 +1,4 @@
|
|||
[package]
|
||||
edition = "2021"
|
||||
name = "raindrops"
|
||||
version = "1.1.0"
|
86
rust/raindrops/HELP.md
Normal file
86
rust/raindrops/HELP.md
Normal file
|
@ -0,0 +1,86 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
```
|
||||
|
||||
All but the first test have been ignored. After you get the first test to
|
||||
pass, open the tests source file which is located in the `tests` directory
|
||||
and remove the `#[ignore]` flag from the next test and get the tests to pass
|
||||
again. Each separate test is a function with `#[test]` flag above it.
|
||||
Continue, until you pass every test.
|
||||
|
||||
If you wish to run _only ignored_ tests without editing the tests source file, use:
|
||||
|
||||
```bash
|
||||
$ cargo test -- --ignored
|
||||
```
|
||||
|
||||
If you are using Rust 1.51 or later, you can run _all_ tests with
|
||||
|
||||
```bash
|
||||
$ cargo test -- --include-ignored
|
||||
```
|
||||
|
||||
To run a specific test, for example `some_test`, you can use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test
|
||||
```
|
||||
|
||||
If the specific test is ignored, use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test -- --ignored
|
||||
```
|
||||
|
||||
To learn more about Rust tests refer to the online [test documentation][rust-tests].
|
||||
|
||||
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` 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 [Rust track's documentation](https://exercism.org/docs/tracks/rust)
|
||||
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
## Rust Installation
|
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||
resources.
|
||||
|
||||
## Submitting the solution
|
||||
|
||||
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
|
||||
|
||||
## Feedback, Issues, Pull Requests
|
||||
|
||||
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
|
||||
|
||||
If you want to know more about Exercism, take a look at the [contribution guide].
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
||||
[help-page]: https://exercism.org/tracks/rust/learning
|
||||
[github]: https://github.com/exercism/rust
|
||||
[contribution guide]: https://exercism.org/docs/community/contributors
|
66
rust/raindrops/README.md
Normal file
66
rust/raindrops/README.md
Normal file
|
@ -0,0 +1,66 @@
|
|||
# Raindrops
|
||||
|
||||
Welcome to Raindrops on Exercism's Rust Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Introduction
|
||||
|
||||
Raindrops is a slightly more complex version of the FizzBuzz challenge, a classic interview question.
|
||||
|
||||
## Instructions
|
||||
|
||||
Your task is to convert a number into its corresponding raindrop sounds.
|
||||
|
||||
If a given number:
|
||||
|
||||
- is divisible by 3, add "Pling" to the result.
|
||||
- is divisible by 5, add "Plang" to the result.
|
||||
- is divisible by 7, add "Plong" to the result.
|
||||
- **is not** divisible by 3, 5, or 7, the result should be the number as a string.
|
||||
|
||||
## Examples
|
||||
|
||||
- 28 is divisible by 7, but not 3 or 5, so the result would be `"Plong"`.
|
||||
- 30 is divisible by 3 and 5, but not 7, so the result would be `"PlingPlang"`.
|
||||
- 34 is not divisible by 3, 5, or 7, so the result would be `"34"`.
|
||||
|
||||
~~~~exercism/note
|
||||
A common way to test if one number is evenly divisible by another is to compare the [remainder][remainder] or [modulus][modulo] to zero.
|
||||
Most languages provide operators or functions for one (or both) of these.
|
||||
|
||||
[remainder]: https://exercism.org/docs/programming/operators/remainder
|
||||
[modulo]: https://en.wikipedia.org/wiki/Modulo_operation
|
||||
~~~~
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @EduardoBautista
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ashleygwilliams
|
||||
- @ClashTheBunny
|
||||
- @coriolinus
|
||||
- @cwhakes
|
||||
- @eddyp
|
||||
- @EduardoBautista
|
||||
- @efx
|
||||
- @ErikSchierboom
|
||||
- @IanWhitney
|
||||
- @kytrinyx
|
||||
- @leoyvens
|
||||
- @lutostag
|
||||
- @mkantor
|
||||
- @nfiles
|
||||
- @petertseng
|
||||
- @rofrol
|
||||
- @stevejb71
|
||||
- @stringparser
|
||||
- @xakon
|
||||
- @ZapAnton
|
||||
|
||||
### 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
|
12
rust/raindrops/src/lib.rs
Normal file
12
rust/raindrops/src/lib.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
pub fn raindrops(n: u32) -> String {
|
||||
match (n % 3, n % 5, n % 7) {
|
||||
(0, 0, 0) => "PlingPlangPlong".to_string(),
|
||||
(0, 0, _) => "PlingPlang".to_string(),
|
||||
(0, _, 0) => "PlingPlong".to_string(),
|
||||
(0, _, _) => "Pling".to_string(),
|
||||
(_, 0, 0) => "PlangPlong".to_string(),
|
||||
(_, 0, _) => "Plang".to_string(),
|
||||
(_, _, 0) => "Plong".to_string(),
|
||||
(_, _, _) => format!("{n}"),
|
||||
}
|
||||
}
|
145
rust/raindrops/tests/raindrops.rs
Normal file
145
rust/raindrops/tests/raindrops.rs
Normal file
|
@ -0,0 +1,145 @@
|
|||
use raindrops::*;
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_1_is_1() {
|
||||
let input = 1;
|
||||
let output = raindrops(input);
|
||||
let expected = "1";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_3_is_pling() {
|
||||
let input = 3;
|
||||
let output = raindrops(input);
|
||||
let expected = "Pling";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_5_is_plang() {
|
||||
let input = 5;
|
||||
let output = raindrops(input);
|
||||
let expected = "Plang";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_7_is_plong() {
|
||||
let input = 7;
|
||||
let output = raindrops(input);
|
||||
let expected = "Plong";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_6_is_pling_as_it_has_a_factor_3() {
|
||||
let input = 6;
|
||||
let output = raindrops(input);
|
||||
let expected = "Pling";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2_to_the_power_3_does_not_make_a_raindrop_sound_as_3_is_the_exponent_not_the_base() {
|
||||
let input = 8;
|
||||
let output = raindrops(input);
|
||||
let expected = "8";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_9_is_pling_as_it_has_a_factor_3() {
|
||||
let input = 9;
|
||||
let output = raindrops(input);
|
||||
let expected = "Pling";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_10_is_plang_as_it_has_a_factor_5() {
|
||||
let input = 10;
|
||||
let output = raindrops(input);
|
||||
let expected = "Plang";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_14_is_plong_as_it_has_a_factor_of_7() {
|
||||
let input = 14;
|
||||
let output = raindrops(input);
|
||||
let expected = "Plong";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_15_is_plingplang_as_it_has_factors_3_and_5() {
|
||||
let input = 15;
|
||||
let output = raindrops(input);
|
||||
let expected = "PlingPlang";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_21_is_plingplong_as_it_has_factors_3_and_7() {
|
||||
let input = 21;
|
||||
let output = raindrops(input);
|
||||
let expected = "PlingPlong";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_25_is_plang_as_it_has_a_factor_5() {
|
||||
let input = 25;
|
||||
let output = raindrops(input);
|
||||
let expected = "Plang";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_27_is_pling_as_it_has_a_factor_3() {
|
||||
let input = 27;
|
||||
let output = raindrops(input);
|
||||
let expected = "Pling";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_35_is_plangplong_as_it_has_factors_5_and_7() {
|
||||
let input = 35;
|
||||
let output = raindrops(input);
|
||||
let expected = "PlangPlong";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_49_is_plong_as_it_has_a_factor_7() {
|
||||
let input = 49;
|
||||
let output = raindrops(input);
|
||||
let expected = "Plong";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_52_is_52() {
|
||||
let input = 52;
|
||||
let output = raindrops(input);
|
||||
let expected = "52";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_105_is_plingplangplong_as_it_has_factors_3_5_and_7() {
|
||||
let input = 105;
|
||||
let output = raindrops(input);
|
||||
let expected = "PlingPlangPlong";
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sound_for_3125_is_plang_as_it_has_a_factor_5() {
|
||||
let input = 3125;
|
||||
let output = raindrops(input);
|
||||
let expected = "Plang";
|
||||
assert_eq!(output, expected);
|
||||
}
|
Loading…
Reference in a new issue