Reverse oversimplified?


#1

Some questions have too many answers and I've spent a few hours trying to simplify this problem and this is the best I can do. Is there a flaw in this?


def reverse(text):
    ntext = ""
    for letter in text:
        ntext = letter + ntext
    return ntext


#2

Hi @awittyjar,

Your solution is fine. However, it would be more efficient to store the characters in a list within the for loop, rather than to concatenate them to a string, and then to use the join method, after the loop, to combine the characters in the list to form the resulting string.

The inefficiency is in the repeated concatenation. Each concatenation creates an entirely new string.


#3

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