7. Reverse. I don't comprehend the answers for reverse


#1

I understand that I’m overthinking it but I’ve browsed the forums hoping that seeing others solutions to the problem would help clarify how someone would get this solution. I don’t understand codecademy’s answer to it or anyone else either.

Codecademy’s answer:

def reverse(text):
    word = ""
    l = len(text) - 1
    while l >= 0:
        word = word + text[l]
        l -= 1
    return word

It may be my understanding of indexes and/or strings as well as the “L -= 1” that is throwing me off but I’m going to have a career in coding and I’m going through any trial I can in order to get there. I’m taking classes but my teachers are learning with me and I want to understand and move on with this problem so I can get to my never-ending goal of learning and helping others through coding.


#2

Such a shame you got the answer, it would have been more fun to work on a solution. All be it with help here on the forum. You missed a good learning opportunity here.

well, a good start would be to add a function call:

print reverse("hello world")

now we can insert print statements as well in the function, to see what your program is up to

In short, we loop over the string in reverse. We use integers + while loop for this, this integers match the indexes of the string.

insert some print statement, to see values like l or text[l] in the loop.

Let me know if you have more questions


#3

Doing now and thank you much for the reply!


#4

Okay I believe I understand this more now. The loop in the [while] is what was throwing me off more. I was glazing over the fact that it is a loop and not thinking of the implications of it. I’m going to continue working it out in my head and try to find others to ask about this. I love learning about code but in both of my coding classes and my previous one the other students just play around on the computers and don’t really do anything till they have to so whenever I bring up questions they react as if they are getting in trouble or they just say “I don’t know, I didn’t get it either”. My teachers try to help but I’m further in the course than they are and it’s hard to achieve that help with that being said.


#5

I’m also wanting to reset my progress and try it all again without using the “Give solution” button but we have deadlines for completing the code in a week and I’ve been working hard on it every day and I’m still around 60%.


#6

then it becomes really difficult. Sure you have no more questions for me?

you can retake the course after you are done and the deadline has passed


#7
def reverse(text):
    word = ""
    l = len(text) - 1
    while l >= 0:
        word = word + text[l]
        l -= 1
    return word

Just a couple of comments about the code above.

  1. Variable names: When using single letter variables avoid using letters that look like numbers. l looks very much like a 1
  2. Compound assignment operators: word = word + text[l] can be written as, word += text[l]

Your code simplified:

>>> def reverse(text):
    word = ""
    n = len(text)
    while n > 0:
        n -= 1
        word += text[n]
    return word

>>> reverse("Python!")
'!nohtyP'
>>> 

#8

Thank you for clearing more of this up. I was at lunch so I wasn’t able to reply.


#9

Thank you guys for the help! I’m going to continue and then go through this all again after this class deadline.


#11

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