码迷,mamicode.com
首页 > 编程语言 > 详细

Python Function Note

时间:2016-01-14 20:50:42      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

Python Function Note

1 #汉诺塔问题Python实现
2 def my_move(n, a, b, c):
3   if n == 1:
4     print(a +  -->  + c)
5   else:
6     my_move(n-1, a, c, b)#将前n-1个盘子从a放到b
7     my_move(1, a, b, c)#将最下面的盘子从a放到c
8     my_move(n-1, b, a, c)#将b上的n-1个盘子放到c上
9   return
1 #杨辉三角Python实现
2 def my_triangles(max):
3   i = 1
4   now = [1]
5   while i <= max:
6     yield now#保存当前list
7     now = [1] + [now[n]+now[n+1] for n in range(len(now)-1)] + [1]#构建下层list
8     i+=1
9   print(done)
 1 #实现将‘123.456’转换成123.456,即函数float的实现
 2 def my_float(s):
 3   def my_front(x, y):
 4     return x*10+y
 5   def my_behind(x, y):
 6     return x*0.1+y
 7 
 8   front = s.split(.)[0]
 9   behind = s.split(.)[1]
10   return reduce(my_front, map(int, front)) + 0.1*reduce(my_behind, map(int, behind))
 1 #利用埃氏筛法筛选出素数
 2 #产生无限的奇数
 3 def my_productNumber():
 4   n = 1
 5   while 1:
 6     n += 2
 7     yield n
 8 
 9 #返回判断是否是素数的函数
10 def my_isPrime(n):
11     return lambda x: x % n > 0
12 
13 #素数发生器
14 def my_Primes():
15   yield 2
16   it = my_productNumber()
17   while 1:
18     n = next(it)
19     yield n
20     it = filter(my_isPrime(n), it)
21 
22 for n in my_Primes():
23   if n < 100:
24     print(n)
25   else:
26     break
1 #判断一个数是否回文
2 def my_isPalindrome(n):
3   return str(n)[::-1] == str(n)
4 print(filter(my_isPalindrome, range(1, 1000)))
 1 #关于装饰器
 2 import functools
 3 
 4 def log(text=None):
 5   def decorator(func):
 6     @functools.wraps(func)
 7     def wrapper(*args, **kw):
 8       if text == None:
 9         # print(‘call %s()‘ % func.__name__)
10         pass
11       else:
12         # print(‘%s %s()‘ % (text, func.__name__))
13         pass
14       print(Begin Func↓)
15       temp = func(*args, **kw)
16       print(End Func↑)
17       return temp
18     return wrapper
19   return decorator
20 
21 @log(call)  #相当于now = log(‘hahaha‘)(now)
22 def now(t):
23   print("2015")
24   return t
25 now(4)
26 print(now.__name__)

 

Python Function Note

标签:

原文地址:http://www.cnblogs.com/jackma86/p/5131407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!