Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by dominieq
def checkio(data):
number = ""
values = [1000,500,100,50,10,5,1]
roman_values = ["M","D","C","L","X","V","I"]
c = 0
for i in values:
count = 0
while data >= i:
count = count + 1
data = data - i
if count > 3:
length_before = len(number)
before = roman_values[c-1]
next = roman_values[c] + roman_values[c-2]
number = number.replace(before, next)
length_after = len(number)
if length_before == length_after:
next = roman_values[c] + roman_values[c-1]
number = number + next
else:
for i in range(count):
number = number + roman_values[c]
c = c+1
return number
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'
Oct. 21, 2016