Create a gist now

Instantly share code, notes, and snippets.

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

Brilliantly done!

jmg commented Dec 13, 2011

Amusing! =)

arshaver commented Mar 8, 2012

i = 1 #Thanks Adam
good one :)

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

Thanks Adam. ROFL

Really good, I laughed so hard!

forste commented Mar 8, 2012

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

dnsmkl commented Mar 8, 2012
dimitri commented Mar 8, 2012

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

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!

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.

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

bamdadd commented Mar 8, 2012

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.

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!

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

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

ddgg commented Mar 9, 2012

data analyst who wants to be a programmer

R
factorial(6)

acefspds commented Mar 9, 2012

Laziest programmer

print 720

ruturajv commented Mar 9, 2012

\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.

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)

Awesome, made my day! :-)

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

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"

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

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

gandaro commented Mar 30, 2012

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

yaph commented Mar 30, 2012

@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
========================================================
msg555 commented Apr 3, 2012
gandaro commented Apr 3, 2012
mcandre commented Apr 4, 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

This is too funny.

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

Nice, LOL

WarFox commented Sep 24, 2012

fascinating...!

Ulu2005 commented Nov 29, 2012

LOL!

Zenithar commented Dec 4, 2012

Lol ! And so desperately true ...

4gn3s commented Dec 20, 2012

awesome! love especially windows programmer :)

ROFL :) the windows programmmer makes me laugh

whardier commented May 3, 2013

shakes head

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

@ghost
ghost commented May 25, 2013

HAHAHAHAHAHAHA brilliant!!!!

5 years Newbie programmer....

Strech commented Jul 4, 2013

⭐️ awesome ⭐️

The windows one is awful

Awesome Man

evecalm commented Nov 13, 2013

Enterprise programmer

That's java's style.

openinx commented Nov 13, 2013

非常讽刺啊。。。。

penta kill ! god like!

iljyya commented Nov 14, 2013

哈哈

LOL, the C-Style is really funny.

brilliant :)

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

xmnlab commented Jun 13, 2015

great!

'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)]))

Hahahaha 👍 Nice Man 👯

Windows programer LOL

@bobardkor great work! :>)

cwyark commented May 7, 2016

Great!

Nice...

__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

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

actullally

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

LOL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment