From 42a068c4179e76b2b2c4e9387a31deacdaef6b86 Mon Sep 17 00:00:00 2001 From: "buqing.wang" Date: Wed, 12 Oct 2016 20:33:37 +0800 Subject: [PATCH 1/3] fuctional --- .../high_gride_functional.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 high gride functional programming/high_gride_functional.py diff --git a/high gride functional programming/high_gride_functional.py b/high gride functional programming/high_gride_functional.py new file mode 100644 index 0000000..9df4acd --- /dev/null +++ b/high gride functional programming/high_gride_functional.py @@ -0,0 +1,30 @@ +#Higher-order function + + +print"==================function as param BEGIN ==================" +def add(x,y): + return x+y + +def sub(x,y): + return x-y + +def ab(x,y): + return x*y + +def func_as_parm(x,y,f): + return f(x,y) +print func_as_parm(3,4,sub) +print func_as_parm(3,4,add) +print func_as_parm(3,4,ab) + +print"================== map / reduce BEGIN ==================" +print "if you want to know more,please refer to research.google.com/archive/mapreduce.html" +# build-in function map(function_var,list) the param function_var will operate each item in list,@return a new list + +def turn_UPPER(str): + print str.upper() + print str.title() + print str.lower() + print str.capa + return str.upper() + From a9255cf57ebd03b4c78a032b7ad39e623a3bbd3e Mon Sep 17 00:00:00 2001 From: "buqing.wang" Date: Thu, 13 Oct 2016 20:02:35 +0800 Subject: [PATCH 2/3] high function 2 --- .../high_gride_functional.py | 43 ++++++++++++++++--- .../high_gride_functional_2.py | 37 ++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 high gride functional programming/high_gride_functional_2.py diff --git a/high gride functional programming/high_gride_functional.py b/high gride functional programming/high_gride_functional.py index 9df4acd..956dc6d 100644 --- a/high gride functional programming/high_gride_functional.py +++ b/high gride functional programming/high_gride_functional.py @@ -1,7 +1,9 @@ + # -*- coding: utf-8 -*- + #上面的一行添加,标识编码,否则出现中文报错 #Higher-order function -print"==================function as param BEGIN ==================" +print"==================function as param BEGIN 中文测试==================" def add(x,y): return x+y @@ -22,9 +24,40 @@ def func_as_parm(x,y,f): # build-in function map(function_var,list) the param function_var will operate each item in list,@return a new list def turn_UPPER(str): - print str.upper() - print str.title() - print str.lower() - print str.capa + #print str.upper() + #print str.title() + #print str.lower() + #print str.capitalize() return str.upper() +def toString(name,f): + return f(name) + + +#source = input("input a string :") NOK,because input will return a num +#source = raw_input("input a string :") +#result = toString(source,turn_UPPER) +#print "you expcet answ :",result + +list=[] +str = raw_input("input a str :") +while str!= 'ok': + list.append(str) + str = raw_input("input a str :") + +print "result is :" +print map(turn_UPPER,list) + +list_num = [] +num = input("input a num :") +while num!=0: + list_num.append(num) + num = input("input a num :") + +def cub(x,y): + return x * y + +print reduce(cub,list_num) + + + diff --git a/high gride functional programming/high_gride_functional_2.py b/high gride functional programming/high_gride_functional_2.py new file mode 100644 index 0000000..1c00489 --- /dev/null +++ b/high gride functional programming/high_gride_functional_2.py @@ -0,0 +1,37 @@ + # -*- coding: utf-8 -*- + #上面的一行添加,标识编码,否则出现中文报错 +print "=======================fileter====================" +# filter(fuc_var,list) return true/false ,if func_var is true ,then keep the item,otherwise delete the item of the given list + +def is_prime(x): + + if type(x) == type(1): + if x <= 2: + return False + for i in range(2,x-1): + if x % i ==0 : + return True + return False + +#is_prime can be inproved +#print is_prime(6) test use + +num_to_judge = input("input a num :") +list = range(1,num_to_judge) +print filter(is_prime,list) + +print "=======================sorted====================" +#sorted(list,key=func) 按照func的方法来排序 + +#输入一组学生 + +L = [] +name = raw_input("input name :") +score = input("input score :") + +while name!="over": + st = name,score + L.append(st) + name = raw_input("input name :") + score = input("input score :") + From 9d462b2eb1df7685680cccccf0b2647d6dc49ee4 Mon Sep 17 00:00:00 2001 From: "buqing.wang" Date: Thu, 13 Oct 2016 20:28:30 +0800 Subject: [PATCH 3/3] high function 3 --- high gride functional programming/module.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 high gride functional programming/module.py diff --git a/high gride functional programming/module.py b/high gride functional programming/module.py new file mode 100644 index 0000000..94afd0d --- /dev/null +++ b/high gride functional programming/module.py @@ -0,0 +1,3 @@ +import sys +sys.path.append("/data/nishome/td/buqing.wang/joymine/other/python_git/python_basic") #add your own path +print sys.path \ No newline at end of file