-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuntions_abstraction.py
70 lines (56 loc) · 1.73 KB
/
funtions_abstraction.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
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
import time
start_time = time.time()
def getNumber(text='Choose an integer:'):
return int(input(text))
def enumeration(objective):
response = 0
while response ** 2 < objective:
print(response)
response += 1
if response ** 2 == objective:
print(f'The square root of {objective} is {response}')
else:
print(f'{objective} does not have an exact square root')
def approximation(objective):
epsilon = 0.01
step = epsilon ** 2
response = 0.0
while abs(response ** 2 - objective) >= epsilon and response <= objective:
print(abs(response ** 2 - objective), response)
response += step
if abs(response ** 2 - objective) >= epsilon:
print(f'The square root of {objective} was not found')
else:
print(f'The square root of {objective} is {response}')
def binary_search(objective):
epsilon = 0.001
low = 0.0
high = max(1.0, objective)
response = (high + low) / 2
while abs(response ** 2 - objective) >= epsilon:
print(f'low={low}, high={high}, response={response}')
if response ** 2 < objective:
low = response
else:
high = response
response = (high + low) / 2
print(f'The square root of {objective} is {response}')
def run ():
text = """Select an option:
1 - enumeration
2 - approximation
3 - binary_search
"""
option = getNumber(text)
objective = getNumber()
if option == 1:
enumeration(objective)
elif option == 2:
approximation(objective)
elif option == 3:
binary_search(objective)
else:
print('? xD')
print(f"--- {round(time.time() - start_time, 2)} seconds ---")
if __name__ == '__main__':
run()