Reverse


#1




Oops, try again. Your function fails on reverse("coolstory"). It returns "yrrtslroc" when it should return "yrotslooc".


What was wrong with my code(below)


def reverse(text):
    s =''
    for i in text:
        s = s + text[len(text) - text.index(i) - 1]
    return s


#2

the problem with your code is the use of .index()

index will give the first index of the letter it can find.

so if you have coolstory:

c   o   o   l   s   t   o   r   y
0   1   2   3   4   5   6   7   8

we have o's at the indexes: 1, 2 and 6, but index() will always return 1


#3

thanks for the reply, fixed it :
def reverse(text):
s =''
for i in range(len(text)):
text_re = text[len(text)- i - 1]
s = s + text_re
return s


#4

well done


#5

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