7 Reverse -- curious


#1

Oops, try again. Your function fails on reverse("Python!"). It returns "['!', 'n', 'o', 'h']" when it should return "!nohtyP".

I am very slowly working through this problem. I know I am not even close to the correct answer but I am curious with what happened here. The error says that it made it's way through !, n, o, h. Why were both y and P not accounted for?


def reverse(text):
    list1 = list(text)
    list2 = list()
    for l in list1:
        list2.append(list1[-1])
        if len(list1) > 0:
            list1.remove(list1[-1])
        else:
            break
    return list2


#2

The problem is the built in increment of the for loop. Easier to do this in a while loop where the conditional is the control:

    list2 = []
    while len(list1) > 0:
        list2.append(list1[-1])
        list1.remove(list1[-1])
    return ''.join(list2)

Not tested, but reasonable to expect a reversed string.


#3

Thank you!!
I am now getting this error:
Oops, try again. Your function fails on reverse("coolstory"). It returns "yroootslc" when it should return "yrotslooc".

Somehow the "o"s are getting put in the wrong place?

def reverse(text):
list1 = list(text)
list2 = list()
while len(list1) > 0:
list2.append(list1[-1])
list1.remove(list1[-1])
return ''.join(list2)


#4

def reverse(text):
    list1 = list(text)
    list2 = list()
    while len(list1) > 0:
        list2.append(list1[-1])
        list1.remove(list1[-1])
    return ''.join(list2)

That question was bound to come up. It centers around the fact that remove() is given a character to remove. This brings into question the use of that built-in for this problem.

What about del?

del list1[-1]

as in,

>>> def reverse(text):
    list1 = list(text)
    list2 = list()
    while len(list1) > 0:
        list2.append(list1[-1])
        del list1[-1]
    return ''.join(list2)

>>> reverse('coolstory')
'yrotslooc'
>>>

#5

That worked! Thank you!
I still don't completely understand what happened. I would understand if the append needed to be modified so that it didn't append all the "o"s together/at one time and then continue to other letters. But the problem was the remove. So somehow the remove made the append, append the letters in the wrong order?


#6
replace(list1[-1])

when it found an o it removed the first one from the left, then later the next, and last, the third one. It did not relate to the char by its position in the string. That's why we use del since it focuses on index, not character.

I think it is great that you worked out this rather unique approach. We don't see this one very often. Hopefully you will continue to look for other solutions, just for the fun and experience.

Of course, if all we wanted was a shortcut we could have written,

text[::-1]

The above is a reverse slice. Not as much fun as discovering new algorithms, though, is it?


#7

Thank you, but I actually didn't know about this reverse slice haha. In the instructions it said not to use it, which of course made me curious and therefore now know about it. Now that I understand that there are a myriad of solutions to a problem, I have more confidence to just start trying things first and then finding the "better" options.
Thanks for being supportive and not making fun of my odd code decisions!


#8

And now we know why. Practice implies an expanding of the mind which only comes when we face down difficulty. Keep it up. It is too soon to set our sights on 'better'. What is best is what gives us some take away understanding and new discovery. Leave the shortcuts and better ways for the less ambitious and pour on everything you have to stretch the envelope.

Myriad solutions implies varying scenarios. The more we dream up, the more solutions we 'invent', test and improve or disprove. Keep an open mind (as you obviously have) and you will go far.


#9

7 posts were split to a new topic: No need to transform it to list


#10