Reverse


#1



Reverse


Why am I getting "string index out of range" error?


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


#2

Because of the way you are trying to slice your string, make it simple.

Use the list slicing method for reversing a string which is [::1]

Now because of that you would'nt need your for loop, so now you'll be left with a working and more efficient piece of code,

def reverse(text):
    s = ''
    s += text[::-1]
    return s

There is no need to use the len() orrange() built in function


#3

I believe we are told to not use this method in the instructions.


#4

Oh that's range cause I passed this exercise with that method. :confused:


#5

Yes I just changed the code to this:

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

Alternatively I could simplify if this way:

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


#6

passed doesn't mean you followed instructions. The SCT is not exhaustive, and usually lenient. The challenge is to do it without using built in tools.


#7

I see. I modified my code after passing the lesson though,

def reverse(s):
    return  "".join(reversed(s))

#8

That is still using a built-in.

Assume that range() is also a built-in.

We need to think this through in fundamental terms, the steps rudimentary as behold. Use as few building blocks as possible would be the complete challenge, one could see it.


#9

Toss aside Python; pseudo code wiith no built in functionality. Then coding begins to make sense.


#10

You can loop through the string in reverse, as I did. Here is my code:

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

#11

We should use no built in functions, range included. That way we are forced to think through the algorithm using the very simple tools we are left with.

which code we've seen a thousand times. It is not the point of the exercise to find the coolest solution. Just the one with the least bells and whistles.


#13

You may not use reversed or [::-1] to help you with this.

This is the only condition I found in that exercise. Seems as if we are allowed to use "range" function.


#14

That clears that up. Still, the challenge of working without it is on the table. Over to you...


#15

I don't feel like wasting more of my time on a single exercise when it's possible to finish it up with the minimum resources. After all, I believe Python wasn't made for low-level programming without the use of it's inbuilt functions. By the way, challenge accepted :slight_smile:. I hope we are allowed to use len() function

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

#16

At this stage of learning, I think we need all the low level programming we can get. When nothing is abstracted away, we can see in plain sight all the processes at work. Good job on the challenge!


#18