Help Reverse


#1


Oops, try again. Does your reverse function take exactly one argument (a string)? Your code threw a "sequence item 0: expected string, int found" error.

def reverse(x):
    letter = []
    for char in range(len(x)-1, 0-1, -1):
        letter.append(char)
    return "".join(letter)


Help i'm not even sure what the range(len(x)-1, 0-1, -1) is completely for . How does this reverse the order of the string entered for x? Also, isn't range only supposed to take two values?


#2

here:

letter.append(char)

char is just an integer, but this integer do happen to match the indexes of letter.

range will give you the indexes needed (from high to low) to reverse the string

range can have 3 values:

range(start, stop, step)

you start on the right side (len(x) -1), the stop value is -1 because range doesn't include its stop value and strings are zero indexed based. Given you are going from high to low value, you need to specify negative steps/strides


#3

What lesson was the three range values taught because I do not remember that at all. I mean it makes perfect sense now that it has been explained but I wish it would've been covered in the first place.


#4

Hello Stetim94,

For the stop value, why cannot we use '-1' directly instead of saying '0-1'?


#5

who says we can't?


#6

:grinning: Okay thanks!


#7

Hello Stetim94,

Also how does " ".join(letter) convert a list to a string? Is the join function a sequence of appends to an empty string?


#8

someone build a function (join) to make it easier to get from list to string. Its the purpose of the function


#9

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