Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by BartoszKowalski
def checkio(data):
wynik = ""
data = str(data)
i = 0
if len(data)==4:
wynik += int(data[0])*"M"
data = data[1:]
if len(data)==3:
if data[0]=="0":
x = 0
elif int(data[0]) < 4:
wynik += int(data[0])*"C"
elif int(data[0]) == 4:
wynik += "CD"
elif int(data[0]) == 5:
wynik += "D"
elif int(data[0]) == 9:
wynik += "CM"
elif int(data[0]) > 5:
wynik += "D" + (int(data[0])-5)*"C"
data = data[1:]
if len(data)==2:
if data[0]=="0":
x = 0
elif int(data[0]) < 4:
wynik += int(data[0])*"X"
elif int(data[0]) == 4:
wynik += "XL"
elif int(data[0]) == 5:
wynik += "L"
elif int(data[0]) == 9:
wynik += "XC"
elif int(data[0]) > 5:
wynik += "L" + (int(data[0])-5)*"X"
data = data[1:]
if len(data)==1:
if data[0]=="0":
x = 0
elif int(data[0]) < 4:
wynik += int(data[0])*"I"
elif int(data[0]) == 4:
wynik += "IV"
elif int(data[0]) == 5:
wynik += "V"
elif int(data[0]) == 9:
wynik += "IX"
elif int(data[0]) > 5:
wynik += "V" + (int(data[0])-5)*"I"
data = data[1:]
return wynik
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'
print('Done! Go Check!')
Nov. 26, 2018