程序员的进化

来源:互联网 发布:c语言输出乘法表 while 编辑:程序博客网 时间:2024/04/29 06:50

  不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的程序员编出的Python代码显示出了不同的风格,代码都很简单,有趣。我把它整理了一下,并修改了几处错误。

编程新手

1
2
3
4
5
6
deffactorial(x):
    ifx==0:
        return1
    else:
        returnx*factorial(x-1)
printfactorial(6)

一年编程经验(学Pascal的)

1
2
3
4
5
6
7
8
deffactorial(x):
    result=1
    i=2
    whilei <=x:
        result=result*i
        i=i+1
    returnresult
printfactorial(6)

一年编程经验(学C的)

1
2
3
4
5
6
7
8
9
deffact(x):#{
    result=i=1;
    while(i <=x):#{
        result*=i;
        i+=1;
    #}
    returnresult;
#}
print(fact(6))

一年编程经验(读过 SICP)

1
2
3
4
5
@tailcall
deffact(x, acc=1):
    if(x > 1):return(fact((x-1), (acc *x)))
    else:      returnacc
print(fact(6))

一年编程经验(Python)

1
2
3
4
5
6
defFactorial(x):
    res=1
    foriinxrange(2, x +1):
        res*=i
    returnres
printFactorial(6)

懒惰的Python程序员

1
2
3
deffact(x):
    returnx > 1andx *fact(x-1)or1
printfact(6)

更懒的Python程序员

1
2
f=lambdax: x andx*f(x-1)or1
printf(6)

Python 专家

1
2
fact=lambdax: reduce(int.__mul__,xrange(2, x +1),1)
printfact(6)

Python 黑客

1
2
3
4
5
6
importsys
@tailcall
deffact(x, acc=1):
    ifx:returnfact(x.__sub__(1), acc.__mul__(x))
    returnacc
sys.stdout.write(str(fact(6))+'\n')

专家级程序员

1
2
fromc_mathimportfact
printfact(6)

大英帝国程序员

1
2
fromc_mathsimportfact
printfact(6)

Web 设计人员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
deffactorial(x):
    #-------------------------------------------------
    #--- Code snippet from The Math Vault          ---
    #--- Calculate factorial (C) Arthur Smith 1999 ---
    #-------------------------------------------------
    result=str(1)
    i=1#Thanks Adam
    whilei <=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
    returnresult
printfactorial(6)

Unix 程序员

1
2
3
4
importos
deffact(x):
    os.system('factorial ' + str(x))
fact(6)

Windows 程序员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
NULL=None
defCalculateAndPrintFactorialEx(dwNumber,
                                 hOutputDevice,
                                 lpLparam,
                                 lpWparam,
                                 lpsscSecurity,
                                 *dwReserved):
    iflpsscSecurity !=NULL:
        returnNULL#Not implemented
    dwResult=dwCounter=1
    whiledwCounter <=dwNumber:
        dwResult*=dwCounter
        dwCounter+=1
    hOutputDevice.write(str(dwResult))
    hOutputDevice.write('\n')
    return1
importsys
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL,
 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

企业级程序员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
defnew(cls,*args,**kwargs):
    returncls(*args,**kwargs)
  
classNumber(object):
    pass
  
classIntegralNumber(int, Number):
    deftoInt(self):
        returnnew (int,self)
  
classInternalBase(object):
    def__init__(self, base):
        self.base=base.toInt()
  
    defgetBase(self):
        returnnew (IntegralNumber, self.base)
  
classMathematicsSystem(object):
    def__init__(self, ibase):
        Abstract
  
    @classmethod
    defgetInstance(cls, ibase):
        try:
            cls.__instance
        exceptAttributeError:
            cls.__instance=new (cls, ibase)
        returncls.__instance
  
classStandardMathematicsSystem(MathematicsSystem):
    def__init__(self, ibase):
        ifibase.getBase() !=new (IntegralNumber, 2):
            raiseNotImplementedError
        self.base=ibase.getBase()
  
    defcalculateFactorial(self, target):
        result=new (IntegralNumber, 1)
        i=new (IntegralNumber, 2)
        whilei <=target:
            result=result*i
            i=i+new (IntegralNumber, 1)
        returnresult
  
printStandardMathematicsSystem.getInstance(new (InternalBase,
new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))

英文原文链接:The Evolution of a Python Programmer


原创粉丝点击