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

Evolution of a Python programmer

Name: Anonymous 2007-05-25 5:34 ID:e87L90K/


#Newbie programmer
def factorial(x):
    if x == 0:
        return 1
    else:
        return x * factorial(x - 1)
print factorial(6)


#First year programmer, studied Pascal
def factorial(x):
    result = 1
    i = 2
    while i <= x:
        result = result * i
        i = i + 1
    return result
print factorial(6)


#First year programmer, studied C
def fact(x): #{
    result = i = 1;
    while (i <= x): #{
        result *= i;
        i += 1;
    #}
    return result;
#}
print(fact(6))


#First year programmer, SICP
@tailcall
def fact(x, acc=1):
    if (x > 1): return (fact((x - 1), (acc * x)))
    else:       return acc
print(fact(6))


#First year programmer, Python
def Factorial(x):
    res = 1
    for i in xrange(2, x + 1):
        res *= i
    return res
print Factorial(6)


#Lazy Python programmer
def fact(x):
    return x > 1 and x * fact(x - 1) or 1
print fact(6)


#Lazier Python programmer
f = lambda x: x and x * f(x - 1) or 1
print f(6)


#Python expert programmer
import operator as op
import functional as f
fact = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
print fact(6)


#Python hacker
import sys
@tailcall
def fact(x, acc=1):
    if x: return fact(x.__sub__(1), acc.__mul__(x))
    return acc
sys.stdout.write(str(fact(6)) + '\n')


#EXPERT PROGRAMMER
import c_math
fact = c_math.fact
print fact(6)


#ENGLISH EXPERT PROGRAMMER
import c_maths
fact = c_maths.fact
print fact(6)


#Web designer
def factorial(x):
    #-------------------------------------------------
    #--- Code snippet from The Math Vault          ---
    #--- Calculate factorial (C) Arthur Smith 1999 ---
    #-------------------------------------------------
    result = str(1)
    i = 1 #Thanks Adam
    while i <= x:
        #result = result * i  #It's faster to use *=
        #result = str(result * result + i)
           #result = int(result *= i) #??????
        result str(int(result) * i)
        #result = int(str(result) * i)
        i = i + 1
    return result
print factorial(6)


#Unix programmer
import os
def fact(x):
    os.system('factorial ' + str(x))
fact(6)


#Windows programmer
NULL = None
def CalculateAndPrintFactorialEx(dwNumber,
                                 hOutputDevice,
                                 lpLparam,
                                 lpWparam,
                                 lpsscSecurity,
                                 *dwReserved):
    if lpsscSecurity != NULL:
        return NULL #Not implemented
    dwResult = dwCounter = 1
    while dwCounter <= dwNumber:
        dwResult *= dwCounter
        dwCounter += 1
    hOutputDevice.write(str(dwResult))
    hOutputDevice.write('\n')
    return 1
import sys
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)


#Enterprise programmer
def new(cls, *args, **kwargs):
    return cls(*args, **kwargs)

class Number(object):
    pass

class IntegralNumber(int, Number):
    def toInt(self):
        return new (int, self)

class InternalBase(object):
    def __init__(self, base):
        self.base = base.toInt()

    def getBase(self):
        return new (IntegralNumber, self.base)

class MathematicsSystem(object):
    def __init__(self, ibase):
        Abstract

    @classmethod
    def getInstance(cls, ibase):
        try:
            cls.__instance
        except AttributeError:
            cls.__instance = new (cls, ibase)
        return cls.__instance

class StandardMathematicsSystem(MathematicsSystem):
    def __init__(self, ibase):
        if ibase.getBase() != new (IntegralNumber, 2):
            raise NotImplementedError
        self.base = ibase.getBase()

    def calculateFactorial(self, target):
        result = new (IntegralNumber, 1)
        i = new (IntegralNumber, 2)
        while i <= target:
            result = result * i
            i = i + new (IntegralNumber, 1)
        return result

print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))

Name: Anonymous 2007-09-26 3:47 ID:UgzNM2mX

>>120
first enter /prog/ -> realize how retarded lispfags are -> realize how shit lisp is -> move on to real languages and out of /prog/

Name: Anonymous 2007-09-26 3:55 ID:J2CzvopX

first enter /prog/ -> try to troll lispfags and/or say SICP sucks -> fail -> move out of /prog/

Name: Anonymous 2007-09-26 4:22 ID:KjvUunY3

Well, but SICP does suck and LISP too.

Name: Anonymous 2007-09-26 4:39 ID:J2CzvopX

>>123

You're almost there!

Name: Anonymous 2007-11-30 14:55

Name: Anonymous 2008-01-01 21:56

>>52
gb2 4-ch/code/!!!!

Name: Anonymous 2008-01-01 21:59

oh fuck I didn't want to bump this thread

Name: Anonymous 2008-01-02 0:16

>>127
email goes in sage field

Name: sage !ccqXAQxUxI 2008-01-02 0:27

sage goes in all fields

Name: Anonymous 2008-02-26 20:37

WAPAN POWA

Name: Anonymous 2008-02-26 22:29

;; Good programmer
(fact x)

Name: Anonymous 2008-02-27 0:56

>>131
x!

Name: Anonymous 2008-02-27 1:51

>>132
Real programmers are point-free.

!p

Name: sage 2008-02-27 10:17

sage

Name: Anonymous 2008-02-27 10:46

>>133
Haskell is point-less.

Name: Anonymous 2008-04-19 19:25

HI, I'M PAUL GRAHAM, FOUNDER AND CEO OF Y COMBINATOR. I HAVE MADE MANY INSIGHTFUL STATEMENTS IN MY ESSAY ENTITLED "YOUR MOTHER AND THE 48 VERY SMART PEOPLE" AND WOULD LIKE TO THANK TREVOR BLACKWELL, ROBERT MORRIS AND JESSICA LIVINGSTON FOR HAVING READ DRAFTS OF THIS ESSAY, WHICH YOU WILL BE ABLE TO FIND WITH 21 OTHERS ESSAYS IN MY UPCOMING BOOK "HACKERS AND YOUR SISTER". I GUARANTEE IT.

Name: Anonymous 2008-04-19 20:32

WRITING POINTLESS CODE IS POINTLESS I GET IT HA HA HA

Name: "GRUNNUR" 2008-10-31 9:14

>>138
"GRUNNUR"

Name: Anonymous 2008-10-31 9:53

This thread is comedy gold.

Name: Anonymous 2008-10-31 10:17

>>140

out

Name: Anonymous 2008-11-22 3:25

>With int.__mul__, it restricts what you do to to ints

I usually use lambda x, y: x * y

Name: Anonymous 2009-03-06 12:59

The result of 41?

Name: Anonymous 2009-03-12 13:11

@143: operator.mul could be used instead of int.__mul__
@52: The code can be simplified:

>>> map(lambda n: (lambda f, n: f(f, n))(lambda f, n: n*f(f, n-1) if n > 0 else 1, n), range(10))
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

Name: Anonymous 2009-03-12 14:21

@145
back to twitter, please

Name: Anonymous 2009-03-12 20:24

>>124
The last post with an ID in this thread ;_;

Name: Anonymous 2009-03-13 0:43

I'm somewhat alarmed the expert programmer does not use prime swing or split recursive. I certainly would not hire him to produce expert code for me.

Name: Anonymous 2009-03-13 0:56

>>148
you can't write efficient algorithms in python. guido, like most people, associates efficient algorithms with functional programming, and we all know how he feels about that...

Name: Anonymous 2009-03-13 1:08

>>149
Funny that, because when I said expert programmer; I wasn't referring to the imaginary one that still uses toy languages.

Name: Anonymous 2009-03-13 3:54

>>148-149
here's split recursive:
def factorial(n):
    if not isinstance(n, (int, long)): raise TypeError("expected %s but got %s" % ((int, long), type(n)))
    if n < 0: raise ValueError("%d is less than 0" % n)
    if n < 2: return 1
    global N
    p = r = N = high = 1
    h = shift = 0

    def floorlog2(n):
        a = 0
        while n > 1:
            n /= 2
            a += 1
        return a

    log2n = floorlog2(n)

    def product(n):
        global N
        m = n / 2
        if m == 0:
            N += 2
            return N
        if n == 2:
            N += 4
            return N * (N - 2)
        return product(n - m) * product(m)

    while h != n:
        shift += h
        h = n >> log2n
        log2n -= 1
        len = high
        high = (h - 1) | 1
        len = (high - len) / 2
        if len > 0:
            p *= product(len)
            r *= p
    return r << shift


>>150
split recursive in python is still way too slow to be useful.
prime swing in haskell is fast enough to be useful, and takes about as much time to write as split recursive in python.
prime swing in python might be fast enough to be useful, but i'm not going to write that unless someone pays me at least 10 XAG to do it.

Name: Anonymous 2009-03-13 4:50

    def floorlog2(n):
        a = 0
        while n > 1:
            n /= 2
            a += 1
        return a


What, are you trying to make this slower than a regular one line recursive facts?

Name: Anonymous 2009-03-13 5:24

>>152
you think you can do it faster? where is your code?

Name: Anonymous 2009-03-13 5:46

>>153
I'm not going to write it because I'll be honest and say I'm not expert at python and I will get laughed at. I'll give you a protip however, integers are STORED in a base 2 representation that easily lends itself to describing the lg of its decimal representation, actually going and calculating this is completely redundant and will incur an O(lg(n)) time cost which is infact the same as a regular facts.

Name: Anonymous 2009-03-13 5:56

>>152
none of the recursive ones can even do factorial(10000): RuntimeError: maximum recursion depth exceeded

Name: Anonymous 2009-03-13 5:59

>>154
yes, you should be able to do it faster, but as far as i can tell python doesn't let you get at that information.

Name: Anonymous 2009-03-13 6:15

>>156
If you are telling me python does not have bitwise operations I am sure you are full of shit.

Name: Anonymous 2009-03-13 6:26

>>157
according to the benchmarks i just did, >>= 1 isn't any faster than /= 2.

Name: Anonymous 2009-03-13 6:28

>>158
hurrdurr, what the hell do you think /= 2 represents you faggot and where did yu get the idea a compiler wouldnt optimize it out?

Name: Anonymous 2009-03-13 6:39

>>159
you're the one who asserted that bitwise operations would be faster in >>157, dipshit.

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