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 Jakob_Wolitzki
def reverse_roman(str):
tab = {
'I' : 1,
'IV' : 4,
'V' :5,
'IX' :9,
'X' :10,
'XL' :40,
'L' :50,
'XC' :90,
'C' :100,
'CD':400,
'D' :500,
'CM' :900,
'M':1000
}
i = 0
wynik = 0
while i < len(str)-1:
if tab[str[i]] < tab[str[i+1]]:
wynik += tab[str[i]+str[i+1]]
i+=2
else:
wynik+=tab[str[i]]
i+=1
if i < len(str):
wynik+=tab[str[i]]
return (wynik)
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!');
Oct. 23, 2017