Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by lukasz.bogaczynski
romans = [[1000, "M"], [500, "D"], [100, "C"], [50, "L"], [10, "X"], [5, "V"], [1, "I"]]
substractable = {4: "IV", 9: "IX", 40: "XL", 90: "XC", 400: "CD", 900: "CM"}
def checkio(number_to_transform):
result = []
while number_to_transform > 0:
number_as_str = str(number_to_transform)
most_significant = ((10 ** (len(number_as_str) - 1))) * int(number_as_str[0])
if most_significant in substractable.keys():
number_to_transform = number_to_transform - most_significant
result.append(substractable[most_significant])
else:
for r in romans:
if r[0] > number_to_transform:
continue
number_to_transform = number_to_transform - r[0]
result.append(r[1])
break
return ''.join(result)
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'
Nov. 13, 2017