Last active
August 29, 2015 14:10
-
-
Save paulghaddad/cbf3f885079f69411ab4 to your computer and use it in GitHub Desktop.
Exercism: Raindrops
This file contains hidden or 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 | |
Write a program that converts a number to a string, the contents of which depends on the number's prime factors. | |
- If the number contains 3 as a prime factor, output 'Pling'. | |
- If the number contains 5 as a prime factor, output 'Plang'. | |
- If the number contains 7 as a prime factor, output 'Plong'. | |
- If the number does not contain 3, 5, or 7 as a prime factor, | |
just pass the number's digits straight through. | |
## Examples | |
- 28's prime-factorization is 2, 2, 7. | |
- In raindrop-speak, this would be a simple "Plong". | |
- 1755 prime-factorization is 3, 3, 3, 5, 13. | |
- In raindrop-speak, this would be a "PlingPlang". | |
- The prime factors of 34 are 2 and 17. | |
- Raindrop-speak doesn't know what to make of that, | |
so it just goes with the straightforward "34". | |
## Source | |
A variation on a famous interview question intended to weed out potential candidates. [view source](http://jumpstartlab.com) |
This file contains hidden or 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 Exercism Problem ############## | |
class Raindrops | |
class << self | |
def convert(number) | |
add_sounds(number) || number.to_s | |
end | |
private | |
FACTOR_STRING_TABLE = { | |
3 => 'Pling', | |
5 => 'Plang', | |
7 => 'Plong' | |
} | |
def add_sounds(number) | |
new_string = '' | |
FACTOR_STRING_TABLE.each do |factor, string| | |
new_string << string if factor?(number, factor) | |
end | |
new_string.empty? ? nil : new_string | |
end | |
def factor?(integer, factor) | |
(integer % factor).zero? | |
end | |
end | |
end |
This file contains hidden or 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
class Raindrops | |
class << self | |
def convert(num) | |
sounds_for(num) || num.to_s | |
end | |
private | |
def sounds_for(num) | |
sounds = "" | |
sounds += "Pling" if (num % 3).zero? | |
sounds += "Plang" if (num % 5).zero? | |
sounds += "Plong" if (num % 7).zero? | |
sounds.empty? ? nil : sounds | |
end | |
end | |
end |
This file contains hidden or 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
require 'minitest/autorun' | |
require_relative 'raindrops' | |
class RaindropsTest < MiniTest::Unit::TestCase | |
def test_1 | |
assert_equal "1", Raindrops.convert(1) | |
end | |
def test_3 | |
assert_equal "Pling", Raindrops.convert(3) | |
end | |
def test_5 | |
assert_equal "Plang", Raindrops.convert(5) | |
end | |
def test_7 | |
assert_equal "Plong", Raindrops.convert(7) | |
end | |
def test_6 | |
assert_equal "Pling", Raindrops.convert(6) | |
end | |
def test_9 | |
assert_equal "Pling", Raindrops.convert(9) | |
end | |
def test_10 | |
assert_equal "Plang", Raindrops.convert(10) | |
end | |
def test_14 | |
assert_equal "Plong", Raindrops.convert(14) | |
end | |
def test_15 | |
assert_equal "PlingPlang", Raindrops.convert(15) | |
end | |
def test_21 | |
assert_equal "PlingPlong", Raindrops.convert(21) | |
end | |
def test_25 | |
assert_equal "Plang", Raindrops.convert(25) | |
end | |
def test_35 | |
assert_equal "PlangPlong", Raindrops.convert(35) | |
end | |
def test_49 | |
assert_equal "Plong", Raindrops.convert(49) | |
end | |
def test_52 | |
skip | |
assert_equal "52", Raindrops.convert(52) | |
end | |
def test_105 | |
assert_equal "PlingPlangPlong", Raindrops.convert(105) | |
end | |
def test_12121 | |
skip | |
assert_equal "12121", Raindrops.convert(12121) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment