Reverse


#1

Hi there!

Could anybody have a look at my code and help to understand where is the problem?



This is that I am getting after submitting my code:
"Your function fails on reverse("Python!"). It returns "" when it should return "!nohtyP"."


def reverse(text):
    reversed_text=""
    for i in range (len(text),0):
        reversed_text = reversed_text + i
    return reversed_text

Thanks!


#2

The first thing that you need is a variable that counts each letter in the string. Basically, what you have to do is break the string into individual letters. You would iterate through the text and add one to the count each time there is a letter in text. Additionally, reversed_text needs to be an empty array rather than an empty string. You want to use append and this is where it gets a little complicated. While count isn't 0, you want to append text[count-1:count] to reversed_text. That would look like:

    while count != 0:
        reversed_text.append(text[count-1:count])

Then inside the while loop still, you would want to use count -= 1.
Outside of the loop but inside of the function, you want reversed_text to be set to ''.join(reversed_text) and then you want to return reversed_text


#3

Thank you for the explanation! Everything works now!


#4

I'm glad! Good job. :slight_smile:


#5

Very n00b coding. You can do the reversed_text with empty strings also. Besides, wasn't it illegal to use text[count-1:count] type format (rule no. 1). Below is a working code.

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

#6

:slight_smile:

I honestly just looked at my code that I completed a while back to help with this one, and mine ended up being a lot longer. Great code. Thank you for pointing this out to me, I do like seeing a more efficient way of doing things, even though efficiency isn't what's really needed here. My complete code is

def reverse(text):
    count = 0
    result = []
    for x in text:
        count = count+1
    while count != 0:
        result.append(text[count-1:count])
        count = count -1
    result = ''.join(result)
    return result

and I wasn't even thinking of a more simple, elegant way. I did this section when I didn't know the most about coding and had no experience. I will say that it's okay to have different code. Obviously fewer lines is preferable. But I got working code, which was my goal. Yours is better though, so kudos.


#7

Can someone please explain this code? I understand it as far as the start of the for loop.

reverse_string += text[len(text)-i-1]

What is the code doing here? EDIT: never mind, I figured it out. The -1 was throwing me off until I realized that was necessary because of how indexing works.

I also saw another solution for this:

def reverse(text):
    rev = ""
    for char in text:
        rev = char + rev
    return rev

Why does this work? It doesn't seem to specify that the loop should go through the characters in reverse. But it works.


#8

Suppose, text = "Balls". rev is an empty string.

Iteration 1: when char = 'B'. RHS (Right Hand Side) = char + rev = 'B' + "" = "B" and it gets stored in the empty string rev = LHS (Left Hand Side)

Iteration 2: when char = 'a', RHS = char + rev = 'a' + "B" = "aB" = rev = LHS

Iteration 3: when char = 'l', RHS = char + rev = 'l' + "aB" = "laB" = rev = LHS

Iteration 4: whenchar = 'l', RHS = char + rev = 'l' + "laB" = "llaB" = rev = LHS

Iteration 5: when char = 's', RHS = char + rev = 'l' + "llaB" = "sllaB" = rev = LHS

It works because the empty string was placed on the right side of char, i.e.rev = char + rev
Had it been rev += char or rev = rev + char, It wouldn't have worked.

If you are having difficulty in understanding how the instructions under your for loop are being executed, it's a good idea to use a print command to see how each iterations are modifying your variable. Like:

def reverse(text):
    rev = ""
    for char in text:
        rev = char + rev
        print rev         # To check what's happening to variable rev
    return rev

#9

Thanks, that makes perfect sense. Very helpful answer.


#10

Hi,

I didn't get how -i-1 works in [len(text)-i-1]


#11

Suppose text = "arbok". The length of the string len(text) = 5.
Now, for i in range(len(text)) will have i = 0, 1, 2, 3, 4.
So, len(text) - i - 1 will have
Turn 1: when i = 0, 5 - 0 - 1 = 4. and text[4] = 'k'
Turn 2: when i = 1, 5 - 1 - 1 = 3 and text[3] = 'o'.
...
...
Turn 5: when i = 4, 5 - 4 - 1 = 0 and text[0] = 'a'
Setting the value of the reversed_string as "kobra".

You can check with the editor what happens when you set text[len(text)-i-1] and text[len(text)-i] by a print command within the for loop. You will see that when i = 0 for the first iteration, text[len(text)-i] yields text[5] and there is no character on index 5, resulting in an error. Besides, I think that the solution siorac came up with is better than mine.


#12

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