public
Last active

  • Download Gist
Evolution of a Python programmer.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#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))

import operator
f = lambda n: reduce(operator.mul, range(1,n+1))

Brilliantly done!

i = 1 #Thanks Adam
good one :)

That's aweomse. The windows API style gave me shivers.

Really good, I laughed so hard!

shouldn't it be '\r\n' for windows ? :D

I can't believe no expert programmer is memoizing the result. And using some Lisp :)

Indeed, memoization or bottom-up would be the best approaches (DP).

; midlife crisis programmer (had to remove brackets around first n to post)
(defn factorial n (reduce * (range 1 (inc n))))

#probabalistic programmer:
http://threebrothers.org/brendan/blog/stirlings-approximation-formula-clojure/

So true, brilliant.

Really nice. Here's what I'd write:

fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)

Where is the algorithmist's implementation? Divide and conquer with Fürer's algorithm is obviously the most reasonable choice.

Best laugh I've had today, the web designer one is the best!

I agree, Web designer is the funniest.
Although I really like to code like the "#Python expert programmer" one :)

The first one is not very realistic; I've not seen many newbie programmers who understand recursion.
Reading the last two (windows programmer and enterprise programmer), I fell off my chair laughing.

The web designer one had me laughing. Great :)

the php curl programmer, would curl the url above and scrap the results LOL

Web designer and Windows programmer made my night!

Well, "Lazier" is the best I can get at present.

you probably want some factories, abstract factories or factory factories if you want to be a successful enterprise programmer :)

It looks like I wanted to be a Newbie programmer forever!

#data analyst who wants to be a programmer
R
factorial(6)

#Laziest programmer
print 720

#VBA programmer
def factorial(x):
    if x == 0:
        return 1
    if x == 1:
        return x
    if x == 2:
        return x * (x-1)
    if x == 3:
        return x * (x-1) * (x-2)
    if x == 4:
        return x * (x-1) * (x-2) * (x-3)
    if x == 5:
        return x * (x-1) * (x-2) * (x-3) * (x-4)
    if x == 6:
        return x * (x-1) * (x-2) * (x-3) * (x-4) * (x-5)
print factorial(6)

I actually saw this code at post office.

from math import factorial
print factorial(6)
#Optimized for performance and size
def factorial(x):
    if x == 0:
        return 1
    if x == 1:
        return 1
    if x == 2:
        return 2
    if x == 3:
        return 6
    if x == 4:
        return 24
    if x == 5:
        return 120
    if x == 6:
        return 720
print factorial(6)

I love the enterprise programmer :P
have a life dude !

uaau very good!!

Haha windows programmer FTW

@obeleh

import c_math
def factorial(x):
    return {
        0: 1,
        1: 1,
        2: 2,
        3: 6,
        4: 24,
        5: 120,
        6: 720
    }.get(x) or c_math.fact(x)

Very intriguing, especially the very realistic portrayal of enterprise programmers, though they probably manage to write even more complex solutions in their preferred language Java.

Scumbag programmer:

def factorial():
  return 720
print factorial(6) # it works !

Engineer:

import math
print math.exp(x * math.log(x) - x)

Scientist:

import scipy.misc
def factorial(n): scipy.misc.factorial(n,True)
print factorial(6)

Awesome, made my day! :-)

That’s my style:

#!/usr/bin/env python3
# compatible from Python 2.6 to Python 3.2
from operator import mul
from functools import reduce

# range can only handle ranges up to sys.maxint
def iterrange(a, b):
    while a < b:
        yield a
        a += 1

def factorial(x):
    assert x > 0, 'x has to be greater than or equal to 0'
    return reduce(mul, iterrange(2, x+1), 1)


if __name__ == '__main__':
    print(factorial(6))

Startup guy obsessed with caching everything:

from functools import wraps
def memo(func): 
    cache = {}
    @wraps(func)
    def wrap(*args):
       if args not in cache: cache[args] = func(*args)
       return cache[args] 
    return wrap

@memo
def fact(x):
    return (1<x<1000 and x*fact(x-1)) or 1 # possibly raise VCsDontPayMeEnoughToExecuteThisQueryError('true story.')

haha love it. curious what you mean by "english expert programmer"

ah haha thanks

On Mar 10, 2012, at 2:56 PM, Jakob Kramer wrote:

@chasingtheflow wrote:

curious what you mean by "english expert programmer"

In British English it is "maths," not "math." That’s all. ;)


Reply to this email directly or view it on GitHub:
https://gist.github.com/289467

@gandaro
No, it's not.

 >>> def aa(x):
...     print 'aa'
...     return 20
... 
>>> print {1:1, 10:10}.get(10, aa(10))
aa
10
>>> print {1:1, 10:10}.get(10) or aa(10)
10
>>>

a or b or c or d - in this case arguments will be calculated lazy. In case of {a:a}.get(x, func(x)) func(x) will be calculeted before pass it to get

@troolee That has nothing to do with the snippet above, but you are right about the lazy evaluation. That would be inefficient.

amazing! Windows programmer is great.....

@radiosilence OMG in my run with 200 rounds the Windows Programmer is the fastest

@radiosilence @yaph on my windows machine, the "Entreprise Programmer" is the faster ... haha

@nire thats even worse :D

Do check my benchmark code isn't insane.

I think Python compiles most of them to similar byte code

@radiosilence: Why don’t you use timeit.Timer?

I was too busy, could you do a pull request? :)

@radiosilence: I already rewrote it from scratch now … :bird:

Edit: BTW, @radiosilence, you only test a single algorithm.

@gandaro well, then it's no surprise that Windows programmer and Enterprise programmer did well ;)

Gandaro - Oh FUCK.

Could you do a pull request?

Here are some results now it's fixed:

Recursion limit: 4000
========================================================
Starting algorithm Newbie for 2000!
========================================================
Did it in 9359.77697372 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year Pascal for 2000!
========================================================
Did it in 5224.70474243 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year C for 2000!
========================================================
Did it in 5262.83979416 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year SICP for 2000!
========================================================
Did it in 12595.5700874 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year Python for 2000!
========================================================
Did it in 4722.47600555 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Lazy Python for 2000!
========================================================
Did it in 9027.01616287 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Lazier Python for 2000!
========================================================
Did it in 8971.9414711 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Python Expert for 2000!
========================================================
Did it in 13691.5564537 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Python Hacker for 2000!
========================================================
Did it in 14962.4228477 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Expert Programmer for 2000!
========================================================
Did it in 4450.71458817 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Web Designer for 2000!
========================================================
Did it in 2142111.67097 microsecs averaged over 20 rounds.
========================================================
Answer was correct? False
========================================================

========================================================
Starting algorithm Unix Programmer for 2000!
========================================================
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
Did it in 14699.4709969 microsecs averaged over 20 rounds.
========================================================
sh: factorial: command not found
Answer was correct? False
========================================================

========================================================
Starting algorithm Windows Programmer for 2000!
========================================================
Did it in 5209.56516266 microsecs averaged over 20 rounds.
========================================================
Answer was correct? False
========================================================

========================================================
Starting algorithm Enterprise Programmer for 2000!
========================================================
Did it in 13747.048378 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

Nice! Time for this thread to get meta

On Tue, Apr 3, 2012 at 4:32 PM, Joe Larson <
reply@reply.github.com

wrote:

Is there a way to unsubscribe from a gist thread?


Reply to this email directly or view it on GitHub:
https://gist.github.com/289467

Am 03.04.2012 22:32, schrieb Joe Larson:

Is there a way to unsubscribe from a gist thread?

No, unfortunately there is no way to do so at the moment, but I sent a
feature request to Github to support unsubscribing. Maybe you want to
do so too? Maybe they understand this is
a desired feature then.

I'll be selecting my email client's Spam option if this isn't addressed
soon.

Cheers,

Andrew Pennebaker
www.yellosoft.us

On Tue, Apr 3, 2012 at 5:22 PM, Jakob Kramer <
reply@reply.github.com

wrote:

Am 03.04.2012 22:32, schrieb Joe Larson:

Is there a way to unsubscribe from a gist thread?

No, unfortunately there is no way to do so at the moment, but I sent a
feature request to Github to support unsubscribing. Maybe you want to
do so too? Maybe they understand this is
a desired feature then.


Reply to this email directly or view it on GitHub:
https://gist.github.com/289467

Nice!
Also, a google engineer would use a map-reduce approach with thousands of nodes.

I love how the C programmer has commented on the curly braces :D

Very nicely written..!!! Good job..!

fascinating...!

Lol ! And so desperately true ...

awesome! love especially windows programmer :)

ROFL :) the windows programmmer makes me laugh

ROFLMAO when I read the windows programmer...:D

HAHAHAHAHAHAHA brilliant!!!!

5 years Newbie programmer....

The windows one is awful

#Enterprise programmer
That's java's style.

非常讽刺啊。。。。

penta kill ! god like!

LOL, the C-Style is really funny.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.