Not long ago, there was an interesting article on the internet about the same problem, the Python code of different levels of Python programmers showed different styles, the code is very simple and interesting. Let's take a look at a Python program the whole process of ape advanced. (laughter)
Novice Programmer
def factorial (x): if x = = 0: return 1 else: return x * factorial (X-1)//Not simple, iterative, novice oh. Print factorial (6)
One year programming experience (Pascal's)
def factorial (x): result = 1 i = 2 while i <= x: resultresult = result * I II = i + 1 return Result print factorial (6)
One year programming experience (learning C)
def fact (x): #{ result = i = 1; while (i <= x): #{ result *= i; i + = 1; #} return result; #}
One year programming experience (read SICP)
@tailcall def fact (X, acc=1): if (x > 1): Return (Fact ((X-1), (ACC * x)) else: return ACC PR Int (fact (6))
One year programming experience (Python)
def factorial (x): res = 1 for i in Xrange (2, x + 1): res *= i return res
Lazy python Programmer
def fact (x): return x > 1 and X * FACT (X-1) or 1 print fact (6)
A lazy python programmer.
f = lambda x:x and X * F (x-1) or 1//anonymous function, bad. Program apes are really lazy people do! Print F (6)
Python experts
Fact = Lambda X:reduce (int.__mul__, Xrange (2, x + 1), 1) Print fact (6) //Expert is bad.
python hacker
Import Sys @tailcall def fact (X, acc=1): if X:return fact (x.__sub__ (1), acc.__mul__ (x)) return ACC
Expert programmer
From C_math Import fact Print fact (6)
Daying Empire Programmer
From c_maths Import fact Print fact (6) web designer 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
Unix Programmers
Import OS def fact (x): os.system (' factorial ' + str (x)) fact (6)
Windows Programmer
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 )//May own Dizzy Dish ...
Enterprise-Class Programmers
Def new (CLS, *args, **kwargs): Return cls (*args, **kwargs) class number (object): Pass class Integralnumber (i NT, 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 Mathematicssy Stem (object): Def __init__ (self, ibase): Abstract @classmethod def getinstance (CLS, ibase): try: Cls.__instance except attributeerror:cls.__instance = new (CLS, ibase) return cls.__instance Clas S Standardmathematicssystem (Mathematicssystem): Def __init__ (self, ibase): If Ibase.getbase ()! = new (Integralnumb Er, 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))//object-oriented, but in this case, it is long and smelly.