Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#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

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

@tkrajcar

Brilliantly done!

@jmg

Amusing! =)

@arshaver

i = 1 #Thanks Adam
good one :)

@sourishkrout

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

@bertomartin

#Thanks Adam. ROFL

@davidgomes

Really good, I laughed so hard!

@forste

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

@dnsmkl

+

@dimitri

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

@davidgomes

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

@bin5

i like it :D

@xicubed

; 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

Purely awesome!

@brixtonasias

I smiled.

@nremond

So true, brilliant.

@2bt
2bt commented

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

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

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

@marcqualie

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

@bamdadd

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

@jhdevos

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

The web designer one had me laughing. Great :)

@Mercurial

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

@captainhead

Web designer and Windows programmer made my night!

@seth-hg

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

@tszming

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

@ronbeltran

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

@ddgg

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

@acefspds

#Laziest programmer
print 720

@ruturajv

\m/

@kohashi
#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
from math import factorial
print factorial(6)
@obeleh
#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

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

@radames

uaau very good!!

@DVDPT

Haha windows programmer FTW

@troolee

@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

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

Scumbag programmer:

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

Engineer:

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

Scientist:

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

Awesome, made my day! :-)

@mcandre

Yes, yes, yes!

@gandaro

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

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.')
@chasingtheflow

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

@chasingtheflow
@troolee

@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

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

@ghdawn

amazing! Windows programmer is great.....

@yaph

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

@nremond

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

@yaph

@nire thats even worse :D

@radiosilence
@gandaro

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

@radiosilence
@yaph

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

@radiosilence

Gandaro - Oh FUCK.

Could you do a pull request?

@radiosilence

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
@gandaro
@mcandre
@caruccio

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

@muxcmux

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

@nickpoorman

This is too funny.

@pythonjunkie

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

@pydsigner

Nice, LOL

@WarFox

fascinating...!

@Ulu2005

LOL!

@Zenithar

Lol ! And so desperately true ...

@4gn3s

awesome! love especially windows programmer :)

@gnaggnoyil

ROFL :) the windows programmmer makes me laugh

@whardier

shakes head

@SubhrajitPrusty

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

@alxkls

HAHAHAHAHAHAHA brilliant!!!!

@realduke2000

5 years Newbie programmer....

@Strech

:star: awesome :star:

@minaeid90

The windows one is awful

@abhinavongraph

Awesome Man

@evecalm

#Enterprise programmer
That's java's style.

@openinx

非常讽刺啊。。。。

@hustlijian

penta kill ! god like!

@iljyya

哈哈

@Hacker-YHJ

LOL, the C-Style is really funny.

@neerajsharma7

brilliant :)

@nickschurch

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

Very educational and brilliant

@xmnlab

great!

@iamwucheng

'awesome'*sys.maxint, LOL

@pavdmyt
# 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)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.