i wrote this code:
admitted_List = [1, 5, 10, 50, 100, 500, 1000]
tempString = ""
finalList = []
for i in range(len(xkcd)-1):
if int(xkcd[i] + xkcd[i+1]) in admitted_List:
tempString += xkcd[i]
continue
else:
tempString += xkcd[i]
finalList.append(int(tempString))
tempString = ""
return (finalList)
that basically takes in (xkcd) a string of weights of roman numbers like '10010010010100511' and it should return me the list of weights like [100, 100, 100, 10, 100, 5, 1, 1] so that C C C XC V I I makes sense, of course the first 4 chars of the string make the number 1001 that in roman numbers means nothing so my number will be 100 and then the check should stop and begin a new number.
I tried the above algorithm. Please excuse me if bad code or bad body question, I'm pretty new to python and stack overflow.
The idea for this snippet of code is to list the decimal numbers corresponding to roman numbers from bigger value to lower value (see the new definition of variable admitted_List
).
Your code become the following:
def func(xkcd):
admitted_List = ["1000", "500", "100", "50", "10", "5", "1"]
tempString = ""
finalList = []
loop_out = 0
while loop_out == 0:
for roman_num in admitted_List:
#print(roman_num)
if (xkcd.startswith(roman_num)):
finalList.append(int(roman_num))
xkcd = xkcd[len(roman_num):]
if len(xkcd) == 0:
loop_out = 1
break
return (finalList)
xkcd = '10010010010100511'
print(func(xkcd))
It prints the following list: [100, 100, 100, 10, 100, 5, 1, 1]