Last active
August 29, 2015 14:12
-
-
Save anonoz/62f50168bbd46a686a7d to your computer and use it in GitHub Desktop.
Done in ruby tuesday 16 for code kata, thanks Jimmy Ngu
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
# Refer http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm | |
require 'rspec/given' | |
require 'roman_numeral_converter' | |
describe RomanNumeralConverter do | |
Given(:converter) { RomanNumeralConverter.new } | |
Then { converter.convert(1) == 'I' } | |
Then { converter.convert(2) == 'II' } | |
Then { converter.convert(3) == 'III' } | |
Then {converter.convert(4) == 'IV'} | |
Then {converter.convert(5) == 'V'} | |
Then {converter.convert(6) == 'VI'} | |
Then {converter.convert(7) == 'VII'} | |
Then {converter.convert(8) == 'VIII'} | |
Then {converter.convert(9) == 'IX'} | |
Then {converter.convert(10) == 'X'} | |
Then {converter.convert(11) == 'XI'} | |
Then {converter.convert(12) == 'XII'} | |
Then {converter.convert(13) == 'XIII'} | |
Then {converter.convert(14) == 'XIV'} | |
Then {converter.convert(15) == 'XV'} | |
Then {converter.convert(16) == 'XVI'} | |
Then {converter.convert(17) == 'XVII'} | |
Then {converter.convert(18) == 'XVIII'} | |
Then {converter.convert(19) == 'XIX'} | |
Then {converter.convert(20) == 'XX'} | |
Then {converter.convert(33) == 'XXXIII'} | |
Then {converter.convert(48) == 'XLVIII'} | |
Then {converter.convert(77) == 'LXXVII'} | |
Then {converter.convert(95) == 'XCV'} | |
Then {converter.convert(124) == 'CXXIV'} | |
Then {converter.convert(433) == 'CDXXXIII' } | |
Then {converter.convert(900) == 'CM' } | |
Then {converter.convert(1500) == 'MD' } | |
Then {converter.convert(1800) == 'MDCCC' } | |
Then {converter.convert(4999) == 'MMMMCMXCIX'} | |
end |
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
class RomanNumeralConverter | |
def convert(n) | |
case n | |
when 1..3 then 'I' * n | |
when 4 then 'IV' | |
when 5..8 then 'V' + convert(n - 5) | |
when 9 then 'IX' | |
when 10..39 then 'X' + convert(n - 10) | |
when 40..49 then 'XL' + convert(n % 10) | |
when 50..89 then 'L' + convert(n % 50) | |
when 90..99 then 'XC' + convert(n % 90) | |
when 100..399 then 'C' + convert(n - 100) | |
when 400..499 then 'CD' + convert(n % 400) | |
when 500..899 then 'D' + convert(n % 500) | |
when 900..999 then 'CM' + convert(n % 900) | |
when 1000..5000 then 'M' + convert(n - 1000) | |
else '' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment