
Evolution of a Python programmer - r11t
http://gist.github.com/289467
======
sophacles
Web 2.0 programmer:

    
    
      import urllib
      import json
    
      def fact(n):
         res = urllib.urlopen('http://math.org/factorial/?' +
             urllib.urlencode({'n':n, 'format':'json'})).read()
         return json.loads(res)['answer']
      fact(6)
    

Edit: for a "web scale systems guy" throw an @memcache_memoize in there :)

Edit2: fixed a syntax error.

Edit3: general cased it. thanks lolcraft!

~~~
lolcraft

      {'n':6, 'format':'json'}
    

Shouldn't that be:

    
    
      {'n':n, 'format':'json'}

~~~
sharkbrainguy
why write such a general case, YAGNI

~~~
sophacles
More of a scotty principle type thing: when management comes by and wants to
us to figure out combinations of 7 things, we can claim it will take 5 days,
get it done in a 1 char fix, play games all week. Come monday morning's
meeting, don't drink coffee before hand (to look tired), and claim that for a
little extra work (and a looong weekend) you got it to work for combinations
of ANY NUMBER of things! Worse that happens: you get credit for taking one for
the team, best case: "take the week off, thanks for your heroics!"

~~~
umjames
If you happen to know of a place where you earnestly hear "take the week off,
thanks for your heroics!", please don't be stingy with their URL. I'd be
interested in working there.

~~~
daeken
I actually got a week's worth of PTO after a little crunch time (a month or
so) at the last startup I worked for. However, I don't believe they still
exist in any substantial form anymore (and couldn't in good faith recommend
them anyways, for reasons I don't believe I should go into).

------
notaddicted

      # "close enough" programmer
      import math
      def fact(x):
        math.sqrt(2*math.pi*n) * math.pow(n/math.e, n)
      print fact(6)
    
      # lazy-er programmer
      def fact(x):
        factorials = {6:720} # found on my calculator, add more as they come up
        if factorials.has_key(x):
          return factorials[x]
        else:
          raise NotImplementedError
      print fact(6)

~~~
Vivtek
NotImplementedError - that kills me!

------
shabda
<http://dis.4chan.org/read/prog/1180084983/>

<http://www.hackerne.ws/item?id=1012877>

Wow the domain can make a huge difference. :)

~~~
tl
What's with the hackerne.ws link?

whois ycombinator.com -> pg

whois hackerne.ws -> some godaddy registrar.

------
yan
The web designer and windows programmer snippets are brilliant.

~~~
cabalamat
The enterprise programmer one is pretty good too. Reminds me of EJB (shudder).

~~~
pwhelan
Productivity can only be measured by lines of code-- what a co-worker told me.
Reminds me of that dark, evil place.

~~~
yan
Likewise, productivity of surgeons can only be measured in numbers of
stitches.

------
mrshoe
I'm proud to be a Lazy Python Programmer... but not lazy enough to skip
"What's New in Python 2.5":

    
    
        def factorial(x):
            return x * factorial(x-1) if x > 1 else 1

------
codexon
This is an uninspiring ripoff of the older original here:

[http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer....](http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html)

And here is one that is actually well done for Haskell.

<http://www.willamette.edu/~fruehr/haskell/evolution.html>

~~~
nailer
The article is entirely new content updated to fit an existing idea. How is
that a 'ripoff'?

------
sriramk
I'm ashamed to admit that I actually learnt something new from this (the
@tailcall thing)

~~~
pmiller2
You should never be ashamed to admit ignorance, especially after it's been
cured. :-)

~~~
pgbovine
wait, is @tailcall part of the Python standard library? or just something the
author made-up for illustrative purposes?

~~~
bdr
made up [http://neopythonic.blogspot.com/2009/04/final-words-on-
tail-...](http://neopythonic.blogspot.com/2009/04/final-words-on-tail-
calls.html)

~~~
sophacles
There are a few /very/ hacky @tailcall decorators floating around the web. In
some cases they actually speed things up some small amount. A year or two ago
there was a bug furor over bytecode hacking -- such things were the result :)

------
alxv
Functional programmer:

    
    
        import operator
        def factorial(n):
            return reduce(operator.mul, xrange(1, n+1), 1)
        print factorial(6)
    

Cutting-edge programmer:

    
    
       import math
       print(math.factorial(6))

~~~
ozataman
reduce is pretty much the same thing as foldl. The functional programmer is
already included as "Expert Python Programmer"...

~~~
Estragon
The difference is, the code given here actually works out of the box.

------
alrex021
Java programmer:

    
    
      class FactorialCommand(object):
          def set_num(self, num):
              self.__num = num
    
          def get_num(self):
              return self.__num
    
          def execute(self):
              result = 1
              i = 2
              while i <= self.get_num():
                  result = result * i
                  i = i + 1
              return result
    
      cmd = FactorialCommand()
      cmd.set_num(6)
      print cmd.execute()

~~~
elai
I thought the 'enterprise' programmer was the java programmer.

~~~
jbooth
No, that's the crappy java programmer

~~~
rbanffy
So... The FactorialCommand one was the good Java programmer?

~~~
bfung
More like C# programmers. Java programmers would either not know how to solve
this problem (the majority), or write the 3 line static method.

Sarcasm based on interviewing experience =P

------
samdk
The "I find a way to use list comprehensions for everything" Python
programmer:

    
    
      def fact(n):
          return eval('*'.join([str(x+1) for x in xrange(n)] + ['1']))

~~~
pmiller2
Here's a better one (taken from
<http://code.activestate.com/recipes/67668/#c7> ):

    
    
        f= lambda n: [[[j for j in (j*i,)][0] for i in range(2,n+1)][-1] for j in (1,)][0]

------
alec
Where's the one where the Python programmer has to make something go fast and
needs to write a C module?

~~~
jerf
That would be trickier than it looks. Run this in your python interpreter:

    
    
        reduce(lambda x, y: x * y, range(1, 100))
    

Replicating that in C is certainly possible but you're probably not going to
do it quickly, unless you're already pretty deep into that sort of thing.

~~~
jrockway
Clearly the answer is -1818210304. My C program told me that really quickly!

~~~
jerf
I stand corrected.

------
Deestan
Bored Python programmer:

    
    
      def fact(l):
          return sum(map(bool,reduce(lambda I,l:''.join([l for I in I]),
                                     ['+'+'+'*l for l in xrange(l)])))
      assert fact(6)==720

------
Groxx
I think I'll have to contribute some J code (can anyone uglify Python this
much?):

    
    
      (*$:@:<:)^:(1&<) 6
    

Finds the factorial recursively. J programmers are masochists.

(From: <http://rosettacode.org/wiki/Factorial_function#Recursive_11>)

------
ajross
The "first year pascal" example has a metasyntax error. It's missing a "#end"
token for symmetry with the C example.

------
Flow
So funny. The Windows one makes me wanna abandon computers altogether.

------
AndrewO
Doesn't PEP 20 say that there should be only one way to do it? :-)

~~~
sophacles
I know your question is a joke, but it is frequently asked in seriousness, so:
No. Not at this granularity. Otherwise the python community would still be
using plone, and Django would not exist. Or there would be only blocking (or
non-blocking) io. And so on.

------
whyenot
I don't understand. What's wrong with the factorial function that comes with
python?

    
    
        from math import factorial
    

done.

~~~
yan
You can't amuse thousands of techies with an import from a math library..

~~~
Luyt
But the 'Seasoned Hacker' from the original article does something likewise
with the Hello, World program:

    
    
      % cc -o a.out ~/src/misc/hw/hw.c
      % a.out
    

;-)

------
Confusion
I kind of miss an @memoized version

------
est
Python one-liner anyone?

    
    
        >>> reduce(int.__mul__, range(1,6))
        120

~~~
bugs
Range should go to 7 if we are doing factorial of 6.

~~~
est
Treat it as gamma function instead :)

------
brehaut
;; python programmer who reads HN and Proggit

(defn factorial [n] (reduce * (range 1 (inc n))))

~~~
Torn

      ;; Any programmer reading HN long enough to experiment with some lisp / clojure ;)
      (defn factorial [n]
        (apply * (range 1 (+ n 1))))

~~~
brehaut
I was torn between those two formulations, but decided on the reduce because
the academic purity of an implicit hylomorphism ;) However, your formulation
is more idiomatic of a lispy solution.

------
viraptor
"I've got a lot of memory" or "runtime is more importatant than
initialisation" programmer

    
    
        fact = init_fact()
        ...
        res = fact[x]

------
jbyers
Enterprise programmer made me laugh. Especially after spending a few days
buried in SAML.

------
yason
Heh, this is quite even better than the "Evolution of a programmer".

------
gregcmartin
I am going to have to go with Lazier Python programmer

------
jchonphoenix
Wonder what a mathematicians python looks like...

~~~
nick_maxwell
<http://news.ycombinator.com/item?id=1089936>

------
gcb
<http://www.willamette.edu/~fruehr/haskell/evolution.html>

for all the closet functional programmers around here :)

~~~
hyperbovine
_That's_ the link I was looking for, I knew had seen this somewhere before.
The Python one is entertaining, but the Haskell one is brilliant. "explicit
type recursion with functors and catamorphisms", ha. I finally learned what a
functor is a few days ago.

