一个Python程序员的进化
2015-03-18 10:32
495 查看
不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的Python程序员编出的Python代码显示出了不同的风格,代码都很简单,有趣。
编程新手
一年编程经验(学Pascal的)
一年编程经验(学C的)
一年编程经验(读过SICP)
一年编程经验(Python)
懒惰的Python程序员
更懒的Python程序员
Python专家
Python黑客
专家级程序员
大英帝国程序员
Web设计人员
Unix程序员
企业级程序员
view
source
print?
编程新手
1 | def factorial(x): |
2 | if x = = 0 : |
3 | return 1 |
4 | else : |
5 | return x * factorial(x - 1 ) / / 递归!不简单啊! |
6 | factorial( 6 ) |
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 | factorial( 6 ) |
1 | def fact(x): #{ |
2 | result = i = 1 ; / / 连续赋值! |
3 | while (i< = x): #{ |
4 | result * = i; |
5 | i + = 1 ; |
6 | #} |
7 | return result; |
8 | #} |
9 | (fact( 6 )) |
1 | @tailcall |
2 | def fact(x, acc = 1 ): |
3 | if (x> 1 ): return (fact((x - 1 ), (acc * x))) / / 递归! |
4 | else : return acc |
5 | (fact( 6 )) |
1 | def Factorial(x): |
2 | res = 1 |
3 | for i in xrange ( 2 , x + 1 ): / / 工具! |
4 | res * = i |
5 | return res |
6 | Factorial( 6 ) |
1 | def fact(x): |
2 | return x> 1 and x * fact(x - 1 ) or 1 / / 语法! |
3 | fact( 6 ) |
1 | f = lambda x:x and x * f(x - 1 ) or 1 / / 匿名函数 + 语法! |
2 | f( 6 ) |
1 | fact = lambda x: reduce ( int .__mul__, xrange ( 2 , x + 1 ), 1 ) / / 匿名函数 + 工具 + 底层! |
2 | fact( 6 ) |
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 | fact( 6 ) |
1 | from c_maths import fact |
2 | fact( 6 ) |
01 | def factorial(x): |
02 | #------------------------------------------------- |
03 | #--- |
04 | #--- |
05 | #------------------------------------------------- |
06 | result = str ( 1 ) |
07 | i = 1 #Thanks Adam |
08 | while i< = x: |
09 | #result=result*i#It'sfastertouse*= |
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 | factorial( 6 ) |
01 | import os |
02 | def fact(x): |
03 | os.system( 'factorial ' +str(x))//代码量少! |
04 | fact( 6 ) |
05 | Windows 程序员 |
06 | NULL = None |
07 | def CalculateAndPrintFactorialEx(dwNumber, |
08 | hOutputDevice, |
09 | lpLparam, |
10 | lpWparam, |
11 | lpsscSecurity, |
12 | *dwReserved): |
13 | if lpsscSecurity NULL : |
14 | return NULL #Not implemented |
15 | dwResult 1 |
16 | while dwCounter |
17 | dwResult |
18 | dwCounter 1 |
19 | hOutputDevice.write(str(dwResult)) |
20 | hOutputDevice.write( '\n' ) |
21 | return 1 |
22 | import sys |
23 | CalculateAndPrintFactorialEx( 6 ,sys.stdout, NULL , NULL , NULL ,//自个都晕! |
24 | NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ) |
view
source
01 | def new( cls , * args, * * kwargs): |
02 | return cls ( * args, * * kwargs) |
03 | class Number( object ): |
04 | pass |
05 | class IntegralNumber( int ,Number): |
06 | def toInt( self ): |
07 | return new ( int , self ) |
08 | class InternalBase( object ): |
09 | def __init__( self ,base): |
10 | self .base = base.toInt() |
11 | def getBase( self ): |
12 | return new (IntegralNumber, self .base) |
13 | class MathematicsSystem( object ): |
14 | def __init__( self ,ibase): |
15 | Abstract |
16 | @classmethod |
17 | def getInstance( cls ,ibase): |
18 | try : |
19 | cls .__instance |
20 | except AttributeError: |
21 | cls .__instance = new ( cls ,ibase) |
22 | return cls .__instance |
23 | class StandardMathematicsSystem(MathematicsSystem): |
24 | def __init__( self ,ibase): |
25 | if ibase.getBase()! = new (IntegralNumber, 2 ): |
26 | raise NotImplementedError |
27 | self .base = ibase.getBase() |
28 | def calculateFactorial( self ,target): |
29 | result = new (IntegralNumber, 1 ) |
30 | i = new (IntegralNumber, 2 ) |
31 | while i< = target: |
32 | result = result * i |
33 | i = i + new (IntegralNumber, 1 ) |
34 | return result |
35 | StandardMathematicsSystem.getInstance(new (InternalBase, |
36 | new (IntegralNumber, 2 ))).calculateFactorial(new (IntegralNumber, 6 )) / / 面向对象! |
相关文章推荐
- 一个Python程序员的进化
- 一个Python程序员的进化
- 一个Python程序员的进化
- 开发者的发展 一个 Python 程序员的进化
- 一个Python程序员的进化(转载)
- 一个Python程序员的进化
- 一个Python程序员的进化
- 一个Python程序员的进化(转)
- 一个Python程序员的进化
- 一个Python程序员的进化【转】
- 一个Python程序员的进化
- 一个Python程序员的进化史
- 一个Python程序员的进化
- 一个Python程序员的进化
- 趣文:Python程序员的进化史
- 一个 Python 程序每一行的内存占用分析- 杨超(星辰海 | 真人图书馆·Python 程序员) - 42qu.com
- 分享:趣文:Python程序员的进化史
- [Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …
- 从程序看python程序员的进化
- 一个python程序员的ruby三日游(三)——构建工具