Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by kv.polyanskiy
def hundreds_tens_ones(num, min_sign, half_sign, max_sign):
res = ''
if 0 < num <= 3:
res = min_sign * num
elif 3 < num <= 5:
res = min_sign * (5 - num) + half_sign
elif 5 < num <= 8:
res = half_sign + min_sign * (num - 5)
elif 8 < num < 10:
res = min_sign * (10 - num) + max_sign
else:
pass
return res
def checkio(data):
res = ''
thousands = data // 1000
if thousands > 0:
res = 'M' * thousands
hundreds = data % 1000 // 100
res += hundreds_tens_ones(hundreds, 'C', 'D', 'M')
tens = data % 1000 % 100 // 10
res += hundreds_tens_ones(tens, 'X', 'L', 'C')
ones = data % 1000 % 100 % 10
res += hundreds_tens_ones(ones, 'I', 'V', 'X')
return res
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'
April 27, 2015
Comments: