7.Reverse


#1


Oops, try again. Your function fails on reverse("Python!"). It returns "Python!" when it should return "!nohtyP".


def reverse(text):
    length=len(text)
    re_text=[]
    count=0
    for l in range(1,length):
        re_text.append(text[:-1])
        re_text.remove(text[:-1])
        count-=1
        return text


#3

@gst82, posting answers without explanation is not according to the guidelines, try to explain why w111340 code isn't working


#4

@w111340, there are a few issues with your code.

first, understand that this:

text[:-1]

will give you a slice, lets say text contains Python! then text[:-1] will give you Python, if you want the last character simply do: text[-1]

then another huge problem in this line:

re_text.remove(text[:-1])

.remove() is for lists, not for strings, strings are immutable in python, so you can't just remove items from a string.

you could of course convert the string into a list, then using remove is possible, but realize that .remove() removes the match item, not on index, let me show you what i mean:

aList = ['a','b','c']
aList.remove('a')

if you want to delete on index use del:

aList = ['a','b','c']
del aList[-1] # will remove the last item

then the final problem:

return text

are you sure you don't want to return re_text, which is going to contain your reversed string? also, a function ends the moment a return keyword is reached, place the return after the loop has finished:

for l in range(1,length):
   # loop content
return re_text

#5

so the first problem is that you are appending a character and in the next row you are removing it, second problem you are returning text, not re_text, which will always return the original text since the strings are immutable, so in order to create a new reverse string you need either to convert the string into list and return the list or create an empty list and append characters to it


#6

def reverse(text):
    re_text = ''
    for l in range(len(text)):
        re_text = re_text + text[len(text) - l - 1]
        l += 1
    return re_text

So I have simplified your code, what I am doing right now is that I am creating an empty string called re_text and I am appending the characters in opposite order and then I am returning the same string to the console. In this case:
l - will be between 0 and the length of the string contained in "text"
and text[len(text) - l - 1] will be the index of the string in opposite direction


#7

If it helps

def reverse(text):
    lst = []
    for i in text:
        lst.append(i)
    lst2 = []    
    for n in range(1,len(lst)+1):
        lst2.append(lst[len(lst)-n])
    word = ""
    for x in lst2:
        word += str(x)
    return word

#8

l is incrementing in the for loop. Have you tested this code?

Posting complete solutions without discussion is discouraged on these forums. Yours warrants some discussion, though...

Why use a loop to build a list when you have the list() function right at hand?

lst = list(text)

Likewise, why use a loop when we have the join() method at hand?

return ''.join(lst2)

#9

okay :slight_smile:
thanks a lot for the help


#10

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