Practice makes perfect- Reverse...Help!


#1



Ok, so im turning the parameter of the function text into list. and i want to know how can i reverse the list and then change it back into a string?


Replace this line with your code. 
def reverse(text):
    text = [ ]
    for word in text:


#2

That is actually wiping out the parameter. Use the list() function...

text = list(text)

#3

def reverse(text):
    letter = []
    for word in list(text):
        letter.append(word[-1])

that's what i got.
error: Oops, try again. Your function fails on reverse("Python!"). It returns "['P', 'y', 't', 'h', 'o', 'n', '!']" when it should return "!nohtyP".

expected to happen:
letter was suppose to append every last word first. And after that i tried to turn [] into a string with str()...and error.


#4

This could be named something like reverse_text, that says what it is.

Here would be where to use the variable name, letter.

for letter in list(text):
    reverse_text.append(letter)
return ''.join(reverse_text)

That's the variable and return mechanics. Now we just have to figure out how to reverse it.


#5

def reverse(text):
    reversed_text = []
    for word in list(text):
        reversed_text.insert(0,word)
    return ''.join(reversed_text)

i have changed from append to insert because append can only take one argument. I don't know how my coded worked. But can you explained to me how reversed_text.insert(0,word) worked? Thank you for your time


#6

Then you shouldn't use it as your solution. Write a solution that uses what you have learned in this module, up to this point. It can be done.


#7

def reverse(text):
    reversed_text = []
    for word in list(text):
        reversed_text.append(word)
    return ''.join(reversed_text)

Ok, if im going to use append instead of insert.
how am i going to reverse it without using reverse()


#8

That is the aim of this exercise, to come up with a method that does it manually, without the use of a built in function. We're learning how to implement a methodical step by step approach, not the fastest or easiest way.

You will not learn much if you don't think this one through.


#9

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