电竞比分网-中国电竞赛事及体育赛事平台

分享

Python 程序員的進(jìn)化...

 ~水手~!! 2011-06-10

Python 程序員的進(jìn)化

3人收藏此文章, 收藏此文章 發(fā)表于4個月前 , 已有763次閱讀 共1個評論 3人收藏此文章

轉(zhuǎn)自:http://www.oschina.net/news/15319/evolution-of-a-python-programmer

在綜合資訊欄中看到這個帖子,覺得很有意思,轉(zhuǎn)上來,最后面加上些自己的知識補遺~~~

==========================================================================================

本文是從 The Evolution of a Python Programmer 這篇文章翻譯而來。

不久前,在互聯(lián)網(wǎng)上出現(xiàn)了一篇有趣的文章,講的是對于同一個問題,不同層次的程序員編出的Phthon代碼顯示出了不同的風(fēng)格,代碼都很簡單,有趣。這篇文章的原始出處在這里,我把它整理了一下,并修改了幾處錯誤。

編程新手

1
            2
            3
            4
            5
            6
            
def factorial(x):
            if x == 0:
            return 1
            else:
            return x * factorial(x - 1)
            print factorial(6)

一年編程經(jīng)驗(學(xué)Pascal的)

1
            2
            3
            4
            5
            6
            7
            8
            
def factorial(x):
            result = 1
            i = 2
            while i <= x:
            result = result * i
            i = i + 1
            return result
            print factorial(6)

一年編程經(jīng)驗(學(xué)C的)

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

一年編程經(jīng)驗(讀過 SICP)

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

一年編程經(jīng)驗(Python)

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

懶惰的Python程序員

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

更懶的Python程序員

1
            2
            
f = lambda x: x and x * f(x - 1) or 1
            print f(6)

Python 專家

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

Python 黑客

1
            2
            3
            4
            5
            6
            
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')

專家級程序員

1
            2
            
from c_math import fact
            print fact(6)

大英帝國程序員

1
            2
            
from c_maths import fact
            print fact(6)

Web 設(shè)計人員

1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            
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 程序員

1
            2
            3
            4
            
import os
            def fact(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
            
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)

企業(yè)級程序員

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
            
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))

====================================================================================

lambda使用:

python lambda是在python中使用lambda來創(chuàng)建匿名函數(shù),而用def創(chuàng)建的方法是有名稱的

    1 python lambda會創(chuàng)建一個函數(shù)對象,但不會把這個函數(shù)對象賦給一個標(biāo)識符,而def則會把函數(shù)對象賦值給一個變量。

    2 python lambda它只是一個表達(dá)式,而def則是一個語句。

下面是python lambda的格式

    lambda x:表達(dá)式

lambda表達(dá)式在“:”后只能有一個表達(dá)式。也就是說,在def中,用return可以返回的也可以放在lambda后面,不能用return返回的也不能定義在python lambda后面。因此,像if或for或print這種語句就不能用于lambda中,lambda一般只用來定義簡單的函數(shù)。

reduce()函數(shù)

 reduce(func,seq[,init]),用二元函數(shù)func對序列seq中的元素進(jìn)行處理,每次處理兩個數(shù)據(jù)項(一個是前次處理的結(jié)果,一個是序列中的下一個元素),如此反復(fù)的遞歸處理,最后對整個序列求出一個單一的返回值。

該函數(shù)最多3個參數(shù),第一個參數(shù)為二元函數(shù),第二個參數(shù)必須可迭代,可以沒有第三個參數(shù)

例如:

    reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])    計算的是1+2+3+4+5

    reduce(lambda x, y: x+y, [1, 2, 3, 4, 5], 2) 計算的是2+1+2+3+4+5

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多