Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
divmod and match-case solution in Clear category for Roman Numerals by lxf42
def checkio(data):
ROMANS = {1:'I', 5:'V', 10:'X', 50:'L', 100:'C', 500:'D', 1000:'M'}
pos = 1
roman = ''
while data > 0:
data, val = divmod(data, 10)
match val:
case 1 | 2 | 3:
roman = val * ROMANS[pos] + roman
case 4:
roman = ROMANS[pos] + ROMANS[pos * 5] + roman
case 5:
roman = ROMANS[pos * 5] + roman
case 6 | 7 | 8:
roman = ROMANS[pos * 5] + (val - 5) * ROMANS[pos] + roman
case 9:
roman = ROMANS[pos] + ROMANS[pos * 10] + roman
pos *= 10
return roman
March 22, 2022
Comments: