Created
February 5, 2016 23:13
-
-
Save j0ni/5d3e7ef42dc1e724d49e to your computer and use it in GitHub Desktop.
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 romans | |
{"M" => 1000, "D" => 500, "C" => 100, "L" => 50, "X" => 10, "V" => 5, "I" => 1} | |
end | |
def find_next_component(num) | |
romans.select { |l, v| v <= num } | |
.sort_by { |l, v| v } | |
.last | |
end | |
def to_roman(num) | |
answer = "" | |
while num > 0 | |
current_component = find_next_component(num) | |
puts "Next component found: #{current_component}" | |
answer = "#{answer}#{current_component.first}" | |
puts "The answer so far: #{answer}" | |
num -= current_component.last | |
end | |
answer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment