Last active
May 16, 2019 18:14
-
-
Save kkchu791/2c72eff5506d1d7e265f412675a9e557 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Raindrops | |
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". | |
* * * * | |
Solution: | |
module Raindrops | |
SOUNDS={ 3 => 'Pling', | |
5 => 'Plang', | |
7 => 'Plong'} | |
def self.convert(drop_count) | |
make_sounds(drop_count) | |
end | |
def self.make_sounds(drop_count) | |
total_sounds = SOUNDS.map do |num, sound| | |
sound if (drop_count % num).zero? | |
end.compact.join | |
total_sounds.empty? ? drop_count.to_s : total_sounds | |
end | |
end | |
* * * * | |
def test_the_sound_for_105_is_plingplangplong_as_it_has_factors_3_5_and_7 | |
assert_equal "PlingPlangPlong", Raindrops.convert(105) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment