Unique REVERSE solution trying to make it work



Ok, so I've been trying to use the pop() method to solve this problem. And the end result does work on the console but for some reason I can't pass the section because apparently it just returns "None".

I can't figure out why this solution wouldn't work. Yes, it's more convoluted than using the range step method but I think this should work.

def reverse(text):
    product= ""
    text = list(text)
    for i in range(len(text)):
        for letter in text.pop():
            product +=letter
        if len(product) == len(producter):
            print str(product)


Only these lines are needed...

reverse_text = ""
text_iterable = text
text = list(text)

Now we are free to destroy text. Iterate over the copy, and pop from the original. This ensures a full count of reversed characters making the last conditional also unnecessary.


>>> text = "Python!"
>>> reverse_text =""
>>> iter_text = text
>>> text = list(text)
>>> for i in iter_text:
	reverse_text += text.pop()

>>> reverse_text

We don't make any special use of i other than to use it as a dummy variable that serves the iteration process.


That is just what your function does, I'm afraid. In line 10 you print out the finished string, but you should really return it to whatever called the function.

Hope that helps

Tim F


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