7 reverse


#1

This is my code.


def reverse(text):
rev=[]
for i in range(len(text)):
rev.append(text(len(text)-i-1)
return rev

print reverse("abc")


and here is an error message.
File "python", line 5
return rev
^
SyntaxError: invalid syntax


can you guys tell me what's wrong with it


#2

Difficult to say, what with plain pasting, but indentation may be an issue. There is a way to post code; you know that, right?


#4

I'm having problems with this one too. This is what I have so far

def reverse(text):

new_text = []
num = len(text) - 1

for x in text:
    new_text.append(num)
    num == num- 1

print "".join(new_text)   

This is the error message I am getting

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.


#5

should read,

new_text.append(x)

#6

The OP is on the right track, as is the above. Iterate through the text from end to start and build an array in reverse.

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

print reverse("a bird in the hand is worth two in the bush")
# hsub eht ni owt htrow si dnah eht ni drib a
Code
def reverse(text):
    k = len(text) - 1
    r = []
    for i in range(k+1):
        print (i, k-i)
        r.append(text[k-i])
    return "".join(r)

print reverse("a bird in the hand is worth two in the bush")

#7

def reverse(text):
    results = []
    for n in range(len(text)):
        results.append(text[len(text) - (n + 1)])
    return "".join(results)
    
print reverse("abcd")

My solution is similar to yours, but I think you need to do (n + 1) instead of the - i - 1.

You can post accurate spacing by using three back ticks before and after your code. (back ticks are to the left of the number 1 on most keyboards)


#8

def revers(text):
    k = len(text) - 1
    r = ''
    for i in range(k+1):
        r += text[k-i]
    return r

print revers("a bird in the hand is worth two in the bush")
Code
def reverse(text):
    k = len(text) - 1
    r = []
    for i in range(k+1):
        print (i, k-i)
        r.append(text[k-i])
    return "".join(r)

print reverse("a bird in the hand is worth two in the bush")

def revers(text):
    k = len(text) - 1
    r = ''
    for i in range(k+1):
        print (i, k-i)
        r += text[k-i]
    return r

print revers("a bird in the hand is worth two in the bush")

#9

Simplify, simplify, simplify! That’s what my math and physic teachers would always say. The simpler the better, so long as it stays with the laws that apply.

def rever(text):
    r = ''
    for i in range(len(text)):
        r = text[i] + r
    return r

print rever("a bird in the hand is worth two in the bush")
Code
# reverse text
# study by Roy inspired by Q&A topic
# http://discuss.codecademy.com/t/7-reverse/12764


def reverse(text):
    k = len(text) - 1
    r = []
    for i in range(k+1):
        # print (i, k-i)
        r.append(text[k-i])
    return "".join(r)

print reverse("a bird in the hand is worth two in the bush")

def revers(text):
    k = len(text) - 1
    r = ''
    for i in range(k+1):
        # print (i, k-i)
        r += text[k-i]
    return r

print revers("a bird in the hand is worth two in the bush")

def rever(text):
    r = ''
    for i in range(len(text)):
        r = text[i] + r
    return r

print rever("a bird in the hand is worth two in the bush")

#11

Consider the best response in this thread... "Reverse a string in Python ..."


#12

Thanks all, this is what I got to work in the end

def reverse(text):

new_text = []

for t in text:
    new_text.insert(0, t)

return "".join(new_text)

print reverse("hello")


#13

The simplest code that i can think is this one

def reverse(text):
    reversed_text = ""
    for c in text:
        reversed_text = c +reversed_text
    return reversed_text

:smirk:


#14

Which is where we were leading, and @cssninja18503 has hit upon it, rather squarely, I might add.

def reve(text):
    r = ''
    for i in text:
        r = i + r
    return r

print reve("a bird in the hand is worth two in the bush")
Code
# reverse text
# study by Roy inspired by Q&A topic
# http://discuss.codecademy.com/t/7-reverse/12764


def reverse(text):
    k = len(text) - 1
    r = []
    for i in range(k+1):
        # print (i, k-i)
        r.append(text[k-i])
    return "".join(r)

print reverse("a bird in the hand is worth two in the bush")

def revers(text):
    k = len(text) - 1
    r = ''
    for i in range(k+1):
        # print (i, k-i)
        r += text[k-i]
    return r

print revers("a bird in the hand is worth two in the bush")

def rever(text):
    r = ''
    for i in range(len(text)):
        r = text[i] + r
    return r

print rever("a bird in the hand is worth two in the bush")

def reve(text):
    r = ''
    for i in text:
        r = i + r
    return r

print reve("a bird in the hand is worth two in the bush")

#15

nice code man...simplest and fastest..:grin:


#18

oh wow... I went and solved this but made it far more complicated than need be !!! :joy: :joy:

def reverse(text):
    L = [] #List for original string
    M = [] #List for reversed string

    for i in text:
        L.append(i) #append text to list "L"
    length = len(L) #get length of list
    while length >=1:
        M.append(L[length -1])#append from the back to new list "M"
        length = length -1
    return "".join(M) #return reversed string

print reverse("a bird in the hand is worth two in the bush")

#20

So I might just be showing my "noob" status, but I can't wrap my head around how/why the line "r = i + r" works. I understand the rest of the code, trying to visualize it is driving me insane. I answered the question accurately. My code is below. It is not as pretty, but it works.


#21

The line,

    r = i + r

Builds the reverse string from right to left. The letters are captured from left to right, then prepended to the new string.

Python
P
yP
tyP
htyP
ohtyP
nohtyP

#23

@mtf

I keep trying to post code properly, but I can't seem to get it to keep the indentations. I tried looking it up also, but nothing I try works.

How does one keep the indentations?

Thanks.


#24
  1. Code phrase inline, use back-tick like a quote; eg.

`

for..in

`

appears as, for..in within the context of a sentence.
2. Four (4) space characters at the start of a line for code sample on that line.
3. Select a block of code and click </> to preserve editor format.
4. On a line above and below a code segment, one or more lines, type three (3) back-ticks.


#25

My code visually prints !nohtyP but I get the error Your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP".

Any idea of why that is?

def reverse(text):
    length = len(text)
    while length > 0:
        for c in text:
            print text[length - 1],
            length -= 1 
            break
reverse('Python!')

#26

The code visually prints, is the operative phrase. It should instead by concatenated into a string and returned.

def reverse(text):
    rev = ""
    for c in text:
        rev = c + rev
    return rev

print reverse("Python!");