Why does my reverse -function work on some inputs but not all inputs?


#1



def reverse(x):
    charcount = 0
    xlist = []

    #xlist = x.split()

    for char in str(x):
        charcount += 1

    chars = range(charcount)

    for i in range(charcount):
        xlist.append(str(x[i:(i + 1)]))

    # now I have a list consisting of the characters of the input str given to x
    reverselist = []
    subtractor = 1

    for char in xlist:
        if xlist.index(char) == 0:
            reverselist.append(xlist[charcount - 1])
        else:
            reverselist.append(xlist[(charcount - subtractor)])
        subtractor += 1

    reverse_string = ''

    for i in reverselist:
        reverse_string = (reverse_string + i)

    return reverse_string


#2

because index() always finds the first match. for example:

"letter".index("e") will always give 1, never 4


#3

Thank you. You're a time saver.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.