Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Reverse Roman Numerals via RegExp solution in Clear category for Reverse Roman Numerals by Molot
import re
def reverse_roman(roman_string):
R_NUMS = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M': 1000,
'IV': 4, 'IX':9, 'XL': 40, 'XC':90, 'CD':400, 'CM':900}
sep_list = re.findall(r'(IV|IX|XL|XC|CD|CM|I|V|X|L|C|D|M)', roman_string)
return sum(map(lambda x: R_NUMS[x], sep_list))
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!');
June 25, 2018