ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# epequeno's solution

## to Raindrops in the Rust Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

## Examples

• 28's factors are 1, 2, 4, 7, 14, 28.
• In raindrop-speak, this would be a simple "Plong".
• 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
• In raindrop-speak, this would be a "PlingPlang".
• 34 has four factors: 1, 2, 17, and 34.
• In raindrop-speak, this would be "34".

## Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

## Writing the Code

Execute the tests with:

``````\$ cargo test
``````

All but the first test have been ignored. After you get the first test to pass, open the tests source file wich 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 all tests without editing the tests source file, use:

``````\$ cargo test -- --ignored
``````

To run a specific test, for example `some_test`, you can use:

``````\$ cargo test some_test
``````

If the specfic test is ignored use:

``````\$ cargo test some_test -- --ignored
``````

## Feedback, Issues, Pull Requests

The exercism/rust repository on 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.

## Source

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### raindrops.rs

``````extern crate raindrops;

#[test]
fn test_1() {
assert_eq!("1", raindrops::raindrops(1));
}

#[test]
#[ignore]
fn test_3() {
assert_eq!("Pling", raindrops::raindrops(3));
}

#[test]
#[ignore]
fn test_5() {
assert_eq!("Plang", raindrops::raindrops(5));
}

#[test]
#[ignore]
fn test_7() {
assert_eq!("Plong", raindrops::raindrops(7));
}

#[test]
#[ignore]
fn test_6() {
assert_eq!("Pling", raindrops::raindrops(6));
}

#[test]
#[ignore]
fn test_8() {
assert_eq!("8", raindrops::raindrops(8));
}

#[test]
#[ignore]
fn test_9() {
assert_eq!("Pling", raindrops::raindrops(9));
}

#[test]
#[ignore]
fn test_10() {
assert_eq!("Plang", raindrops::raindrops(10));
}

#[test]
#[ignore]
fn test_14() {
assert_eq!("Plong", raindrops::raindrops(14));
}

#[test]
#[ignore]
fn test_15() {
assert_eq!("PlingPlang", raindrops::raindrops(15));
}

#[test]
#[ignore]
fn test_21() {
assert_eq!("PlingPlong", raindrops::raindrops(21));
}

#[test]
#[ignore]
fn test_25() {
assert_eq!("Plang", raindrops::raindrops(25));
}

#[test]
#[ignore]
fn test_27() {
assert_eq!("Pling", raindrops::raindrops(27));
}

#[test]
#[ignore]
fn test_35() {
assert_eq!("PlangPlong", raindrops::raindrops(35));
}

#[test]
#[ignore]
fn test_49() {
assert_eq!("Plong", raindrops::raindrops(49));
}

#[test]
#[ignore]
fn test_52() {
assert_eq!("52", raindrops::raindrops(52));
}

#[test]
#[ignore]
fn test_105() {
assert_eq!("PlingPlangPlong", raindrops::raindrops(105));
}

#[test]
#[ignore]
fn test_3125() {
assert_eq!("Plang", raindrops::raindrops(3125));
}

#[test]
#[ignore]
fn test_12121() {
assert_eq!("12121", raindrops::raindrops(12121));
}``````
``````use std::collections::HashMap;

pub fn raindrops(n: usize) -> String {
let ordered_targets = vec![3, 5, 7];
let mut targets = HashMap::new();
targets.insert(3, "Pling");
targets.insert(5, "Plang");
targets.insert(7, "Plong");

let mut res = vec![];

for target in ordered_targets {
if n % target == 0 {
res.push(targets[&target])
}
}

if res.len() > 0 {
res.join("").to_string()
} else {
n.to_string()
}
}``````