Reverse


#1

I’ve entered code that seems to work, however I am still getting an error.

My code:

def reverse(text):
  x = len(text) - 1
  for i in text:
    print text[x],
    x -= 1
    if x < 0:
      break

Error:
Your function fails on reverse(“Python!”). It returns “None” when it should return “!nohtyP”.

In the terminal it does print back c b a ! n o h t y P.
Would it be the spaces that are causing the problem?


#2

printing and the result of a function are entirely separate things

(Check out the buttons in the post editor, one of them is for pre-formatted text such as code. imaginary internet points for effort though)


#3

Thanks…but that still doesn’t help me understand why I’m getting the error.


#4

But it does.
You say you print the result. But printing isn’t what’s asked for.


#5

To give you an idea of how the exercise interacts with your code:

result = reverse('Python!')
print "reverse('Python!') -> {!r}".format(result)

You’ll see the reversed text printed (because your function prints), but the print in this code will say the result is None, because it pays no attention to what’s printed.

reverse('Python!') -> None

What you’d want to see is the reversed string being returned:

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

Note for example that your function can’t be used twice to obtain the original text

reverse(reverse('Python!'))  # should return 'Python!'

#6

Fixed. But now it’s saying:

“Your function fails on reverse(“Python!”). It returns “!” when it should return “!nohtyP”.”

def reverse(text):
  x = len(text) - 1
  for i in text:
    return (text[x])
    x -= 1
    if x < 0:
      break

#7

How many times does your function return?
What happens in a function after returning from it?
At the time that your function returns, how much of the result has been completed?

What’s the return value of this function?

def f():
    return 1
    return 2
    return 3

Additionally, if you execute the break statement, what then will get returned?


#8

I think I know what you’re getting at and where I messed up. I added a while loop to the code, but am still getting the same error.

def reverse(text):
  x = len(text) - 1
  while x >= 0:
    for i in text:
      return (text[x])
      x -= 1
    else:
      if x < 0:
        break

#9

Think back to what actions you should be carrying out. Does that really match having two loops?
What does for-else do?

What I’m getting at is, don’t use anything you don’t know what it does. And, refer to what actions should be happening (forget about code while deciding that)

You’re the one describing what the computer should do, you’re the one aligning everything to do what you want. If you get the slightest detail wrong, the outcome will be wrong (unless by accident, but that’s no better)


#10

To answer myself:

The return statement exits the current function, optionally with a value being sent to the caller.

That means nothing can be done after you’ve returned. That’s it, you’re done, that’s what return says. You have to have finished figuring out the result when you return, because there is no “later”

Since you can only return once, a return statement is usually not found inside a loop, that defeats the purpose of a loop (repetition) because it wouldn’t continue

And, if you’re iterating over a string, then that already says how many iterations there will be. There probably shouldn’t also be a break in that loop.


#11

I started over and really thought through it. I want it to grab the character from the back so I setup a string that would find the total length of the “text”. I know that I need to have it iterate through the whatever is dumped in so I setup a for loop to iterate through the “text”. Now the problem is where to store the new info. I setup a string outside of the for loop equal to a blank string. The for loop would then append that blank string. End it with a return and it was good to go.

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

#12

Yeah. Better. You’re not guessing, you’re writing what you want.


#13

Two things I’d still want to poke at.
1) You’re ignoring i from the for-loop. A while-loop would be more suitable.
2) Strings are immutable. Can’t be changed. That means you’re not adding characters, you’re creating a whole new string with an additional character. (A lot of copying for large text). The way around this is to place the characters in a list which does support adding, and then create a string when all the characters are known.

I’d still accept it if that was for me to decide, it’s fine for small input, making it look nice is about experience and knowing more ways to express things, and it’s an absolutely silly thing to implement in python which already facilitates this kind of thing:

def reverse(text):
    return text[::-1]

(Which of course isn’t an appropriate solution, since it avoids implementing it)
It gets difficult to say what should be allowed or not, and the nicer the code is supposed to be, the more it’ll be like cheating

Oh yeah and, some python implementations will cheat here, and actually will add to the string rather than making a copy. They can do this because they see that there are no other references to the string, nobody could possibly tell the difference (aside from the speed difference)


#14

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