Last active
December 22, 2015 22:19
-
-
Save softr8/6539707 to your computer and use it in GitHub Desktop.
Roman to integers in ruby
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 solution(roman) | |
roman.scan(/IV|IX|CM|XC|I|V|L|X|C|M|D/).inject(0) do |final, current| | |
final += case current | |
when 'IV' then 4 | |
when 'IX' then 9 | |
when 'XC' then 90 | |
when 'CM' then 900 | |
when 'I' then 1 | |
when 'V' then 5 | |
when 'X' then 10 | |
when 'L' then 50 | |
when 'C' then 100 | |
when 'D' then 500 | |
when 'M'then 1000 | |
else 0 | |
end | |
final | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment