Created
June 26, 2012 22:43
-
-
Save proudlygeek/2999829 to your computer and use it in GitHub Desktop.
Roman Numbers
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
def romanize(n) | |
number_map = [ | |
["M", 1000], | |
["CM", 900], | |
["D", 500], | |
["CD", 400], | |
["C", 100], | |
["XC", 90], | |
["L", 50], | |
["XL", 40], | |
["X", 10], | |
["IX", 9], | |
["V", 5], | |
["IV", 4], | |
["I", 1] | |
] | |
number = number_map.map do |symbol, value| | |
# Rebind higher-scope variable | |
k,n = n / value, n % value | |
symbol * k | |
end.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment