7. reverse


#1


Hi there

I wasnt able to write this code myself... :disappointed_relieved: Had to look it up at the QA. Now I dont understand what i = len(text) - 1 and i = i -1 is standing for? which one is reversing the letters and what is the other one doing?


def reverse(text):
    letters = [] 
    i = len(text) -1  #reverse letters?
    while i >= 0:
        letters.append(text[i])
        i = i -1 #?
    return "".join(letters)#put together the letters without commas ect
print reverse("hello")


#2

if you have a string:

"world"

you can access the letters by index:

"world"[4]

lets look at i:

i = len(text) - 1

i will give you the index of the last letter (for example, world is 5 letters long, so the length is obviously 5. the letters are zero indexed based (counting starts at zero), so the last letter is at index 4 (that explains the -1)

then you append these letters to the letters array. Then you decrease i so you will get to the previous letter, this way the string is slowly reversed.

You can always add print statements to see what a program is doing, i recommend you to do this


#3

ok, makes sense... thanks!


#4

Did you add some print statements so you could see it? That generally helps a lot


#5

I did. But I couldnt manage to print the letters list after appending text. (before reversing) is that possible somehow?


#6

you can add print statements in the loop if you like:

def reverse(text):
    letters = [] 
    i = len(text) -1  #reverse letters?
    while i >= 0:
        print letters, i, text[i]
        letters.append(text[i])
        i = i -1 #?
    return "".join(letters)#put together the letters without commas ect
print reverse("hello")

I don't see why you can't do this, in fact, i would everyone highly recommend to do this, so you can see what is actually happening


#8

I solved it like this:

    def reverse(text):
        new = []
        for n in range(len(text)):
            new.append(str(text)[-1-n])
        print(''.join(new))
    reverse('"Python!"')

It wokrs good in IDLE but here there's an error.


#9

"If" works as well

  def reverze(text):
        letter = []
        i = len(text)
        if i > 0:
            letter.append(text[i])
            i = i - 1
        return "".join(letter)

#10

it works fine in the exercise as well, except instead of printing you should return the result. If you do want see the returned result, you can call print on the function call:

print reverse('"Python!"')

#11

Why [-1-n]. I see it Works, but as a good newbie, I am not able to understand it


#12

The index of the last symbol in string is '-1'
1 step: 'n' is '0' , symbol index is '-1-0=-1' (1st symbol from the end)
2 step: 'n' is '1' , symbol index is '-1-1=-2' (2nd symbol from the end)
3 step: 'n' is '0' , symbol index is '-1-2=-3' (3rd symbol from the end)
and so on.
Do I explain in a clear way?


#13

Crystal clear pal. Thanks!!!!!!!!!


#14

Can someone help me understand why this range function works here?

'Len(text)-1' means to start at the "last" index or right-most, correct?
I don't understand the stop and step values. I'm stepping one place to the left each time?

def reverse(text):
s = ''
for i in range(len(text)-1, -1, -1):
s += text[i]
return s


#15

This helped me understand using a '-1' step value in range, thank you.

Could you explain why the correct stop value is also -1? Didn't we start at that index?


#16

take a look at some general syntax:

for i in range(start, stop, step)

we start at len(text)-1, then we go till numbers which are greater then -1 (0,1,2 and so on) in steps of -1.

as stop we need -1, because -1 itself is not included. If we would have had stop 0, zero (the first letter of the string) wouldn't have been included

This is not some kind of topic you can dump all questions about it, if you have more questions just create your own topic instead of replying to this topic