Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Improved Sequential Dividing solution solution in Clear category for Roman Numerals by romeech
# This is improved version of my previous solution "Sequential dividing"
# Thanks to r.bathoorn for the valuable remarks (https://py.checkio.org/user/r.bathoorn/)
def checkio(data):
units = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
decs = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
hundreds = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
thousands = ['', 'M', 'MM', 'MMM']
dividers = [
(1000, thousands),
(100, hundreds),
(10, decs),
]
remainder = data
roman_parts = ''
for divider, roman_codes in dividers:
times = int(remainder / divider)
remainder = remainder % divider
roman_parts += roman_codes[times]
if remainder:
roman_parts += units[remainder]
return roman_parts
Feb. 20, 2020