Instantly share code, notes, and snippets.

# fmeyer/Evolution of a Python programmer.py Created Jan 29, 2010

What would you like to do?
 #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))

### Cam1337 commented Nov 3, 2011

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

### tkrajcar commented Nov 28, 2011

 Brilliantly done!

### jmg commented Dec 13, 2011

 Amusing! =)

### arshaver commented Mar 8, 2012

 `i = 1 #Thanks Adam` good one :)

### sourishkrout commented Mar 8, 2012

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

### davidgomes commented Mar 8, 2012

 Really good, I laughed so hard!

### forste commented Mar 8, 2012

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

### dimitri commented Mar 8, 2012

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

### davidgomes commented Mar 8, 2012

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

### mpaltun commented Mar 8, 2012

 i like it :D

### xicubed commented Mar 8, 2012

; 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/

### Scorpil commented Mar 8, 2012

 Purely awesome!

### brixtonasias commented Mar 8, 2012

 I smiled.

### nremond commented Mar 8, 2012

 So true, brilliant.

### 2bt commented Mar 8, 2012

 Really nice. Here's what I'd write: `fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)`

### msg555 commented Mar 8, 2012

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

### marcqualie commented Mar 8, 2012

 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 :)

### jhdevos commented Mar 8, 2012

 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.

### brianewing commented Mar 8, 2012

 The web designer one had me laughing. Great :)

### Mercurial commented Mar 9, 2012

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

### captainhead commented Mar 9, 2012

 Web designer and Windows programmer made my night!

### seth-hg commented Mar 9, 2012

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

### tszming commented Mar 9, 2012

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

### ronbeltran commented Mar 9, 2012

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

R
factorial(6)

# Laziest programmer

print 720

 \m/

### kohashi commented Mar 9, 2012

 ```#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.

### rspeed commented Mar 9, 2012

 ```from math import factorial print factorial(6)```

### obeleh commented Mar 9, 2012

 ``````#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) ``````

### mrahma01 commented Mar 9, 2012

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

### radames commented Mar 9, 2012

 uaau very good!!

### DVDPT commented Mar 9, 2012

 Haha windows programmer FTW

### troolee commented Mar 9, 2012

 @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) ``````

### yaph commented Mar 9, 2012

 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.

### bobardkor commented Mar 9, 2012

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

### rjw57 commented Mar 9, 2012

 Engineer: ``````import math print math.exp(x * math.log(x) - x) ``````

### ali65 commented Mar 9, 2012

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

### mcandre commented Mar 9, 2012

 Yes, yes, yes!

### gandaro commented Mar 9, 2012

 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)) ``````

### luke14free commented Mar 10, 2012

 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

### chasingtheflow commented Mar 10, 2012

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

### chasingtheflow commented Mar 10, 2012

 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

### troolee commented Mar 11, 2012

 @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`

### gandaro commented Mar 11, 2012

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

### ghdawn commented Mar 13, 2012

 amazing! Windows programmer is great.....

### yaph commented Mar 30, 2012

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

 Lol!!

### nremond commented Mar 30, 2012

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

### yaph commented Mar 30, 2012

 @nire thats even worse :D

### radiosilence commented Mar 30, 2012

 Do check my benchmark code isn't insane. I think Python compiles most of them to similar byte code

### gandaro commented Mar 30, 2012

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

### radiosilence commented Mar 30, 2012

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

### gandaro commented Mar 30, 2012

 @radiosilence: I already rewrote it from scratch now … 🐦 Edit: BTW, @radiosilence, you only test a single algorithm.

### yaph commented Mar 30, 2012

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

### radiosilence commented Apr 1, 2012

 Gandaro - Oh FUCK. Could you do a pull request?

### radiosilence commented Apr 1, 2012

 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 ======================================================== ``````

### msg555 commented Apr 3, 2012

 Nice! Time for this thread to get meta … On Tue, Apr 3, 2012 at 4:32 PM, Joe Larson < ***@***.*** > 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

### gandaro commented Apr 3, 2012

 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](https://github.com/contact)? Maybe they understand this is a desired feature then.

### mcandre commented Apr 4, 2012

 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 < ***@***.*** > 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](https://github.com/contact)? Maybe they understand this is a desired feature then. --- Reply to this email directly or view it on GitHub: https://gist.github.com/289467

### caruccio commented Apr 24, 2012

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

### muxcmux commented May 15, 2012

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

### nickpoorman commented Sep 2, 2012

 This is too funny.

### pythonjunkie commented Sep 7, 2012

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

### pydsigner commented Sep 7, 2012

 Nice, LOL

### WarFox commented Sep 24, 2012

 fascinating...!

 LOL!

### Zenithar commented Dec 4, 2012

 Lol ! And so desperately true ...

### 4gn3s commented Dec 20, 2012

 awesome! love especially windows programmer :)

### gnaggnoyil commented Apr 24, 2013

 ROFL :) the windows programmmer makes me laugh

### SubhrajitPrusty commented May 20, 2013

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

### ghost commented May 25, 2013

 HAHAHAHAHAHAHA brilliant!!!!

### realduke2000 commented Jun 25, 2013

 5 years Newbie programmer....

### Strech commented Jul 4, 2013

 ⭐️ awesome ⭐️

### minaeid90 commented Jul 10, 2013

 The windows one is awful

### abhinavongraph commented Sep 4, 2013

 Awesome Man

# Enterprise programmer

That's java's style.

### openinx commented Nov 13, 2013

 非常讽刺啊。。。。

### hustlijian commented Nov 13, 2013

 penta kill ! god like!

 哈哈

### Hacker-YHJ commented Nov 14, 2013

 LOL, the C-Style is really funny.

### neerajsharma7 commented Dec 8, 2014

 brilliant :)

### nickschurch commented Jun 11, 2015

 Can I add what is clearly the best and most elegant solution to the mix: ``````#Python data scientist import scipy.misc as sc sc.factorial(6) ``````

### oskar-j commented Jun 11, 2015

 Very educational and brilliant

 haha

 great!

### iamwucheng commented Jun 30, 2015

 `'awesome'*sys.maxint`, LOL

### pavdmyt commented Jul 16, 2015

 ``````# Python programmer with math background import math as m def fact(x): return m.exp(sum([m.log(k) for k in range(1, x+1)])) ``````

### faizalpribadi commented Aug 2, 2015

 Hahahaha 👍 Nice Man 👯

### felipsmartins commented Sep 3, 2015

 Windows programer LOL

### jemyzhang commented Jan 29, 2016

 @bobardkor great work! :>)

 Great!

### ilkereroglu commented Aug 24, 2016

 Nice...

### hossein-pourbozorg commented Sep 5, 2016

 `__import__('math').factorial(8)`

### pavdmyt commented Sep 20, 2016

 ``````fact = lambda n: reduce(int.__mul__, range(1, n+1), 1) ``````

### alexsv commented Nov 16, 2016• Edited 1 time alexsv Nov 16, 2016

 fact = lambda n: reduce(int.mul, range(1, n+1), 1) actullally `fact = lambda n: reduce(__import__('operator').mul, range(1, n+1))`

 LOL

### hz658832 commented May 30, 2017

 Did somebody programm this staff using PLC (structed Text) :) But cool!