搞笑的程序猿:看看你是哪种Python程序员


Posted in Python onJune 12, 2015

不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的Python程序员编出的Python代码,显示出了不同的风格,代码都很简单,有趣。下面让我们一起来看看一个Python程序猿进阶的全过程吧。(偷笑)

编程新手

def factorial(x):  
  if x == 0:  
    return 1  
  else:  
    return x * factorial(x - 1) //不简单啊,迭代,新手哦。 
print factorial(6)

一年编程经验(学Pascal的)

def factorial(x):  
  result = 1  
  i = 2  
  while i <= x:  
    resultresult = result * i  
    ii = i + 1  
  return result  
print factorial(6)

一年编程经验(学C的)

def fact(x): #{  
  result = i = 1;  
  while (i <= x): #{  
    result *= i;  
    i += 1;  
  #}  
  return result;  
#}  
print(fact(6))

一年编程经验(读过SICP)

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

一年编程经验(Python)

def Factorial(x):  
  res = 1  
  for i in xrange(2, x + 1):  
    res *= i  
    return res  
 print Factorial(6)

懒惰的Python程序员

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

更懒的Python程序员

f = lambda x: x and x * f(x - 1) or 1 //匿名函数,厉害。程序猿真是懒人做的! 
print f(6)

Python专家

fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)  
print fact(6)               //专家厉害啊。

 Python黑客

import sys  
@tailcall  
def fact(x, acc=1):  
  if x: return fact(x.__sub__(1), acc.__mul__(x))  
  return acc  
sys.stdout.write(str(fact(6)) + '\n') //一般人压根看不懂。

专家级程序员

from c_math import fact  
print fact(6)

大英帝国程序员

from c_maths import fact  
print fact(6)  
Web设计人员
def factorial(x):  
  #-------------------------------------------------  
  #--- Code snippet from The Math Vault     ---  
  #--- Calculate factorial (C) Arthur Smith 1999 ---  
  #-------------------------------------------------  
  result = str(1)  
  i = 1 #Thanks Adam  
  while i <= x:  
    #result = result * i #It's faster to use *=  
    #result = str(result * result + i)  
      #result = int(result *= i) #??????  
    result = str(int(result) * i)  
    #result = int(str(result) * i)  
    i = i + 1  
  return result  
print factorial(6)

Unix 程序员

import os  
def fact(x):  
  os.system('factorial ' + str(x))  
fact(6)

Windows 程序员

NULL = None  
def CalculateAndPrintFactorialEx(dwNumber,  
                 hOutputDevice,  
                 lpLparam,  
                 lpWparam,  
                 lpsscSecurity,  
                 *dwReserved):  
  if lpsscSecurity != NULL:  
    return NULL #Not implemented  
  dwResult = dwCounter = 1  
  while dwCounter <= dwNumber:  
    dwResult *= dwCounter  
    dwCounter += 1  
  hOutputDevice.write(str(dwResult))  
  hOutputDevice.write('\n')  
  return 1  
import sys  
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL,  
 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) //可能自己都晕菜了...

企业级程序员

def new(cls, *args, **kwargs):  
  return cls(*args, **kwargs)  
   
class Number(object):  
  pass  
   
class IntegralNumber(int, Number):  
  def toInt(self):  
    return new (int, self)  
   
class InternalBase(object):  
  def __init__(self, base):  
    self.base = base.toInt()  
   
  def getBase(self):  
    return new (IntegralNumber, self.base)  
   
class MathematicsSystem(object):  
  def __init__(self, ibase):  
    Abstract  
  
  @classmethod  
  def getInstance(cls, ibase):  
    try:  
      cls.__instance  
    except AttributeError:  
      cls.__instance = new (cls, ibase)  
    return cls.__instance  
   
class StandardMathematicsSystem(MathematicsSystem):  
  def __init__(self, ibase):  
    if ibase.getBase() != new (IntegralNumber, 2):  
      raise NotImplementedError  
    self.base = ibase.getBase()  
   
  def calculateFactorial(self, target):  
    result = new (IntegralNumber, 1)  
    i = new (IntegralNumber, 2)  
    while i <= target:  
      result = result * i  
      i = i + new (IntegralNumber, 1)  
    return result  
   
print StandardMathematicsSystem.getInstance(new (InternalBase,  
new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6)) //面向对象,但就此题来说,又长又臭。
Python 相关文章推荐
python基础教程之缩进介绍
Aug 29 Python
python 数据清洗之数据合并、转换、过滤、排序
Feb 12 Python
python读取文本中的坐标方法
Oct 14 Python
Pandas Shift函数的基础入门学习笔记
Nov 16 Python
python合并已经存在的sheet数据到新sheet的方法
Dec 11 Python
Python实现的字典排序操作示例【按键名key与键值value排序】
Dec 21 Python
使用pyqt5 tablewidget 单元格设置正则表达式
Dec 13 Python
Django使用list对单个或者多个字段求values值实例
Mar 31 Python
Pandas实现一列数据分隔为两列
May 18 Python
Python使用plt.boxplot() 参数绘制箱线图
Jun 04 Python
Python中三维坐标空间绘制的实现
Sep 22 Python
Django-simple-captcha验证码包使用方法详解
Nov 28 Python
Python 3.x 新特性及10大变化
Jun 12 #Python
Python实现比较两个列表(list)范围
Jun 12 #Python
在Linux系统上安装Python的Scrapy框架的教程
Jun 11 #Python
Python语言实现机器学习的K-近邻算法
Jun 11 #Python
在Linux下使用Python的matplotlib绘制数据图的教程
Jun 11 #Python
python中的代码编码格式转换问题
Jun 10 #Python
python实现数独算法实例
Jun 09 #Python
You might like
德生H-501的评价与改造
2021/03/02 无线电
使用GD库生成带阴影文字的图片
2015/03/27 PHP
Symfony2中被遗弃的getRequest()方法分析
2016/03/17 PHP
php利用imagemagick实现复古老照片效果实例
2017/02/16 PHP
PHP array_shift()用法实例分析
2019/01/07 PHP
js实现一个省市区三级联动选择框代码分享
2013/03/06 Javascript
浅析jQuery中常用的元素查找方法总结
2013/07/04 Javascript
js 获取页面高度和宽度兼容 ie firefox chrome等
2014/05/14 Javascript
JavaScript使用setTimeout实现延迟弹出警告框的方法
2015/04/07 Javascript
jQuery中animate动画第二次点击事件没反应
2015/05/07 Javascript
jQuery height()、innerHeight()、outerHeight()函数的区别详解
2016/05/23 Javascript
浅谈时钟的生成(js手写简洁代码)
2016/08/20 Javascript
基于JS实现横线提示输入验证码随验证码输入消失(js验证码的实现)
2016/10/27 Javascript
AngularJS遍历获取数组元素的方法示例
2017/11/11 Javascript
js读取本地文件的实例
2017/12/22 Javascript
vue实例中data使用return包裹的方法
2018/08/27 Javascript
Net微信网页开发 使用微信JS-SDK获取当前地理位置过程详解
2019/08/26 Javascript
vue.js页面加载执行created,mounted的先后顺序说明
2020/11/07 Javascript
微信小程序实现列表左右滑动
2020/11/19 Javascript
python提示No module named images的解决方法
2014/09/29 Python
python对url格式解析的方法
2015/05/13 Python
python3使用requests模块爬取页面内容的实战演练
2017/09/25 Python
python reverse反转部分数组的实例
2018/12/13 Python
Python函数中不定长参数的写法
2019/02/13 Python
详解Python 解压缩文件
2019/04/09 Python
windows下Python安装、使用教程和Notepad++的使用教程
2019/10/06 Python
在OpenCV里实现条码区域识别的方法示例
2019/12/04 Python
Python读取图像并显示灰度图的实现
2020/12/01 Python
深入浅析css3 border-image边框图像详解
2015/11/24 HTML / CSS
CSS3实现翘边的阴影效果的代码示例
2016/06/13 HTML / CSS
Waterford加拿大官方网站:世界著名的水晶杯品牌
2016/11/01 全球购物
New delete 与malloc free 的联系与区别
2013/02/04 面试题
团支书的期末学习总结自我评价
2013/11/01 职场文书
2015年社区计生工作总结
2015/04/21 职场文书
驳回起诉民事裁定书
2015/05/19 职场文书
党小组评议意见
2015/06/02 职场文书