Created
September 1, 2015 23:52
-
-
Save Scoutski/e60895f4c6b9688da5fa to your computer and use it in GitHub Desktop.
Roman Numerals Solution
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
require 'pry' | |
class RomanNumerals | |
def initialize num | |
to_roman num | |
end | |
def to_roman num | |
result = "" | |
roman_mappings.each do |arabic, roman| | |
while num >= arabic | |
result << roman | |
num -= arabic | |
end | |
end | |
puts result | |
result | |
end | |
def roman_mappings | |
{ | |
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', | |
} | |
end | |
end | |
roman = RomanNumerals.new 2900 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment