7 - 'Reverse' Solution Critique


Hello all!

I'm a Python newbie and just solved the "Reverse" challenge in the 'Practice Makes Perfect' section. My resolution is posted below, but I'd like to know what are some more elegant solutions that the Python Pros here would have used?

def reverse(text):
    temp = []
    final = ''
    for c in text:
    for i in temp:
        x = int(len(temp)) - 1
        while x >= 0:
            final += temp[x]
            x -= 1
        return final
print reverse('abcdefghi')

Thanks in advance to anyone that takes the time to respond!



It looks like the indentations did not come through when the code was pasted in. Is there a more effective way to paste code into forum postings here so that the indentations show?


There is a button above the post area that looks like </> and you can also surround your code with


# Code Here
```# End


That did it - thanks! I bestow up thee a :doughnut:


I have one for ya,

def reverse(text):
    return ''.join((text[-(index + 1)] for index, letter in enumerate(text)))

This works because we access the index with enumerate then we add one to it and flip it to a negative, this works backwards on the list.

Also, I use the fact that join takes any item it can iterate over and I pass it a generator which can be iterated over.


Congrats !

But I didn't understand this part:
x = int(len(temp)) - 1

I just put x = len(temp) - 1

And also I didn't use the second for loop.


Thanks Carlos! The "int" was not required, I removed it and it still works just fine. How did you iterate through the 'temp' list of the characters without a second loop? I used to second loop to iterate thought the "temp" list and create a final "string" value of the original text printed in reverse.

Here is the updated code with the "int" removed

def reverse(text):
    temp = []
    final = ''
    for c in text:
    for i in temp:
        x = (len(temp)) - 1
        while x >= 0:
            final += temp[x]
            print x
            x -= 1
        return final
print reverse('abcdefghi')


Thanks Zeziba! All of that in a single line looks much better :sunglasses:

I haven't yet worked with the join or enumerate operators(is this terminology is corect?). I'm hoping these will be referenced to in the more advanced portions of the Python course later on.


You can save the for loop in
for c in text:
You can just write temp = list(text)

def reverse(text):
    new_text = []
    for x in text:
    return ''.join(new_text)

this works too.

the for loops keep inserting letters into the first position of the list. as we start from the front of the list to the back, the last letter will end up in front. Last In First Out.

then the last list returns the list in a string format.


actually this code is working but i don't know why it gives an error..

print mystr = "ABCD"
reverse = ""
length = len(mystr)
while length > 0:
----reverse += mystr[length-1]
----length -= 1
print reverse


this is what i used XD ^¬^ just sharing

def reverse(text):
    x = len(text) - 1
    new_text = ""
    for i in text:   
        i = text[x]
        new_text = new_text + i 
        x = x - 1
    return new_text


Hey why did u use -1 in the second line of your code , I don't understand. It's because comp starts counting from 0.

x = len(text)** - 1**

Thanks for answer :smiley:



Do some reasearch on your own,

Do the following in an IDE or interpreture

a = [1, 2, 3, 4, 5, 6]

except IndexError:

I think this should help you along nicely. You have to take into account what len() returns vs. how computers count.

If you still need clarification after this tiny exercise I will do my best to assist.


Thanks , it helped me a lot, appreciate it. You are doing great job here! :smile:


def reverse(text):
l = len(text)
temp = ''
for n in range(l-1,-1,-1):
temp = temp + text[n]
return temp

This code worked for me


def reverse(text):
result = ""
for i in text:
result = i + result
return result


You didn't use any functions. Try embedding this code in a function.


I've just made your code smaller by removing the outer 'for' loop which I thought was irrelevant. This code too works fine. Thought I'd let you know :smile:
def reverse(text):
final = ''
x = len(temp) - 1
while x >= 0:
final += temp[x]
print x
x -= 1
return final


Wow.... I looked at that and thought it cant be that simple..... it works and the code is easier to follow