# Hi, I need help on 7/15

#1

def reverse(text):
container = []
a = 0
reversal = []
b = len(text) - 1
c = 0
for t in text:
t = container[a]
a += 1
while b >= 0:
container[b] = reversal[c]
c += 1
b -= 1
return “”.join(reversal)

print reverse(“abcd”)

I think this should work but it does not. Can anyone explain to me why it does not? Thank you.

#2

Perhaps explaining your logic will help sort matters out?

#3

I iterate through each letter t in text with a for loop and add each letter to container, starting with index a=0, then increment a by +1. Then, I use a while loop to go backwards through the container list while adding each letter in a forward direction to the reversal list. For example, with string text = “abcd”, since b = len(text)-1, the part under the while loop starts at container[3] = reversal[0] and increments downwards a letter/index with container and upwards with reversal. (The text “abcd” has already been added into container using the previous for loop). Finally, I use .join to print out the characters collected in the reversal list together as a string. When I copy and pasted my code to the forums it got rid of the indents, but the two lines after “for t in text:” and then the three lines after “while b >= 0:” should be indented by four spaces, while everything under “def reverse(text):” should be indented by two spaces except for the print statement.

#4

Please explain how `container[a]` even exists.

#5

Traceback (most recent call last):
File “python”, line 16, in
File “python”, line 8, in reverse
IndexError: list index out of range

This is the error message I get.

#6

Refer to earlier question…

#7

In the second line of code I make an empty list called container. In the third line of code, I set variable a equal to zero. This is all done before the for loop.

#8

Does not explain logic that follows. Please continue…

Step back and do a rethink. It’s your ball. Make it bounce.

#9

Alright, I don’t get it but thanks for the encouragement, I’ll try to figure it out.

#10

I kinda understand your logic, but you are overcomplicating a simple problem mate, try to think simple.
For instance, what do you need to do for a string, list or whatever to be backwards? Dont you need to build the string in a backwards order, adding each item the loop iterates before the previous one?

I made it through with 5 lines, dont know if its possible do solve it with less, (I would defnitely wanna know). Im sure you can too!

GL

#11

Thanks. Yeah I figured it out. I didn’t realize that you could call each variable of the string separately without putting it into a list.

#12

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