7 - 'Reverse' Solution Critique


#1

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:
        temp.append(c)
    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!

-Mitch


#2

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?


#3

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

```python

# Code Here
```# End

#4

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


#5

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.


#6

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.


#7

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:
        temp.append(c)
    for i in temp:
        x = (len(temp)) - 1
        while x >= 0:
            final += temp[x]
            print x
            x -= 1
        return final
    
print reverse('abcdefghi')

#8

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.


#9

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


#10
def reverse(text):
    new_text = []
    for x in text:
        new_text.insert(0,x)
    
    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.


#11

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


#12

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

#13

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:


#14

@objectslayer68527

Do some reasearch on your own,

Do the following in an IDE or interpreture

a = [1, 2, 3, 4, 5, 6]
print(len(a))

try:
    a[len(a)]
except IndexError:
    print('Oppps')

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.


#15

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


#16

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


#17

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


#18

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


#19

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 = ''
temp=list(text)
x = len(temp) - 1
while x >= 0:
final += temp[x]
print x
x -= 1
return final


#20

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