Skip to content

Instantly share code, notes, and snippets.

@McTano
Forked from davidvandusen/roman_numerals.rb
Last active May 28, 2016 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McTano/fe6fbea13533a8d104c667ce40db2d77 to your computer and use it in GitHub Desktop.
Save McTano/fe6fbea13533a8d104c667ce40db2d77 to your computer and use it in GitHub Desktop.
NUMERALS = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
LX: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1
}
def to_roman(number)
result = ''
NUMERALS.each do |roman, arabic|
div, number = number.divmod(arabic)
result << (roman.to_s * div)
end
result
end
def test_drive
puts "My totally sweet testing script:"
puts ""
puts "input | expected | actual"
puts "------|----------|-------"
puts "4 | IV | #{to_roman(4)}"
puts "9 | IX | #{to_roman(9)}"
puts "13 | XIII | #{to_roman(13)}"
puts "1453 | MCDLIII | #{to_roman(1453)}"
puts "1646 | MDCXLVI | #{to_roman(1646)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment