Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Shorter but less readable solution in Clear category for Roman Numerals by virzen
numerals = {
1: 'I',
5: 'V',
10: 'X',
50: 'L',
100: 'C',
500: 'D',
1000: 'M',
}
def checkio(data):
if (type(data) != int or data < 1 or data > 3999):
return ''
result = ''
keys = reversed(sorted(numerals.keys()))
for num in keys:
divided = data / num
result += divided * numerals[num]
data -= divided * num
div = num / 10 if num in [10, 100, 1000] else (num / 5 if num in [5, 50, 500] else -1)
if data < num and div >= 0 and data - (num - div) >= 0:
result += numerals[div] + numerals[num]
data -= num - div
return result
Dec. 13, 2016