Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sequential dividing solution in Clear category for Roman Numerals by romeech
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
if times:
roman_parts.append(roman_codes[times - 1])
if remainder:
roman_parts.append(units[remainder - 1])
return ''.join(roman_parts)
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'
print('Done! Go Check!')
Jan. 12, 2020
Comments: