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 PythonLearner
ROMAN_NUMERALS = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
def reverse_roman(roman_number: str) -> int:
number = 0
for current_numeral, next_numeral in zip(roman_number, roman_number[1:]):
if ROMAN_NUMERALS[current_numeral] >= ROMAN_NUMERALS[next_numeral]:
number += ROMAN_NUMERALS[current_numeral]
else:
number -= ROMAN_NUMERALS[current_numeral]
number += ROMAN_NUMERALS[roman_number[-1]]
return number
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!');
March 9, 2019
Comments: