python笔记之编程风格大比拼

    虽然我的python age并不高,但我仍然愿意将我遇到的或者我写的有趣的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:
result = result * i
i = 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(

第一年刚学完Python的新手

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

爱偷懒的程序员

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 专家

import operator as op
import functional as f
fact = lambda x: f.foldl(op.mul, 1, xrange(2, x + 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')

专家级程序员

import c_math
fact = c_math.fact
print fact(6)

英语系的专家级程序员

import c_maths
fact = c_maths.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, 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笔记之编程风格大比拼的更多相关文章

  1. Python笔记002-Python编程基础概念

    第二章(1):Python编程基础概念 1. Python 程序的构成 Python 程序有模块组成.一个模块对应 Python 源文件,一般后缀名是:.py. 模块有语句组成.运行 Python程序 ...

  2. python 笔记4-- 函数式编程

    高阶函数 把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式. 在python中 函数也是一种变量 def add(x, y, f): return f(x) + f( ...

  3. Python笔记-IO编程

    IO在计算机中是指input和output(数据输入与输出),涉及到数据交换(磁盘.网络)的地方就需要IO接口. 输入流input stream是指数据从外面(磁盘.网络服务器)流入内存:输出流out ...

  4. python笔记 面向对象编程从入门到高级

    目录: 一.概念 二.方法    2.1组合 2.2继承 2.3多态 2.4封装 2.5归一化设计 三.面向对象高级   3.1   反射(自省) 3.2   内置方法__getatter__, __ ...

  5. Python笔记-面向对象编程

    1.类和实例 面向-对象的三大特点:数据封装.继承和多态 在Python中,所有数据类型都可以视为对象,当然也可以自定义对象.自定义的对象数据类型就是面向对象中的类(Class)的概念. 假设我们要处 ...

  6. python笔记--socket编程

    socket编程 osi七层模型 socket Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族 ...

  7. angular2.0学习笔记6.编程风格指南

    1.组件的类名应该是大驼峰形式,并且以Component结尾. 因此英雄详情组件的类名是HeroDetailComponent. 组件的文件名应该是小写中线形式,每个单词之间用中线分隔,并且以.com ...

  8. 《EMCAScript6入门》读书笔记——24.编程风格

  9. Python笔记(二)

    python笔记 函数式编程 函数 函数是Python内建支持一种封装(将大段代码拆成函数) 通过函数的调用,可以将复制的任务分解. 函数式编程(Functional Programming) 计算机 ...

随机推荐

  1. 根据文字计算Label的尺寸

    CGSize size = [self.username.text boundingRectWithSize:(CGSize){130,20} options:NSStringDrawingUsesL ...

  2. Hibernate整合Struts2时报错

    今天在整合Hibernate和Struts2的时候遇到一个问题 总是出现各种异常,经过仔细检查,代码本身并没有问题, ----------------------------------------- ...

  3. mysql order by 妙用

    今天在做一个2次开发的时候,出现一个需求, 需要在商品分类页里面带一个参数,也就是商品ID, 如果分类链接里面有这个ID的时候就需要把这个商品排在分类商品列表的第1个, 原来的思路是,选择分类后,在P ...

  4. VC6-Win7下VC++6.0打开多个工程的设置

    在Win7操作系统下,如果是以Administrator登陆,则VC6.0打开工程文件的时候,不能同时打开多个工程文件,后打开的工程会将前一个工程close掉,这样,VC6.0只能出现一个进程.在xp ...

  5. GridView 设置背景透明以及Item的点击动画

    //将点击时的背景色设置为透明 gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); 此时点击GridView的每个Item就不会出现 ...

  6. cf D. Alternating Current

    http://codeforces.com/contest/344/problem/D #include <cstdio> #include <cstring> #includ ...

  7. C# LinkButton 带参数的OnCommand事件的写法

    前台: <asp:TemplateField HeaderText ="操作"> <HeaderStyle HorizontalAlign ="Cent ...

  8. Powershell---1 介绍和安装

    Powershell 介绍和安装   Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境.你可以把它看成是命令行提示符cmd.exe的扩充,不对,应当是颠覆 ...

  9. 错误:指定的任务可执行文件位置 D:\Android\platform-tools\aapt.exe 无效

    android-apt-compiler: Cannot run program "D:\android-sdk\platform-tools\aapt 装上IntelliJ IDEA /下 ...

  10. jsp中全局变量和局部变量的设置