Practice Makes Perfect : Reverse


#1


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Your function fails on reverse("coolstory"). It returns "yroootslc" when it should return "yrotslooc".


I am lost, could someone walk me through what is happening from the beginning of the while?


def reverse(x):
    y=[]
    for letter in x:
        y.append(letter)
    z=[]
    while len(y)>=1:
        print y
        z.append(y[len(y)-1])
        y.remove(y[len(y)-1])
    return "".join(z)
print reverse("coolstory")

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#


#2

remove() doesn't remove based index, for this we have del

remove() will remove the first match it can find, so if we would do:

['c', 'o', 'o', 'l', 's', 't', 'o', 'r', 'y'].remove('o')

we get:

['c', 'o', 'l', 's', 't', 'o', 'r', 'y']

so its impossible to remove the o in story first using remove()


#3

Awesome thanks for your help!


#4

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