Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by ivang
import re
from collections import OrderedDict
NUMERALS = {1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M'}
SUBS = OrderedDict((('DCCCC', 'CM'), ('CCCC', 'CD'), ('LXXXX', 'XC'),
('XXXX', 'XL'), ('VIIII', 'IX'), ('IIII', 'IV')))
def checkio(number):
roman = ''
for key in sorted(NUMERALS.keys(), reverse=True):
roman += number // key * NUMERALS[key]
number %= key
for k, v in SUBS.items():
roman = re.sub(k, v, roman)
return roman
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(6) == 'VI', '6'
assert checkio(76) == 'LXXVI', '76'
assert checkio(499) == 'CDXCIX', '499'
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
Aug. 24, 2015