Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

General python question thread

Name: Pythonymous 2011-06-05 22:51

def reverseit(x):
    y = ''
    for i in range(len(x)):
        j=len(x) - i -1
        y = y + x[j] 
    return(x)


What's wrong with this code?  inb4 FIOC

Name: Anonymous 2011-06-05 23:23

    return x[::-1]*

Name: Anonymous 2011-06-06 0:23

>>1
Or were you

Name: Anonymous 2011-06-06 1:59

>>1
Quibbles aside, shouldn't you be returning y?

Name: Anonymous 2011-06-06 2:14

>>4

HAHAHAHAHAAHAHAH.  I feel dumb now.  :)

Name: Anonymous 2011-06-06 3:03

>>5
Please see >>2 and stop reinventing the wheel.

Name: Anonymous 2011-06-06 3:31

>>6

Could you explain it to me?

Name: Anonymous 2011-06-06 3:35

Name: Pythonymous 2011-06-06 3:53

>>8

Oh wow, I feel so dumb now.  I was told to start attempting to write my own programs immediately to learn and that I'd progress faster than reading the documentation thoroughly.  I will now go proceed to read that before I attempt to create a frankenstein skillset of bad habits.

Name: Anonymous 2011-06-06 4:23

How about return "".join(reversed(x)) instead.

Name: Anonymous 2011-06-06 6:42

(foldl cons '() list)
Was it so hard?

Name: Anonymous 2011-06-06 9:59

>>10
You tell me.


import time

def t1(s):
    return s[::-1]

def t2(s):
    return ''.join(reversed(s))

s = 'something'
t = time.clock()
for i in range(100000):
    t1(s)
print (time.clock() - t)
t = time.clock()
for i in range(100000):
    t2(s)
print (time.clock() - t)

Name: Anonymous 2011-06-06 11:33

Name: Anonymous 2011-06-06 17:40

Name: Anonymous 2011-06-06 17:45


Don't change these.
Name: Email:
Entire Thread Thread List