Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Reverse string solution in Clear category for Reverse Roman Numerals by maksymilian.czop
def reverse_roman(roman_string):
lookup = {"I":1,"V":5, "X":10, "L":50, "C":100, "D":500, "M":1000 }
value = 0
last_char = "I"
for char in roman_string[::-1]:
if lookup[char] >= lookup[last_char] :
value = value + lookup[char]
else:
value = value - lookup[char]
last_char = char
return value
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert reverse_roman('VI') == 6, '6'
assert reverse_roman('III') == 3, '3'
assert reverse_roman('LXXVI') == 76, '76'
assert reverse_roman('IV') == 4, '4'
assert reverse_roman('IX') == 9, '9'
assert reverse_roman('XIV') == 14, '14'
assert reverse_roman('LXIX') == 69, '69'
assert reverse_roman('CDXCIX') == 499, '499'
assert reverse_roman('MMMDCCCLXXXVIII') == 3888, '3888'
print('Great! It is time to Check your code!');
Feb. 10, 2019