Practice makes perfect: Reverse


#1



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


Code Academy returns:
Oops, try again.
Does your reverse function take exactly one argument (a string)?
Your code threw a "'builtin_function_or_method' object has no attribute
'getitem'" error.


I'm trying to convert the input string to a list, take the first entry on the list and append it to the end, then remove it from the beginning. I want this to repeat a number of times equal to the starting length of the list.

At the end, I want to rejoin the list into a string.


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


#2

This is a pretty novel approach, so let's see if we can't get it to work for you. remove() looks for the value or item in the list, del will delete the element at an index. Try using del string[0]


#3

It's probably novel because I'm not terribly good at this and brute forcing my way through it.

I switched to del string[0] and no longer get the previous error. Now, though, CodeAcademy just tells me I'm returning Python! rather than !nohtyP.

I tried throwing a print string at the end of the while series and I get no results at all. If I indent return ''.join(string) I get ython!P as expected. Bit confused by that, actually.


#4

I wouldn't recommend you use the same variable name for two different things.
I'm talking about string = [] and string = list(text). It confuses things.


#5

Okay, now we have the code error out of the way, let's look at the logic. You are grabbing the first character in the list and appending it to the end, then deleting that element. Look closely at what is happening.


#6

Thank you. It's not supposed to be two different variables. I thought I had to create an empty variable named string with string = []; then I populate it with string = list(text).

That may not be the case. In several exercises previously, though, I had to create a blank list first so I just did that.


#7

In this case, having a temporary object is about the only way we can manually reverse the list. Your method is not reversing the text, only shifting it around a key ring.


#8

Remove string = list(text). Strings are iterable so they don't need to be turned into a list or in your case, copied into a different variable.
Python will replace string = [] with string = list(text) because you are using the same variable name for two different things and string = list(text) is more recent. You're going to end up with "Hello WorlddlroW olleH" if your other code works and you have both string variables.


#9

Adooooooyy.

I feel like a dummy.


#10

Given a temporary list,

temp = []

we can iterate the text as a string and use list.insert()

for x in text:
    temp.insert(0, x)
return "".join(temp)

#11

I see how that would work perfectly. That is undoubtedly the most correct way to have taken this approach.

I ended up with:

def reverse(text):
    length = len(text)
    string = list(text)
    newstring = []
    
    while length > 0:
        x = string[-1]
        del string[-1]
        length = length - 1
        newstring.append(x)
    
    return ''.join(newstring)

I made a note of both versions for my reference later. Thank you.


#12

So you want to do it the while loop way?


#13

I don't think I have a particular want. I'm just not terribly good at this and wanted to make sure I could come up with a workable solution that mostly came from my own brain. All of the advice has been hugely appreciated.


#15

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