Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Roman Numerals by bryukh
def checkio(number):
'return roman numeral using the specified integer value from range 1...3999'
units = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
tens = ['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
hundreds = ['C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
res = ''
res += 'M' * (number // 1000)
res += hundreds[((number // 100) % 10) - 1] if ((number // 100) % 10) else ''
res += tens[((number // 10) % 10) - 1] if ((number // 10) % 10) else ''
res += units[(number % 10) - 1] if (number % 10) else ''
return res
if __name__ == '__main__':
print(checkio(1666))
assert checkio(6) == 'VI', 'First'
assert checkio(76) == 'LXXVI', 'Second'
assert checkio(499) == 'CDXCIX', 'Third'
assert checkio(3888) == 'MMMDCCCLXXXVIII', 'Fourth'
print('All ok')
Oct. 28, 2012
Comments: