程序员的进化

简介: 不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的程序员编出的Phthon代码显示出了不同的风格,代码都很简单,有趣。这篇文章的原始出处在这里,我把它整理了一下,并修改了几处错误。

不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的程序员编出的Phthon代码显示出了不同的风格,代码都很简单,有趣。这篇文章的原始出处在这里,我把它整理了一下,并修改了几处错误。

编程新手

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

一年编程经验(学Pascal的)

 
  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) 

一年编程经验(读过 SICP)

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

一年编程经验(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程序员

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

更懒的Python程序员

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

Python 专家

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

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

专家级程序员

 
  1. from c_math import fact  
  2. print fact(6) 

大英帝国程序员

 
  1. from c_maths import fact  
  2. print fact(6) 

Web 设计人员

 
  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.         #resultresult = 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.         ii = i + 1  
  15.     return result  
  16. print factorial(6) 

Unix 程序员

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

Windows 程序员

 
  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,  
  19.  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) 

企业级程序员

 
  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.             resultresult = result * i  
  41.             ii = i + new (IntegralNumber, 1)  
  42.         return result  
  43.    
  44. print StandardMathematicsSystem.getInstance(new (InternalBase,  
  45. new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6)) 

译文链接:http://www.aqee.net/2011/02/09/the-evolution-of-a-python-programmer/

原文链接:程序员的进化

目录
相关文章
|
4天前
|
机器学习/深度学习 设计模式 SQL
编程之路上的飞跃:那些让我技能显著提升的关键概念与技术
编程之路上的飞跃:那些让我技能显著提升的关键概念与技术
21 10
|
9月前
|
人工智能 自然语言处理 程序员
ChatGPT 的出现会导致底层程序员失业吗?
ChatGPT 的出现会导致底层程序员失业吗?
|
9月前
|
设计模式 算法 程序员
培养编程思维的关键——从最基础开始
在当今信息时代,编程已经成为一项不可或缺的技能。而要成为一名优秀的程序员,除了掌握具体的编程语言和工具,更重要的是培养良好的编程思维。本文将从最最基础的层面入手,探讨如何培养编程思维。
154 0
|
11月前
|
设计模式 算法 程序员
代码能力,程序员自我修养之基石
提高代码能力不是一蹴而就的事,需要我们不断努力,通过持续学习和练习、参与开源项目、阅读优秀的代码、与他人合作、提升解决问题的能力等方式,提高自己的代码能力,为自己为公司创造价值。
169 0
代码能力,程序员自我修养之基石
|
机器学习/深度学习 人工智能 Java
我们都在努力做自己,我的编程学习之路分享
我们都在努力做自己,我的编程学习之路分享
169 0
我们都在努力做自己,我的编程学习之路分享
|
敏捷开发 前端开发 架构师
程序员自我发展之路:从态度到方法
程序员自我发展之路:从态度到方法
115 0
程序员自我发展之路:从态度到方法
|
设计模式 架构师 NoSQL
从技术思维角度聊一聊『程序员』摆地摊的正确姿势
有人说程序员这个职业,三年升高工,七年做架构,十年送外卖。对此虽然我也曾非常认可,但现在我可以前瞻性(马后炮)地说四个字,杞人忧天!目光肤浅!正所谓天生我材必有用,用完再把外卖送,现在,新的风口——万亿【烟火经济】来了,除了送外卖我们又多了个新选择:摆地摊!一个人一辈子只有那么几次机会可能实现财务自由,机遇稍纵即逝,一定要牢牢把握住。
1008 0
|
设计模式 负载均衡 算法
从技术思维角度聊一聊,『程序员』摆地摊的正确姿势
有人说程序员这个职业,三年升高工,七年做架构,十年送外卖。对此虽然我也曾非常认可,但现在我可以前瞻性(马后炮)地说四个字,杞人忧天
|
设计模式 算法 网络协议
自学编程的八大误区!克服它!
关于“自学编程的一些常见误区”这个话题其实很早之前就在视频里聊过了。时间过去了大半年,也还是有很多小伙伴会提及各种自学过程中的常见疑惑,所以还是用文字总结一下这几点想法,和大家共勉。
自学编程的八大误区!克服它!
|
测试技术 程序员
那些会阻碍程序员成长的细节[1]
罗马非一日建成,软件系统也不是一天能够写出来的,在经年累月的编码生活中,总会有那么些个不经意的瞬间暴露出来,而这些不经意的外在表现日积月累,犹如水滴石穿,会产生巨大的力量反作用于程序员的成长。我简单列了几条,你来看一看,兴许就在身边实实在在发生过。
1108 0

热门文章

最新文章