Created
October 23, 2013 16:42
-
-
Save yitznewton/7122111 to your computer and use it in GitHub Desktop.
Integer to Roman. Assumes n >= 0
This file contains hidden or 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
| 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