Reverse


#1



def reverse(text):
    total=[]
    for i in text:
        for i in range(len(text),-1,-1):
            total.append[i]
        return total


what is wrong in this? it shows error object has no attribute


#2

append() is a method, it requires parentheses

you have two loops, why? A single loop should be enough


#3

okay but this prints the length or the position of the digits in reverse order.
how to write the correct code using the above loop??
and also how to remove the [] brackets in output
please help


#4

def reverse(text):
    total=""
    for i in range(len(text)-1,-1,-1):
        total = total + (text[i])
    return total
  • changed total to be a string
  • removed the redundant first for loop
  • have the for loop start at len(text)-1 instead of len(text) as the last index is one lower then the length
  • removed append (which does not work with strings) and used '+' to add the characters to total

removed my own solution to not confuse too much as mentioned by stetim


#5

please post an updated version of your code. You could use .join() to join the list into a string

@terarunner78986, why deviate so much from the code the learner has? That is confusing, and given strings are immutable in python, its not very memory friendly code


#6

Hey stetim,

I didn't think my code was that far away from systemjumper's. But now that you mention the .join function I see how this would work fine using the list approach. As I am also learning, I just wanted to show the way I solved this and hoped it could be of any help. Regarding "memory friendly": I guess we're not supposed to know such stuff at this point, at least I have no idea what kind of code is more or less efficient.

But as you mentioned that strings are immutable, I got a question: If I understand correctly it means that you can't alter a string once created. In that case, why exactly can I do something like:

s = "Hi"
s += " Ted"

Isn't that changing the state of s? Thanks in advance for any clarification.


#7

it does, but a new string is created with the joined result. lets say each character is one byte, then Hi is 2 bytes and " Ted" is 4 bytes (space is a byte as well)

this means, in total you have 8 bytes (2 for old string, and 6 for new string)

in a list, you can just add to list, so you would have 6 bytes total

now, for a program this small, this isn't very troubling. However, if your program becomes bigger, this becomes a problem.

And in your reverse approach, you don't create a new string once, but many times


#8

def reverse(text):
total=[]
for i range(len(text)-1,-1,-1):
total[i]
a=""+join(total)
return a

okay if i use join..still not working?


#9

thanks it did work.
but i need to understand the join method as well when i use it in my code mentioned below

def reverse(text):
total=[]
for i range(len(text)-1,-1,-1):
total[i]
a=""+join(total)
return a


#10

As you can see here, you should try

a="".join(total)

(instead of using the + symbol)

.. or directly return "".join(total) if you want it to be a little shorter


#12

def reverse(text):
    total=[]
    for i in range(len(text)-1,-1,-1):
        total[i]
        return "".join(total)

returns with error list index out of range
what should i put in the for loop to make this run?
please help


#13

what are you doing here:

total[i]

i would append to total instead

a function ends the moment a return keyword is reaches, so you want to place the return keyword outside the loop


#14

def reverse(text):
total=[]
for i in range(len(text)-1,-1,-1):
total.append(text[i])
return "".join(total)

thanks i get it now...and worked..earlier i was trying to append i but i had to use text[i] instead
you're awesome.


#15

I'm the begginer too,
Why is there 3 times -1?

for e in range(len(texte)-1,-1,-1):
I do understand first two (because string goes from 0 to end) but third one in defining range?
Thanks!


#16

general syntax:

range(start, stop, step)

well, length start counting at one, where as list are zero indexed based. So you need to subtract one to compensate for this difference

range does not include its stop value, so you need -1, using 0 would mean the the first letter of your word doesn't get appended

by default, range has steps of +1, but given we are going from higher to lower value we need to specify that we want negative steps/strides


#17

Oh, I foget for step.
Thanks!


#18

Oops, try again. Your function fails on reverse("Python!"). It returns "!" when it should return "!nohtyP".

m getting this error with this code!!


#19

then the indention of the code is not correct, if you need help, please make a new topic. This will give you a template which will ask for all information we need to help you


#20

your indentation must be wrong
post your code here


#21

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