Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Reverse-reverse-roman (not fast though...) solution in Creative category for Reverse Roman Numerals by nakanohito_piyo
def reverse_roman(roman_string):
for i in range(4000):
if roman_string==roman(i):
return i
def roman(data):
if data >= 1000:
return 'M' + roman(data-1000)
elif data >= 900:
return 'CM' + roman(data-900)
elif data >= 500:
return 'D' + roman(data-500)
elif data >= 400:
return 'CD' + roman(data-400)
elif data >= 100:
return 'C' + roman(data-100)
elif data >= 90:
return 'XC' + roman(data-90)
elif data >= 50:
return 'L' + roman(data-50)
elif data >= 40:
return 'XL' + roman(data-40)
elif data >= 10:
return 'X' + roman(data-10)
elif data >= 9:
return 'IX' + roman(data-9)
elif data >= 5:
return 'V' + roman(data-5)
elif data >= 4:
return 'IV' + roman(data-4)
elif data >= 1:
return 'I' + roman(data-1)
else:
return ''
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. 6, 2017