easiest
This commit is contained in:
parent
d5f25bd8bf
commit
782185e317
7 changed files with 367 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"
|
||||||
|
}
|
||||||
2
rust/raindrops/.gitignore
vendored
Normal file
2
rust/raindrops/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
Cargo.lock
|
||||||
9
rust/raindrops/Cargo.toml
Normal file
9
rust/raindrops/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "raindrops"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
# Not all libraries from crates.io are available in Exercism's test runner.
|
||||||
|
# The full list of available libraries is here:
|
||||||
|
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
|
||||||
|
[dependencies]
|
||||||
89
rust/raindrops/HELP.md
Normal file
89
rust/raindrops/HELP.md
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
|
Head to [the forum](https://forum.exercism.org/c/programming/rust/) and create a post to provide feedback about an exercise or if you want to help implement new exercises.
|
||||||
|
Members of the rust track team are happy to help!
|
||||||
|
|
||||||
|
The GitHub [track repository][github] is the home for all of the Rust exercises.
|
||||||
|
|
||||||
|
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
|
||||||
14
rust/raindrops/src/lib.rs
Normal file
14
rust/raindrops/src/lib.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
pub fn raindrops(n: u32) -> String {
|
||||||
|
let mut res = String::new();
|
||||||
|
if 0 == n % 3 {
|
||||||
|
res.push_str("Pling");
|
||||||
|
}
|
||||||
|
if 0 == n % 5 {
|
||||||
|
res.push_str("Plang");
|
||||||
|
}
|
||||||
|
if 0 == n % 7 {
|
||||||
|
res.push_str("Plong");
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.is_empty() { n.to_string() } else { res }
|
||||||
|
}
|
||||||
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…
Add table
Add a link
Reference in a new issue