-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL4 higher-order function
More file actions
47 lines (36 loc) · 888 Bytes
/
Copy pathL4 higher-order function
File metadata and controls
47 lines (36 loc) · 888 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
43
44
45
46
47
"""this is the thinking process of how to generalize the functions"""
1) write out all the functions
2. find common pattern
3. abstract the common pattern and formulate individual pattern"""
def sum_naturals(n):
""" sum of first n nature numbers"""
total = 0
i = 1
while i <= n:
total += i
i = i + 1
return total
def sum_squares(n):
"""sum of first n squares"""
total = 0
i = 1
while i <=n:
total += i**
i = i + 1
return total
"""DRY method"""
def summation(n, term): //term is a function here
"""sum the first n terms of summation
>>> summation(5, cube)
225
"""
total = 0
i = 1
while i <= n:
total = total + term(i)
i = i + 1
return total
def identity(n) :
return n;
def cube(n):
return pow(n, 3)