Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Reverse Roman Numerals by koyana222
def reverse_roman(roman_string):
dic = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
result = 0
list1 = ["V","X"]
list2 = ["C","L"]
list3 = ["M","D"]
for i in range(len(roman_string)-1,-1,-1):
if i == len(roman_string)-1:
result += dic[roman_string[i]]
else:
if roman_string[i] == "I" and roman_string[i+1] in list1:
result -= dic[roman_string[i]]
continue
if roman_string[i] == "X" and roman_string[i+1] in list2:
result -= dic[roman_string[i]]
continue
if roman_string[i] == "C" and roman_string[i+1] in list3:
result -= dic[roman_string[i]]
else:
result += dic[roman_string[i]]
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!');
May 12, 2019
Comments: