The Evolution of a Python Programmer.

Delightfully ripped from /prog/:
#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
fact = lambda x: reduce(int.mul, xrange(2, x + 1), 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
from c_math import fact
print fact(6)
#BRITISH EXPERT PROGRAMMER
from c_maths import 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))

That made my Monday morning. Thank you!

Funnier than the original.

I specially love how the code from the Windows programmer looks like a Windows API. LOL at the dwReserved parameter!

Tehee.
Web Designer is spot on.
looks at own code
ahem

What I liked was an equivalent sort of thing in Scheme or Haskell (I forget which) that listed a zillion ways of Fibonacci or something (it was a how crazy can you get if you study CS sort of thing, not a WTF sort of thing). So there was the naive one, the memoizing one, and then there was the one that decomposed the whole thing into Church numerals or some variant and combinators. Oddly enough, it was one of the faster ones.

@Talchas said:
What I liked was an equivalent sort of thing in Scheme or Haskell (I forget which) that listed a zillion ways of Fibonacci or something (it was a how crazy can you get if you study CS sort of thing, not a WTF sort of thing).
So there was the naive one, the memoizing one, and then there was the one that decomposed the whole thing into Church numerals or some variant and combinators. Oddly enough, it was one of the faster ones.
Pure functional languages are the most optimisable languages in common use, since the compiler can perform pretty much any transformation it wants on the program without violating correctness. This means that GHC rips most of those apart and replaces it with one of the very simple forms, because it can figure out that they generate the same output. This is distinct from most imperative languages, where the complexity of the generated code usually has to be roughly proportional to the complexity of the source code.
With an ideal Haskell compiler, most of the examples on that page would reduce to exactly the same code. (With a Turing oracle, nearly every possible way to write the factorial function would reduce to the same code, and that's quite unusual for a practical language).

@Talchas said:
What I liked was an equivalent sort of thing in Scheme or Haskell (I forget which) that listed a zillion ways of Fibonacci or something (it was a how crazy can you get if you study CS sort of thing, not a WTF sort of thing). So there was the naive one, the memoizing one, and then there was the one that decomposed the whole thing into Church numerals or some variant and combinators. Oddly enough, it was one of the faster ones.
Aha, found it. The Evolution of a Haskell Programmer. It has the same sort of WTFness in places, but instead of making fun of industry people, it makes fun of CS/research people. I could have sworn there was a site that benchmarked the various versions...

great!! you made my Monday morning!

@Dark Shikari said:
Delightfully ripped from /prog/:
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x  1)
print factorial(6)
Newbie programmers come with tail recursion now? I find that hard to believe. I might buy
def factorial(x):
return x * factorial(x1)
print factorial(6)

Hmm? Thats not tail recursive. The only difference between yours and the original is that yours won't terminate. The tail recursive with a sufficiently smart interpreter/compiler one is the SICP accumulator one. I expect the Python hacker one is actually tail recursive, or at least is supposed to be.

@obediah said:
@Dark Shikari said:
Delightfully ripped from /prog/:
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x  1)
print factorial(6)
Newbie programmers come with tail recursion now? I find that hard to believe. I might buy
def factorial(x):
return x * factorial(x1)
print factorial(6)That looks like a nice infinite loop

@obediah said:
@Dark Shikari said:
Delightfully ripped from /prog/:
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x  1)
print factorial(6)Newbie programmers come with tail recursion now?
No, but that’s not tail recursion, as the return value of the recursive call must undergo further processing. That’s just ordinary recursion, and it closely mimics the way sequences are defined in mathematics (and thus, our “newbie programmer” may be someone with experience in math, but is new to the field of programming)
A solution using tail recursion would be:
<!TRWTF is the forum software  I typed this as HTML source with proper line breaks, but it’s going to convert them to br tags anyway. Anyone know how to disable it without disabling javascript? This is also the reason the font is specified in the style even though it’s a pre.>def factorial2(x,partial) if x == 0: return partial else: return factorial2(x1,x*partial) def factorial(x) return factorial2(x,1) print factorial(6)

Didn't I see one variant of this that had the manager's solution to this as well? Something along the lines of:
To: Paula Bean
From: Pointy Haired Boss
Subject: Fw: factorial problem?!
Hi Paula,
Can you write a program to calculate the factorial of a number? We really need this by COB today.snip out about 50 message deep email chain about unrelated things
Probably also followed by:
To: Upper Management
CC: The whole company
From: Pointy Haired Boss
Subject: re: Factorial problem  SOLVED!!
I have solved the factorial problem!

@Dark Shikari said:
Delightfully ripped from /prog/
7chan or iichan?

@kirchhoff said:
@Dark Shikari said:
Neither, 4chan itself. World4chan, technically, but they're all now hosted on 4chan without any distinction from the other boards. Textboard, of course. If 4chan/prog/ was an imageboard it would probably end up spammed by pictures of Sussman.Delightfully ripped from /prog/
7chan or iichan?

@Dark Shikari said:
@obediah said:
@Dark Shikari said:
Delightfully ripped from /prog/:
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x  1)
print factorial(6)
Newbie programmers come with tail recursion now? I find that hard to believe. I might buy
def factorial(x):
return x * factorial(x1)
print factorial(6)That looks like a nice infinite loop
It will hit the stack limit first. Also wtf is with all that html formatting? <div style="marginleft: 40px;">...</div><div style="marginleft: 40px;"> WTF
Quote to see.

The way I do factorials is that fact(n) = gamma(n + 1). This way, I can take the factorial of a floatingpoint number.