当前位置: 首页 > news >正文

一个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的)

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

 
  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)

 
  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.         #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 程序员

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

相关文章:

  • 【游戏人生】一个游戏程序员的学习资料
  • AJAX入门---五步使用XMLHttpRequest对象
  • AJAX入门---AJAX操作HTML
  • AJAX入门--- XMLHttpRequest对象的属性和方法
  • mysql查询今天,昨天,近7天,近30天,本月,上一月数据
  • html中使用map标签,coords值怎么精确定位gif上的链接
  • host 配置不生效的解决办法
  • 淘宝rubygem镜像
  • Struts2 S2-020补丁绕过漏洞
  • Discuz爆破无视验证码
  • Codeigniter 利用加密Key(密钥)的对象注入漏洞
  • 内网渗透案例
  • Fckeditor漏洞利用总结
  • 彻底删除Web路径下所有”.svn”遗留文件
  • office 2013 word 打开就停止响应解决办法
  • ES6 ...操作符
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • javascript数组去重/查找/插入/删除
  • jdbc就是这么简单
  • nginx 配置多 域名 + 多 https
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • springboot_database项目介绍
  • Yeoman_Bower_Grunt
  • 番外篇1:在Windows环境下安装JDK
  • 力扣(LeetCode)21
  • 聊聊flink的TableFactory
  • 排序(1):冒泡排序
  • 判断客户端类型,Android,iOS,PC
  • 如何设计一个微型分布式架构?
  • 一个完整Java Web项目背后的密码
  • Android开发者必备:推荐一款助力开发的开源APP
  • Hibernate主键生成策略及选择
  • ​Linux·i2c驱动架构​
  • ​低代码平台的核心价值与优势
  • # Maven错误Error executing Maven
  • ###STL(标准模板库)
  • $().each和$.each的区别
  • (12)Linux 常见的三种进程状态
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (编译到47%失败)to be deleted
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (接口封装)
  • (算法设计与分析)第一章算法概述-习题
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (转)shell调试方法
  • .form文件_SSM框架文件上传篇
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .net 发送邮件
  • .NET企业级应用架构设计系列之技术选型
  • .sh 的运行
  • @SpringBootApplication 包含的三个注解及其含义
  • @德人合科技——天锐绿盾 | 图纸加密软件有哪些功能呢?
  • [ Linux ] git工具的基本使用(仓库的构建,提交)
  • [ vulhub漏洞复现篇 ] Celery <4.0 Redis未授权访问+Pickle反序列化利用
  • [2024最新教程]地表最强AGI:Claude 3注册账号/登录账号/访问方法,小白教程包教包会