Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First 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
if data < num:
if num in [10, 100, 1000] and data - (num - num / 10) >= 0:
result += numerals[num / 10] + numerals[num]
data -= num - num / 10
if num in [5, 50, 500] and data - (num - num / 5) >= 0:
result += numerals[num / 5] + numerals[num]
data -= num - num / 5
return result
if __name__ == '__main__':
assert checkio('test') == ''
assert checkio(('stuff', 11)) == ''
assert checkio([1, 2, 3]) == ''
assert checkio(4000) == ''
assert checkio(0) == ''
assert checkio(6) == 'VI', '6' + ' was: ' + checkio(6)
assert checkio(76) == 'LXXVI', '76'
assert checkio(99) == 'XCIX', '99' + ' was: ' + checkio(99)
assert checkio(499) == 'CDXCIX', '499, was: ' + checkio(499)
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
print('All tests passed')
Dec. 13, 2016