Last active
August 29, 2015 14:03
-
-
Save jsuchal/9655c5005b06c70838c8 to your computer and use it in GitHub Desktop.
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' | |
ROMAN_GLYPHS = { | |
1000 => 'M', 900 => 'CM', | |
500 => 'D', 400 => 'CD', | |
100 => 'C', 90 => 'XC', | |
50 => 'L', 40 => 'XL', | |
10 => 'X', 9 => 'IX', | |
5 => 'V', 4 => 'IV', | |
1 => 'I' | |
} | |
def to_roman(number) | |
ROMAN_GLYPHS.inject('') do |result, (limit, glyph)| | |
count, number = number.divmod(limit) | |
result + glyph * count | |
end | |
end | |
class RomanConversions < Minitest::Test | |
def test_to_roman | |
assert_equal 'I', to_roman(1) | |
assert_equal 'II', to_roman(2) | |
assert_equal 'III', to_roman(3) | |
assert_equal 'V', to_roman(5) | |
assert_equal 'VI', to_roman(6) | |
assert_equal 'VIII', to_roman(8) | |
assert_equal 'X', to_roman(10) | |
assert_equal 'XXX', to_roman(30) | |
assert_equal 'XXIV', to_roman(24) | |
assert_equal 'XLII', to_roman(42) | |
assert_equal 'MMXIV', to_roman(2014) | |
assert_equal 'MCMXCIX', to_roman(1999) | |
assert_equal 'DCLXVI', to_roman(666) | |
assert_equal 'CDXLIV', to_roman(444) | |
assert_equal 'IV', to_roman(4) | |
assert_equal 'IX', to_roman(9) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment