一个Python程序员的进化
Post by
yaohuaq
  2011-02-18 13:06:01 Friday Tags:python,程序员

转:http://bbs.chinaunix.net/thread-1857298-1-1.html

不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的Python程序员编出的Python代码显示出了不同的风格,代码都很简单,有趣。

编程新手

Python代码
  1. def factorial(x):        
  2.   if x == 0:            
  3.     return 1        
  4.   else:            
  5.     return x * factorial(x - 1)    
  6. print factorial(6)  


一年编程经验(学Pascal的)

Python代码
  1. def factorial(x):        
  2.   result = 1       
  3.   i = 2       
  4.   while i <= x:            
  5.     resultresult = result * i            
  6.     ii = i + 1        
  7.   return result    
  8. print factorial(6)   

一年编程经验(学C的)
Python代码
  1. def fact(x): #{        
  2.   result = i = 1;        
  3.   while (i <= x): #{            
  4.     result *= i;            
  5.     i += 1;        
  6.   #}        
  7.   return result;    
  8. #}    
  9. print(fact(6))   

一年编程经验(读过 SICP)
Python代码
  1. @tailcall  def fact(x, acc=1):        
  2.   if (x > 1): return (fact((x - 1), (acc * x)))        
  3. else:         
  4.   return acc    
  5. print(fact(6))   

一年编程经验(Python)
Python代码
  1. def Factorial(x):        
  2.   res = 1       
  3.   for i in xrange(2, x + 1):            
  4.     res *= i        
  5.   return res    
  6. print Factorial(6)   


懒惰的Python程序员

Python代码
  1. def fact(x):        
  2.   return x > 1 and x * fact(x - 1or 1    
  3. print fact(6)   

 

更懒的Python程序员

Python代码
  1. f = lambda x: x and x * f(x - 1or 1    
  2. print f(6)   

 

Python 专家

Python代码
  1. fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)    
  2. print fact(6)   

 

Python 黑客

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

 

专家级程序员

Python代码
  1. from c_math import fact  print fact(6)   

 

大英帝国程序员

Python代码
  1. from c_maths import fact  print fact(6)   

 

Web 设计人员

Python代码
  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  print factorial(6)   

 

Unix 程序员

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

 

Windows 程序员

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

 

企业级程序员

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

评论:
发表评论:
姓名:
联系方法:(选填)
评论内容:
个人信息
迎接
昵称:C7_yaohuaq
QQ:88523499
Email:c77cc#vip.qq.com
居住地:北京 海淀
星座:天蝎
毕业院校:广西民族大学
工作点:喜讯无限
最新评论