-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture 8 - Functions as Objects.py
More file actions
103 lines (84 loc) · 2.29 KB
/
Lecture 8 - Functions as Objects.py
File metadata and controls
103 lines (84 loc) · 2.29 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def is_even(x):
return x % 2 ==0
# <QUESTION 1>
# fix the code that tries to write this function
def is_triangular(n):
"""
:param n: int > 0
:return: True if n is triangular, False otherwise
"""
total = 0
for i in range(n+1):
total += i
if total > 0 and total == n:
return True
return False
print(is_triangular(1))
print(is_triangular(4))
print(is_triangular(6))
# <BISECTION SEARCH AS A FUNCTION>
def bisection_root(x):
"""
:param x: positive int > 1
:return: approximation to the square root of x
"""
epsilon = 0.01
low = 0
high = x
guess = (high + low)/2.0
while abs(guess**2 - x) >= epsilon:
if guess**2 < x:
low = guess
else:
high = guess
guess = (high + low)/2.0
return guess
print(bisection_root(16))
print(bisection_root(1345))
print(bisection_root(124))
# <QUESTION 2>
# write a function that satisfies the following specs:
# def count_nums_with_sqrt_close_to (n, epsilon):
# """
# params n: n is an int > 2
# params epsilon: epsilon is a positive number < 1
# return: how many integers have a square root within epsilon of n
# """
# use bisection_root to get an approx. for the sqrt of an integer
# e.g. print(count_nums_with_sqrt_close_to(10, 0.1)
def count_nums_with_sqrt_close_to (n, epsilon):
count = 0
for i in range (n**3):
sqrt = bisection_root(i)
if abs(sqrt - n) < epsilon:
count+=1
return count
print(count_nums_with_sqrt_close_to(10, 0.1))
# <FUNCTIONS AS PARAMETERS>
def calc(op,x,y):
return op(x,y) # returns a function call where 'op' is the function
def add(a,b):
return a+b
def div(a,b):
if b!= 0:
return a/b
print("Denom was 0.")
res = calc(add,2,3) # returns add(2,3) -> None
# <QUESTION 2>
# write a function that meets these specs.
# def apply(criteria, n):
# """
# criteria is a func that takes in a number and returns a bool
# n is an int
# returns how many ints from 0 to n (inclusive) match the criteria
# (i.e. return True when run with criteria)
# """
# using is_even() function as an example for criteria
def apply (criteria, n):
count = 0
for i in range(0,n+1):
if criteria(i):
count += 1
return count
how_many = apply(is_even,10)
print(how_many)