Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by bartorbard
def rChar0(value):
x = int(value)
if x < 4:
x = (x*"I")
elif x==4:
x = "IV"
elif x == 5:
x = "V"
elif x>5 and x < 9:
x = "V" + ((x-5)*"I")
elif x == 9:
x = "IX"
elif x ==0:
x = ""
return x
def rChar1(value):
x = int(value)
if x < 4:
x = (x*"X")
elif x ==4:
x = "XL"
elif x == 5:
x = "L"
elif x>5 and x < 9:
x = "L" + ((x-5)*"X")
elif x == 9:
x = "XC"
elif x ==0:
x = ""
return x
def rChar2(value):
x = int(value)
if x < 4:
x = (x*"C")
elif x==4:
x = "CD"
elif x == 5:
x = "D"
elif x>5 and x < 9:
x = "D" + ((x-5)*"C")
elif x == 9:
x = "CM"
elif x ==0:
x = ""
return x
def rChar3(value):
x = int(value)
if x < 5:
x = (x*"M")
else:
print ("error")
return x
def function (data2):
rChar = []
x = data2
x = str(x)
lenX = len(x) - 1
for i in x:
rChar.append(x[lenX:lenX+1])
lenX = lenX - 1
theSum = ""
if (len(x)==4):
theSum = (rChar3(rChar[3]) + rChar2(rChar[2]) + rChar1(rChar[1]) + rChar0(rChar[0]) )
elif (len(x)==3):
theSum = (rChar2(rChar[2]) + rChar1(rChar[1]) + rChar0(rChar[0]) )
elif (len(x)==2):
theSum = (rChar1(rChar[1]) + rChar0(rChar[0]) )
elif (len(x)==1):
theSum = (rChar0(rChar[0]) )
return (theSum)
def checkio(data):
data1 = data
x = (str(function(data1)))
#print (x)
return x
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. 26, 2016