Last active
December 21, 2015 23:18
-
-
Save ybur-yug/6380828 to your computer and use it in GitHub Desktop.
turns regular numbers to roman numerals up to 3999. Epicodus exercise
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
def roman_nums(number) | |
romans = { | |
1 => "I", | |
2 => "II", | |
3 => "III", | |
4 => "IV", | |
5 => "V", | |
6 => "VI", | |
7 => "VII", | |
8 => "VIII", | |
9 => "IX", | |
10 => "X", | |
20 => "XX", | |
30 => "XXX", | |
40 => "XL", | |
50 => "L", | |
60 => "LX", | |
70 => "LXX", | |
80 => "LXXX", | |
90 => "XC", | |
100 => "C", | |
200 => "CC", | |
300 => "CCC", | |
400 => "CD", | |
500 => "D", | |
600 => "DC", | |
700 => "DCC", | |
800 => "DCCC", | |
900 => "CM", | |
1000 => "M", | |
2000 => "MM", | |
3000 => "MMM" | |
} | |
individual_nums = number.to_s.reverse.split('') | |
romanNum = [] | |
if individual_nums[0].to_i > 0 | |
romanNum.unshift(romans[individual_nums[0].to_i]) | |
end | |
if individual_nums[1].to_i > 0 | |
romanNum.unshift(romans[(individual_nums[1].to_i)*10]) | |
end | |
if individual_nums[2].to_i > 0 | |
romanNum.unshift(romans[(individual_nums[2].to_i)*100]) | |
end | |
if individual_nums[3].to_i > 0 | |
romanNum.unshift(romans[(individual_nums[3].to_i)*1000]) | |
end | |
romanNum.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment