Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second, with better understading the roman number, which I will keep it as a function. solution in Clear category for Reverse Roman Numerals by ArchTauruS
def reverse_roman(roman_string):
""" Return the decimal representation of the roman number as an int.
"""
result = 0
roman_num_vals = {
"M" : 1000,
"CM" : 900,
"D" : 500,
"CD" : 400,
"C" : 100,
"XC" : 90,
"L" : 50,
"XL" : 40,
"X" : 10,
"IX" : 9,
"V" : 5,
"IV" : 4,
"I" : 1
}
for nums, value in roman_num_vals.items():
while roman_string.startswith(nums):
roman_string = roman_string.replace(nums, "", 1)
result += value
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 16, 2018