Reverse


#1



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


So I'm stuck at reverse, any suggestions why this error message comes up?

Oops, try again. Your code looks a bit off--it threw a "string index out of range" error. Check the Hint if you need help!


def reverse(text):
    new_word = ""
    while(len(text) > 0):
        new_word = new_word + text[len(text)]
        text.remove(text[len(text)])
    return new_word


#2

A good start would be to print out the indexes that you try to use and compare to what the last valid index is


#3

Probably a dumb reply, but if you mean it like this it doesn't change much :sweat_smile:

def reverse(text):
    new_word = ""
    while(len(text) > 0):
        print text[len(text)]
        new_word = new_word + text[len(text)]
        text.remove(text[len(text)])
    return new_word

#4

No I mean the index, not the value at that index.

So you would consider the data you have and say "these indexes are valid" (that would be a range, counting up from zero)

And then you would print out the indexes that you really do use.

For example, "hello" has 5 elements, the first index is 0 and the last one is 4. Any indexes in that range is ok to use. If I had a index out of range error then I could print out the indexes I was using and perhaps I was using something like 10 and I'd see that - oh, that's not within [0, 4] so I would have to think about what I meant to do and how I should carry it out using only valid operations


#5

Ohh ok I get it now, found the issue, got a new one tho, is there an alternative to .remove() for strings?

My new code is this

def reverse(text):
    new_word = ""
    true_length = len(text) - 1
    while(len(text) > 0):
        new_word = new_word + text[true_length]
        text.remove(text[true_length])
    return new_word

My new error shows that I can't use remove tho
---->
Oops, try again. Your code looks a bit off--it threw a "'str' object has no attribute 'remove'" error. Check the Hint if you need help!


#6

Strings cannot be changed. (If you want the behaviour that list has, then use a list)

How does removing characters help in reversing something? Have you thought that through?


#7

My idea was to get the length of a string, then call the last char of the string and put it in my new string, at the end of the loop I would remove the last char of my old string so I can call the "new" last char of my old string


#8

What if you just went to next character without removing the other one? It still being there doesn't prevent access to the one that comes before

Also, you cannot call a character. (What does call mean, and does a character support that behaviour?)


#9

Yeah ... by calling I meant to use the index of a string so word = Hello -> word[1] = e -> I "called" e
Anyway my code to solve it ->

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

Thanks for help :smiley:


#10

That doesn't make sense. If the length is 0, then there is nothing left to do. Your condition stops when the length is -1, which isn't possible.

It's also not a length, because the length is unchanged. It's a position (so it's a misinformative name)


#11

I just solved this.

def reverse(text):
    new_text = '' #defines an empty string for new string
    for i in range(1,len(text)+1): #added 1 in length to avoid lenght overflow problem
        new_text += text[-i] #It will add  letters in reverse order
    return new_text

#12

In retroperspective the name maybe wasn't my best choice, anyway; the code does drop below zero since "true_length -= 1" runs till the while loop stops(at -1)


#13

Could you make i start at the last index and loop until the first instead? (Is that something that the range function supports?)

https://docs.python.org/2/library/functions.html#range


#15

like this?

def reverse(text):
    new_text = ''
    for i in range(-1,-(len(text)+1),-1):
        new_text += text[i]
    return new_text

#16

Indices are positive though, I'd much rather use 3 2 1 0 than -1 -2 -3 -4


#17

Done:

def reverse(text):
    new_text = ''
    for i in range((len(text)-1),-1,-1):
        new_text += text[i]
    return new_text

Is it what you saying to do ?
why we should prefer positive index over negative ones?Just curious :slight_smile:


#18

Only because it plays better with human expectations.
The list points at a location in memory. If you add 0 to that position, then you'll still be at the start - at the first element. If you add 1, then you're at the second element. If you subtract 1 then you're not in the list's memory any more. Languages that don't translate negative indexes to start counting from the back will either crash or overwrite something else (unless it tries to overwrite something it isn't allowed to write to by the OS, in which case it crashes anyway)

If both negative and positive indexes can be used, then positive ones win out because the negative ones aren't real positions. They're nice when you're actually counting from the back, but when referring to positions, then .. do that, and not something that translates into that, it's an extra step


#19

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