-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathproblem_31.py
More file actions
42 lines (35 loc) · 1010 Bytes
/
problem_31.py
File metadata and controls
42 lines (35 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'''
Implement the higher order functions map(), filter()and reduce().
(They are built-in but writing them yourself
may be a good exercise.
'''
def mymap(func,seq):
ini = []
for item in seq:
ini.append(func(item))
return ini
def myfilter(func,seq):
ini = []
for item in seq:
if func(item)==True:
ini.append(item)
return ini
def myreduce(func,seq):
i = len(seq)
while i>1:
f = func(seq[0],seq[1])
seq.pop(0) # "del seq[0]" do so
seq.pop(0)
seq.insert(0,f)
i = len(seq)
return seq[0]
### Copied from net
def myreduce_net(func, seq):
tmp = seq[0]
for item in seq[1:]: # works with only 'seq' too
tmp = func(tmp,item)
return tmp
print(mymap(len,['abc','abcd']))
print(myfilter(lambda x:x>5,[1,3,5,7,9]))
print(myreduce(lambda a,b:a if a>b else b,[3,7,4,5,1]))
print(myreduce_net(lambda a,b:a if a>b else b,[3,7,4,5,1]))