Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f83505a16919e8f35222f72c3fc98d69 to your computer and use it in GitHub Desktop.
Save anonymous/f83505a16919e8f35222f72c3fc98d69 to your computer and use it in GitHub Desktop.
def solution(roman)
numerals = Hash[ 'X' => 10, 'V' => 5, 'I' => 1, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000 ]
roman_array = roman.scan(/((\w)\2*)/).map(&:first).reverse
decimal_array = roman_array.map { |x|
numerals[x[0]]*x.length
}
last_num = 0
decimal_array.map! { |i|
if last_num > i then
last_num = i
i= -i
else
last_num = i
i
end
}
answer = decimal_array.reduce(:+)
return answer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment