Reverse


#1



In the Reverse Exercise, ran my code and got

Does your reverse function take exactly one argument (a string)? Your code threw a "'builtin_function_or_method' object has no attribute 'getitem'" error.

I believe it reads: a function reverse, is defined by:
an empty list, into which we append letters starting from the letter in the place of the length of the letter-1 (because the index starts at 0), and we go down by 1 step, and stop at 0.
then I returned the the items of the list reversed_word, joined by nothing, as indicated by an empty string.
I can't tell where I have gone wrong!
I do have some trouble with the indentation of return on exercises. If someone could explain to me why sometimes return goes an indent further, inside the for loop block, that would also be soooo appreciated!!
Thank you so much!

def reverse(text):
    reversed_word= []
    for i in range[len(text)-1,-1,-1]:
        reversed_word.append(text[i])
    return ''.join(reversed_word)


#2

Functions use parentheses not brackets, check the range function


#3

!!!
such a silly mistake... THANK YOU SO MUCH!
:'))))


#4

Hey I've been having a tough time with this exercise, would someone mind explaining what exactly -1, -1, -1 is doing in this for statement? Why is -1 typed out 3 times?
for i in range(len(text)-1,-1,-1):


#5

yeah that confused me at first too when I was seeing it on the forum

so length of text starts counting at 1 but the index starts at 0, so you need to subtract 1 from the length of the text to get the proper index of the last letter.
the second -1 is the step that you go down in the range (remember range(start,step, stop))
and the last is the stopping point, because you want to go all the way down to 0, and range is not inclusive of the end point, so you make it go all the way to -1
:slight_smile:


#6

Hey Heatherc93, thanks for the reply! I think i understand it now

So the first -1 subtracts an integer from len which avoids the "out of range error.

The second -1 is to reverse the counting direction.

The third -1 is to include the integer that the range would otherwise stop at.

Is that correct?


#7

suppose, text = "Tuna"
new_string = []
So,
text[0] = T,
text[1] = u,
text[2] = n,
text[3] = a

for i in range(len(text)-1,-1,-1):

len(text) ----> will be equal to 4
len(text) - 1 -----> will be equal to 3, say index 3 of empty string, we will start appending the first character to the last
index of our empty string i.e.
new_string[3] = text[0] and so on.

-1 ----> our loop stop at 0'th index because stopping point always less than 1 i.e. range(start, stop, step) property.
-1 -----> this last -1 will used to decrement loop like [3], [2], [1], [0]. its called the step, by default it is 1 but we need -1 so it
is necessary to mention


#8

hey, thanks for your time, that clears up all my questions!


#9

range(len(x) - 1, -1, -1)

The first index is given above as an expression, some length minus 1. That is the starting index of the range, the last data point in the iterator.

 s   u   r   p   r   i   s   e
[0] [1] [2] [3] [4] [5] [6] [7]

len('surprise') - 1    =>  7

The second index is the stop position. Since the stop position as given is excluded from the range, we need to go one past 0 to the left, else 0 will not be seen in the range.

[-1] [0]
  |
excluded

That sets the starting and ending points, all that is needed is a step. That's where the -1 comes in as the last argument. Starting from the right side, go all the way to the first element, one step to the left at a time.


#10

OMG I went full potato and completely forgot about start,stop,step in a range function this entire time. Now I really understand, thanks for the reply!!!


#11

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