Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Advance read solution in Clear category for Reverse Roman Numerals by caldeius
def reverse_roman(roman_string):
#replace this for solution
roman = {'M' : 1000, 'D' : 500, 'C' : 100, 'L' : 50, 'X' : 10, 'V' : 5, 'I' : 1}
roman_list = list(roman_string)
result = 0
current = roman[roman_list.pop(0)]
while roman_list:
prev = current
current = roman[roman_list.pop(0)]
if current > prev:
result -= prev
else:
result += prev
result += current
return result
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('LXXVI') == 76, '76'
assert reverse_roman('CDXCIX') == 499, '499'
assert reverse_roman('MMMDCCCLXXXVIII') == 3888, '3888'
print('Great! It is time to Check your code!');
Feb. 13, 2020
Comments: