Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having trouble with a function that has to split a string into roman numbers weights

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this code:

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]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading