Skip to content

Instantly share code, notes, and snippets.

@yitznewton
Created October 23, 2013 16:42
Show Gist options
  • Select an option

  • Save yitznewton/7122111 to your computer and use it in GitHub Desktop.

Select an option

Save yitznewton/7122111 to your computer and use it in GitHub Desktop.
Integer to Roman. Assumes n >= 0
ROMANS = {
1 => "I",
4 => "IV",
5 => "V",
10 => "X",
40 => "XL",
50 => "L",
90 => "XC",
100 => "C",
400 => "CD",
500 => "D",
900 => "CM",
1000 => "M",
}
def solution(n)
if n == 0
return ""
end
current_int = closest_less_or_equal(n, ROMANS.keys)
ROMANS[current_int] + solution(n - current_int)
end
def closest_less_or_equal(n, ints)
ints.select { |int| int <= n }.max
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment