一段Py脚本的进化

Heroboy  <[email protected]>
sender-time     Sent at 15:57 (GMT+08:00). Current time there: 9:05 PM. ✆
reply-to        [email protected]
to      [email protected]
date    Sat, Jan 30, 2010 at 15:57
subject [CPyUG] Evolution of a Python programmer.py
发源

Newbie

   1 #Newbie programmer
   2 def factorial(x):
   3     if x == 0:
   4         return 1
   5     else:
   6         return x * factorial(x - 1)
   7 print factorial(6)

studied Pascal

   1 #First year programmer, studied Pascal
   2 def factorial(x):
   3     result = 1
   4     i = 2
   5     while i <= x:
   6         result = result * i
   7         i = i + 1
   8     return result
   9 print factorial(6)

studied C

   1 #First year programmer, studied C
   2 def fact(x): #{
   3     result = i = 1;
   4     while (i <= x): #{
   5         result *= i;
   6         i += 1;
   7     #}
   8     return result;
   9 #}
  10 print(fact(6))

SCIP

   1 #First year programmer, SICP
   2 @tailcall
   3 def fact(x, acc=1):
   4     if (x > 1): return (fact((x - 1), (acc * x)))
   5     else: return acc
   6 print(fact(6))

Python

   1 #First year programmer, Python
   2 def Factorial(x):
   3     res = 1
   4     for i in xrange(2, x + 1):
   5         res *= i
   6     return res
   7 print Factorial(6)

== Lazy Py ==

   1 #Lazy Python programmer
   2 def fact(x):
   3     return x > 1 and x * fact(x - 1) or 1
   4 print fact(6)

Lazier

   1 #Lazier Python programmer
   2 f = lambda x: x and x * f(x - 1) or 1
   3 print f(6)

expert Py

   1 #Python expert programmer
   2 import operator as op
   3 import functional as f
   4 fact = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
   5 print fact(6)

hacker

   1 #Python hacker
   2 import sys
   3 @tailcall
   4 def fact(x, acc=1):
   5     if x: return fact(x.__sub__(1), acc.__mul__(x))
   6     return acc
   7 sys.stdout.write(str(fact(6)) + '\n')

EXPERT PROGRAMMER

   1 #EXPERT PROGRAMMER
   2 import c_math
   3 fact = c_math.fact
   4 print fact(6)

ENGLISH EXPERT PROGRAMMER

   1 #ENGLISH EXPERT PROGRAMMER
   2 import c_maths
   3 fact = c_maths.fact
   4 print fact(6)

Web designer

   1 def factorial(x):
   2     #-------------------------------------------------
   3     #--- Code snippet from The Math Vault ---
   4     #--- Calculate factorial (C) Arthur Smith 1999 ---
   5     #-------------------------------------------------
   6     result = str(1)
   7     i = 1 #Thanks Adam
   8     while i <= x:
   9         #result = result * i #It's faster to use *=
  10         #result = str(result * result + i)
  11            #result = int(result *= i) #??????
  12         result str(int(result) * i)
  13         #result = int(str(result) * i)
  14         i = i + 1
  15     return result
  16 print factorial(6)

Unix programmer

   1 import os
   2 def fact(x):
   3     os.system('factorial ' + str(x))
   4 fact(6)

Windows programmer

   1 NULL = None
   2 def CalculateAndPrintFactorialEx(dwNumber,
   3                                  hOutputDevice,
   4                                  lpLparam,
   5                                  lpWparam,
   6                                  lpsscSecurity,
   7                                  *dwReserved):
   8     if lpsscSecurity != NULL:
   9         return NULL #Not implemented
  10     dwResult = dwCounter = 1
  11     while dwCounter <= dwNumber:
  12         dwResult *= dwCounter
  13         dwCounter += 1
  14     hOutputDevice.write(str(dwResult))
  15     hOutputDevice.write('\n')
  16     return 1
  17 import sys
  18 CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

Enterprise programmer

   1 def new(cls, *args, **kwargs):
   2     return cls(*args, **kwargs)
   3  
   4 class Number(object):
   5     pass
   6  
   7 class IntegralNumber(int, Number):
   8     def toInt(self):
   9         return new (int, self)
  10  
  11 class InternalBase(object):
  12     def __init__(self, base):
  13         self.base = base.toInt()
  14  
  15     def getBase(self):
  16         return new (IntegralNumber, self.base)
  17  
  18 class MathematicsSystem(object):
  19     def __init__(self, ibase):
  20         Abstract
  21  
  22     @classmethod
  23     def getInstance(cls, ibase):
  24         try:
  25             cls.__instance
  26         except AttributeError:
  27             cls.__instance = new (cls, ibase)
  28         return cls.__instance
  29  
  30 class StandardMathematicsSystem(MathematicsSystem):
  31     def __init__(self, ibase):
  32         if ibase.getBase() != new (IntegralNumber, 2):
  33             raise NotImplementedError
  34         self.base = ibase.getBase()
  35  
  36     def calculateFactorial(self, target):
  37         result = new (IntegralNumber, 1)
  38         i = new (IntegralNumber, 2)
  39         while i <= target:
  40             result = result * i
  41             i = i + new (IntegralNumber, 1)
  42         return result
  43  
  44 print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))

机械唯物主义

{{{机械唯物主义 <[email protected]> sender-time Sent at 21:05 (GMT+08:00). Current time there: 9:16 PM. ✆ reply-to [email protected] to [email protected] date Sat, Jan 30, 2010 at 21:05 }}} 这个讽刺的东西可真多。。。

我的版本:

   1 def fact(x):
   2    result = 1
   3    for i in range(x):
   4        result *= i+1
   5    return result
   6 
   7 fact(6)


反馈

创建 by -- ZoomQuiet [2010-01-30 13:10:49]

MiscItems/2010-01-30 (last edited 2010-01-30 13:17:34 by ZoomQuiet)