Python Function Note
1 #Hanoi Tower problem python implementation2 defMy_move (n, a, B, c):3 ifn = = 1:4 Print(A +' -'+c)5 Else:6My_move (N-1, A, C, b)#put the first n-1 plate from A to B7My_move (1, A, B, c)#put the bottom plate from a to C.8My_move (n-1, B, A, c)#put the n-1 plate on C on the B.9 return
1 #Yang Hui triangle python implementation2 defmy_triangles (max):3i = 14now = [1]5 whileI <=Max:6 yieldNow#Save current list7now = [1] + [now[n]+now[n+1] forNinchRange (len (now)-1)] + [1]#Building the downlevel list8I+=19 Print(' Done')
1 #implementation converts ' 123.456 ' to 123.456, which is the implementation of the function float2 defMy_float (s):3 defMy_front (x, y):4 returnx*10+y5 defmy_behind (x, y):6 returnx*0.1+y7 8Front = S.split ('.') [0]9behind = S.split ('.') [1]Ten returnReduce (My_front, map (int, front)) + 0.1*reduce (my_behind, map (int, behind))
1 #screening of prime numbers by means of an Ed sieve method2 #produces infinite odd numbers.3 defMy_productnumber ():4n = 15 while1:6n + = 27 yieldN8 9 #returns a function that determines whether a prime number isTen defmy_isprime (n): One return LambdaX:x% n >0 A - #Prime number Generator - defmy_primes (): the yield2 -it =My_productnumber () - while1: -n =Next (IT) + yieldN -it =Filter (My_isprime (n), it) + A forNinchmy_primes (): at ifN < 100: - Print(n) - Else: - Break
1 # Judging whether a number is a palindrome 2 def my_ispalindrome (n): 3 return str (n) [:: -1] = = str (n)4Print(filter (My_ispalindrome, Range (1), 1000)))
1 #about adorners2 ImportFunctools3 4 defLog (text=None):5 defDecorator (func):6 @functools. Wraps (func)7 defWrapper (*args, * *kw):8 ifText = =None:9 #print (' Call%s () '% func.__name__)Ten Pass One Else: A #print ('%s%s () '% (text, func.__name__)) - Pass - Print('Begin func↓') thetemp = func (*args, * *kw) - Print('End func↑') - returnTemp - returnwrapper + returnDecorator - +@log ('Pager')#equals now = log (' hahaha ') (now) A defNow (t): at Print(" -") - returnT -Now (4) - Print(now.)__name__)
Python Function Note