7. reverse


#1

These are my code so far:

def reverse(text):
    if type(text) == str:
    	for i in range(len(text)):
   			return text[-(i+1)]

print reverse('abcd')

The output from this example is only "d". I understand that "return" stops the for loop; I am pretty new to programing so I honestly don't know how to keep the "cycle" going through the entire string in this case. Thank you!


#2

You put the return statement at the same indent level as your for loop, this way, the entire loop will run. Then in the loop, push the characters in an array, and return the array after this for loop has finished


#3

The first thing we need to do is look at the simplest approach and then examine it for weaknesses. What if we try this, for starters?

def reverse(s):
    r = ""
    for x in s:
        r = x + r
    return r

This works but doesn't respect immutability of a string. So rather than build a string, we can build a list, then join the members in a return string.

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

print reverse("Monty Python")

#4

Can you explain me that part of you code:
r.append(text[-(i+1)])
return ''.join(r)
like what means - text[-(i+1)]
and that - ''.join


#5

Best we let the OP explain, since it is their code.


#7

Hey thank you so much for teaching me fishing rather than throwing me a bucket of fishes. I really like how your way of approach. This is my final code and I found the if-statement necessary to keep the for loop running:

def reverse(text):
    result = []
    if type(text) == str:
    	for i in range(len(text)):
            result.append(text[-(i+1)])
    return ''.join(result)

#8

You don't have to do it my way. I am new to programming so I am not really familiar with the "software engineers' way of thinking". I came up with this from a mathmatical approach: if you want every elements/letters of a string go backward, these are the changes you have to make:

string_name[0] --> string_name[-1]
string_name[1] --> string_name[-2]
string_name[2] --> string_name[-3]
...


#9

Thx so much for such help. I was confused that in python is such opportunity like making index of element a less than zero. After C/C++ it is quite strange


#10

The negative index tells Python to index from the right, rather than the left.

[-4][-3][-2][-1]

#11

I got it, it's like in math


#14

def reverse(text):
str_text = str(text)
rev_text = ''
for i in range(1,(len(str_text)+1)):
rev_text += str_text[-i:(len(str_text)+1-i)]
return rev_text
print reverse(123)


#15

Learning more about the nuances of Python from the mod comments in the Q&A forum than anything. When would I have ever found out about negative indexing?
This was my solution without knowing that:

    def reverse(text):
        pre_reversal = []
        spot = len(text)
        reversal = [0]*spot
        for char in text:
            pre_reversal.append(char)
        for item in pre_reversal:
            reversal[spot - 1] = item
            spot -= 1
        return ''.join(reversal)

#16

thank you so much for this


#17

A more simple approach is to just go backwards and add this to an empty str

def reverse(word):
i = len(word) -1

dorw = ""
while i > -1:
    dorw += word[i]
    i -= 1
return dorw