Python程序员的进化

marz posted @ Aug 11, 2010 06:41:35 AM in python with tags python , 1911 readers
 从 酷壳 - CoolShell.cn 作者:耗子

有 92 人喜欢此条目

以前本站发布过一篇《程序员的进化》,以一种幽默的代码展现方式调侃了程序。下面这篇是关于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(6))

第一年刚学完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))

 

摘自:来源

 

  • No match
meidir said:
Feb 11, 2023 12:57:09 AM

I love forgathering useful info, this post has got me even more info! SaaS Lawyer

 

 

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

 

 

very nice post, i definitely really like this website, carry on it SaaS Attorney

meidir said:
Feb 14, 2023 04:55:13 PM

I love forgathering useful info, this post has got me even more info! SaaS Lawyer

meidir said:
Feb 17, 2023 09:05:59 PM

Your website does not show up correctly on my apple iphone – you might want to try and fix that privacidad

 

 

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

 

 

Its excellent as your other blog posts : D, thankyou for putting up. página de apuestas deportivas

meidir said:
Feb 20, 2023 05:52:33 PM

i think that everyone have a fear of public speaking in one way or another:: most trusted career counsellor in Delhi, Noida, Gurgaon

meidir said:
Feb 21, 2023 09:31:24 PM

You should take part in a contest for one of the best blogs on the web. I will recommend this page! florisis cluj

meidir said:
Feb 25, 2023 10:33:35 PM

I’m impressed, I have to admit. Truly rarely must i encounter a weblog that’s both educative and entertaining, and without a doubt, you could have hit the nail for the head. Your notion is outstanding; the pain is a thing that too little people are speaking intelligently about. I’m very happy that we came across this during my try to find something about it. comprobarseo

 

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

 

I’m curious to find out what blog platform you happen to be utilizing? I’m experiencing some small security issues with my latest website and I’d like to find something more secure. Do you have any recommendations? metricasweb.com

 

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

 

Solid post! I honestly wasn’t aware of this. It is a relief to read because I get so disappointed when writers put no thought into their work. It is obvious that you know what you’re writing about. I’ll definitely visit again! naproxeno para que sirve

meidir said:
Feb 28, 2023 03:49:31 PM

great issues altogether, you simply won brand new reader. What may you recommend about your put up that you made a few days ago? Any positive? JWH-210 Powder

 

 

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

 

 

It’s rare knowledgeable men and women for this topic, however you be understood as you know what you’re referring to! Thanks Buy NCLEX Certificate online

 

 

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

 

 

I am often to blogging i actually appreciate your site content. The content has really peaks my interest. I am going to bookmark your web blog and maintain checking for brand new information. Buy Original MCAT Certificate without exam

meidir said:
Mar 01, 2023 01:07:22 AM Frank has been pretending not to receive his pension checks in order to talk with Sara, the customer service representative, on the phone. Grading plan cost
meidir said:
Mar 02, 2023 04:07:35 PM

Nice post. I was checking constantly this blog and I’m impressed! Very helpful information specifically the last part I care for such information much. I was seeking this particular info for a long time. Thank you and best of luck. Buy Headstones & Monuments Online West Virginia

meidir said:
Mar 15, 2023 01:00:59 AM

I always was interested in this subject and still am, thankyou for posting . ทำฟัน ใกล้ฉัน

 

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

 

The INTEREST RATES doesn’t necessarily compensate appeal for almost any too much tax bill repayments, therefore you are really bringing this inside slacks by simply not altering the duty repayments. slot

 

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

 

I’d ought to seek advice from you here. Which isn’t some thing It’s my job to do! I spend time reading a post that could make people believe. Also, thanks for allowing me to comment! east slot เว็บตรง

 

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

 

I’d must check with you here. Which isn’t something It’s my job to do! I love to reading an article that may make people think. Also, thank you allowing me to comment! easyslot

 

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

 

Well, that is most certainly good, however think about the additional options we now have here? Would you brain creating another post regarding these too? Respect! sagame66

 

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

 

Sweet blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks สล็อต

 

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

 

I rattling lucky to find this website on bing, just what I was looking for : D too saved to favorites . jili slot

 

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

 

I come across your blog from facebook and it is actually nice. Thnkx for giving this sort of an incredible article! slot wallet

meidir said:
Mar 16, 2023 01:32:35 AM You can certainly see your enthusiasm in the work you write. The world hopes for even more passionate writers like you who are not afraid to say how they believe. Always go after your heart. Marketing agency for pubs
meidir said:
Mar 19, 2023 02:51:56 AM

I have to have made to order myspace images produced up, would you accomplish that kind of style function by any opportunity? https://weloveanimalsclub.blogspot.com/

 

 

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

 

 

I’ve recently been meditating on the identical matter personally lately. Pleased to see somebody on the same wavelength! Nice article. Descargar series gratis

meidir said:
Mar 25, 2023 10:41:59 PM

I’d must check with you here. Which isn’t one thing I often do! I take pleasure in reading a post that can make folks think. Also, thanks for permitting me to comment! antonimo de elevada

meidir said:
Mar 27, 2023 10:54:54 PM

Hello, I was researching the web and I came across your own blog. Keep in the great work. Woking taxi service

meidir said:
Apr 04, 2023 12:54:58 AM

my dad is a massage therapist and he can really relieve minor pains and injuries’ French Bulldog Puppies for sales in Michigan

meidir said:
Apr 08, 2023 06:47:47 PM

I believe that is among the such a lot important information for me. And i am glad studying your article. However wanna commentary on few normal things, The site taste is ideal, the articles is truly excellent . Just right process, cheers. Woking taxi to Heathrow

meidir said:
Apr 10, 2023 08:19:54 PM

Found this on Google and I’m happy I did. Amazing article. Sitios Web Panamá

 

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

 

I am curious to find out what blog system you happen to be working with? I’m having some small security issues with my latest blog and I would like to find something more safe. Do you have any recommendations? morralitos de manta

 

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

 

Made clear, that they include the fact that return is gotten, though not nonetheless ready-made; this tax burden money back guarantee was sent or feeling stimulated towards your money with a certain time frame; or perhaps notice the IRS . GOV was initially could not present the discount back to you due to numerous sending dilemma. tabla de nutrientes para hidroponía

meidir said:
Apr 11, 2023 07:29:32 PM

I am speechless. It is a very good weblog and very engaging too. Nice paintings! That’s no longer actually so much coming from an beginner writer like me, but it’s all I may just say after diving into your posts. Nice grammar and vocabulary. Not like other blogs. You actually understand what you?re talking about too. Such a lot that you just made me need to explore more. Your weblog has turn out to be a stepping stone for me, my friend. fun88

 

 

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

 

 

i love baby gifts and i love to give baby gifts to my baby and also the my sister’s baby` link w88

meidir said:
Apr 16, 2023 05:33:30 PM

I went over this site and I conceive you have a lot of good information, saved to fav (:. all inclusive cerca de rio de janeiro

 

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

 

I love gathering useful info, this post has got me even more info! . agencia digital bogota

meidir said:
Apr 17, 2023 10:30:53 PM

Content like this is a great change of pace for me. I like articles that have original thoughts that people can relate to and understand. PAW Patrol Toys Reviews

meidir said:
May 04, 2023 10:36:59 PM

I adore your wordpress web template, wherever would you download it from? real estate funds

meidir said:
May 14, 2023 03:23:18 PM

I beloved as much as you will receive carried out right here. The comic strip is attractive, your authored subject matter stylish. nevertheless, you command get got an shakiness over that you would like be delivering the following. unwell undoubtedly come further beforehand once more since precisely the similar nearly a lot steadily within case you protect this hike. excursiones en punta cana

meidir said:
May 20, 2023 01:56:49 AM

Hey there! Great post! Please when I will see a follow up! Strafverteidiger HagenI wish to express my respect for your generosity supporting men and women who require guidance on this one topic. Your special commitment to getting the solution all-around has been especially important and have all the time made those much like me to attain their objectives. Your amazing important recommendations implies much a person like me and a whole lot more to my office workers. Buy Original ECFMG Certificate Without Exam

meidir said:
May 21, 2023 12:23:03 AM

Youre so cool! I dont suppose Ive learn something like this before. So good to search out any person with some unique thoughts on this subject. realy thanks for starting this up. this website is one thing that’s wanted on the net, somebody with a bit originality. helpful job for bringing one thing new to the internet! Hoteles con Jacuzzi

meidir said:
May 22, 2023 12:31:31 AM

I discovered your blog site on google and examine a number of of your early posts. Continue to maintain up the superb operate. I simply extra up your RSS feed to my MSN News Reader. In search of forward to reading more from you later on!… cita para recoger el nie

seo service london said:
Nov 30, 2023 06:43:51 PM

Great post and amazing facts right here.Keep it up the wonderful work

한국야동 said:
Dec 03, 2023 04:48:01 PM

Regards for helping out, excellent information.
한국야동 https://yadong.cam

카지노뱅크 said:
Jan 16, 2024 07:27:44 PM

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking

뉴토 끼 said:
Jan 23, 2024 05:08:26 PM

I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed...


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter