Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions high gride functional programming/high_gride_functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
#上面的一行添加,标识编码,否则出现中文报错
#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.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)



37 changes: 37 additions & 0 deletions high gride functional programming/high_gride_functional_2.py
Original file line number Diff line number Diff line change
@@ -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 :")

3 changes: 3 additions & 0 deletions high gride functional programming/module.py
Original file line number Diff line number Diff line change
@@ -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