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. Hdu1096

    #include <stdio.h> int main() { int T,n; ; while(scanf("%d",&T)!=EOF){ while(sca ...

  2. DataTables选择多行

    $(document).ready(function() { var table = $('#example').DataTable(); $('#example tbody').on( 'click ...

  3. 全局函数的Result一定要每次都初始化,否则上次的结果会被保持到下一次继续使用

    测试半天,原来是因为这个原因.下面例子中,Result:=''必须写,否则其结果会被累计,真是昏倒!! function MyPaths(tache: IXMLTaskType) : String; ...

  4. 链接一个外部lib库的时候注意事项

    1.注意这个库是Debug版还是Release版,一般windows下,约定是Debug版的库文件名会加个d. 2.注意这个库是x86还是x64版本. 3.注意生成这个lib库的是什么编译器

  5. C3P0连接池详细配置

    C3P0连接池详细配置 转自http://msq.javaeye.com/blog/60387 <c3p0-config> <default-config> <!--当连 ...

  6. 杭电2059(dp)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. Java学习作业(14.4.21)

    前三次作业都是基础语法.真的好水啊.从这次开始记录. 1.编写Java程序,把当前目录下扩展名为txt的文件的扩展名全部更名为back. import java.io.*; import java.l ...

  8. 表单javascript checkbox全选 反选 全不选

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  9. AFNetwork 作用和使用方法具体解释

    转自:http://www.maxiaoguo.com/clothes/269.html AFNetworking是一个轻量级的iOS网络通信类库.它建立在NSURLConnection和NSOper ...

  10. 十一招解决:系统IE部分网页打不开怎么办(转载)

    网页打不开这问题,却实非常令人头痛,问过非常多人,都说不出真正的理由和解决方法.以下是在网络上面搜集的一些针对“网页打不开怎么办”解决方法,共十一条,希望可以对大家有帮助. Application M ...