-
Notifications
You must be signed in to change notification settings - Fork 0
/
HOF.py
45 lines (29 loc) · 767 Bytes
/
HOF.py
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
43
44
45
from math import *
def exception_function(f, rejected_input, new_output):
def g(x):
return new_output if x==rejected_input else f(x)
return g
new_sqrt = exception_function(sqrt, 7, 2)
def usually_double(x):
"""Your code here"""
return 2*x
def new_double(x):
"""Your code here"""
if x in [4,7,11]:
return exception_function(usually_double, x, 0)(x)
else:
return usually_double(x)
def make_generator(op):
"""Your code here"""
def g(y):
def h(x):
return op(x,y)
return h
return g
def mul(x,y):
return x*y
def pow(x,y):
return x**y
make_multiplier = make_generator(mul)
make_exponentiator = make_generator(pow)
print(make_exponentiator(3)(2))