Question about 7. reverse


#1

hello, i finished this part with this code

  def reverse(text):
        b = ""
        for a in range(1,len(text)+1):
            c = len(text) - a
            b += text[c]
            a -= 1
        return b

    text = "ABCD"
    print reverse(text)

but i think i don`t realy understand how it works !! .. can someone explain it to me :sweat:


#2

I wouldn't write this program this way, but lets say i do. so we have a string text (text = "ABCD"), now we want to reverse this. to do this, we make a new string (b = ""), then we are going to loop over the string we have, we do this here:

for a in range(1,len(text)+1):

but we actually want the last letter, so we are going to reverse it:

c = len(text) - a

if you would put this on the line below:

print c

you would see 3,2,1,0. 3 is the last letter (you can see this by doing text[3] somewhere at the bottom of your code. so, then on this line:

b += text[c]

you add the letter at the index c to string b, then you reduce so to get the a letter earlier. I find this solution a bit confusing


#3

@stetim94 would you be so kind to tell us, how you would write this code? I am curious how advanced programmer would resolve this task.


#4

Advance? do not exaggerate, i would do the following:

def reverse(text):
    b = ""
    for a in range(len(text),0,-1):
        b += text[a - 1]
    return b

text = "ABCD"
print reverse(text)

i reduced the number of lines you write by a few. Does this solution make more sense?


#5

Well, it's a great idea to set -1 step in range
I admit, when I look at it, it looks really simple, but look can be deceiving :smile:
Thank you for sharing with us.


#6

Yea, and that is the tricky bit. Coming up with the good solution is tricky, then writing it is relative easy.

You have to a lot of sort of this kind of challenges, to get handy with this kind of thing


#7

Thanks very much ^^ @stetim94


#9

I do not understand the b += text[a-1] part, could you please clarify?

I understand that we are trying to put the characters we loop through from the string backwards into b, but why are we adding text[a-1], rather than just text[a]?


#10

well, agree that the string is zero indexed based (counting starts at zero)? text[0] = A, text[1] = B etcetera.

The problem is that range is four, so if you where to modify the html code:

def reverse(text):
    b = ""
    for a in range(len(text),0,-1):
        print a
        b += text[a - 1]
    return b

text = "ABCD"
print reverse(text)

you will see 4,3,2,1 printed, while this should be 3,2,1,0 to access the right letters in the string. so that is why i do a-1, it might make more sense to adjust the loop:

def reverse(text):
    b = ""
    for a in range(len(text)-1,-1,-1):
        b += text[a]
    return b

text = "ABCD"
print reverse(text)

#11

Many thanks, I actually think the original code makes more sense now. Thank you.


#12

If you have my latest code of the previous answers, and you add a print statement:

def reverse(text):
    b = ""
    for a in range(len(text)-1,-1,-1):
        print a
        b += text[a]
    return b

text = "ABCD"
print reverse(text)

you will see 3,2,1,0 which perfectly matches our letters in our string. range(text) is 4, minus one makes it three 3. which is our last letter.


#13

I don't know why you made it so complicated but here's my code:

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

It just adds every letter in text to the front of b


#14

danm, that is clever. I guess very things can beat that