Skip to content

Instantly share code, notes, and snippets.

@j0ni
Created February 5, 2016 23:13
Show Gist options
  • Save j0ni/5d3e7ef42dc1e724d49e to your computer and use it in GitHub Desktop.
Save j0ni/5d3e7ef42dc1e724d49e to your computer and use it in GitHub Desktop.
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