-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture_6
More file actions
64 lines (34 loc) · 824 Bytes
/
Lecture_6
File metadata and controls
64 lines (34 loc) · 824 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
We started with lambda function:
x = 10
square = x * x
>>> square
100
square = lambda x: x * x
>>> square(4)
16
>>> square(x)
100
Currying: Transforming a multi-argument function into a single-argument, higher-order function.
Know: add(2, 3) = 5
from operator import add
newfunction = lambda f: lambda x: lambda y: f(x, y)
>>> newfunction(add)(2)(3)
5
IMPORTANT: find the square root of a number using Babylonian Method:
def square_root(a):
x = 1
while x * x != a:
print(x)
x = square_root_update(x, a)
return x
def square_root_update(x, a):
return (x + a/x) / 2
CUBE ROOT:
def cube_root(a):
x = 1
while x * x * x != a:
print(x)
x = cube_root_update(x, a)
return x
def cube_root_update(x, a):
return (2 * x + a / (x * x)) / 3