Reverse - Naive solution?


#1

I’m not only beginning python but also programming.
So so far i solve the problems but i’m trying to question the way i do it.
But it’s not easy without the experience… :slight_smile:

So i’d like to start to ask comments about the way i solve problems.
Is it ok, can it be optimised, is it just “naive” and not the way a progammmer would do, is it “pythonic” :slight_smile:

So for Reverse, this is my solution:

def reverse(text):
    txet = ""
    for i in range(len(text)):
        txet = txet + text[len(text)-(1+i)]
    print txet
    return txet

Thanks in advance for your time :slight_smile:

[edit] I found some answers in this very interesting topic about the reverse problem:


#3

a good optimization would be to make txet a list, given lists are mutable and strings are not

then use .append() to add/append to list, then use .join() to the list into a string after the loop before returning it


#4

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