Reverse Help


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096


I'm getting the following:
Oops, try again. Your function fails on reverse("Python!"). It returns "[]" when it should return "!nohtyP".

  • So currently my understanding is I've created my function reverse
  • I've created two array variables string and reverseString
  • My first for loop is to put the characters of the input text in to the array "string"
  • My second for loop is to then take the characters from string and put them in reverse order in to "reverseString".


def reverse(text):
    string = []
    reverseString = []
    for x in text:
        string.append(x)
        length = len(string)
        for y in range (0, length, -1):
            reverseString.append(string[y])
    return reverseString


#2

I'd suggest you to do them separately,in different for loops.

1.First append all characters to string list first for loop
2.Then do appending work in other for loop.

Back to your code..
as at first looping when your first for loop runs ,string will have just one character and when your second for loop will start working it will see this..
range(0,1,-1) which is nothing but an empty list and thus your loop does not run and throws an error.


#3

Hi GreenTreeMee,

Ah okay, I see what you're saying with the for loops, looks like I got a bit enthusiastic with the indentation.

I've now put in the following:

def reverse(text):
    string = []
    reverseString = []
    for x in text:
        string.append(x)
        
    length = len(string)
    for y in range (0, length, -1):
        reverseString.append(string[y])
    return reverseString

But I'm still getting the same.


#4

The first problem is range..
as you're going backwards so upper first parameter should be length - 1?
also the second parameter will change!

@tagrunner63517
There is one more problem we will face. (process going to be longer as we're relying on different data type (list) as our input data type is string )

We will have to use join method on our reverseString ( don't forget that Its a List) to convert it to string and return the reversed string.
do reply what're you getting after doing that?


#5

Still not sure why the below didn't work:

def reverse(text):
   
    string = []
    reverseString = ""
    length = len(text) -1

    for x in range (0, length, -1):
        string.append(text[x])
       
    for y in string:
       reverseString = reverseString + y
    return reverseString

#6

hi @tagrunner63517

this line have one issue!

You want to add characters of text backwards then It should be start from
length of text ,also as the last index would be -1 (as we going backwards),the step size is quite right.

make changes in range and do reply what you get?
(also edit your final working code so other just not copy-paste and use it)


#7

You can try this simpler version:

def reverse(text):
    string = ""
    for i in text:
        string = i + string
    return string

So if the text is "goal", the first iteration of the for loop sets string to be "g", and the second becomes "o + g", thus string becomes "og", the third does "a + og = aog", and the fourth "l + aog = laog"!


#8

Yeah that would've been way easie :tired_face:


#9

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