-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_environment_2.py
More file actions
135 lines (108 loc) · 3.49 KB
/
Copy pathPython_environment_2.py
File metadata and controls
135 lines (108 loc) · 3.49 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import math # import math module
# ex 4
fn = lambda n: 2 * n
def Make_iterator(fn):
"""
function that create an iterator that changes according to a function parameter with nonlocal use
:param fn: parameter func that decide the count progress
counter -- a parameter that counts for every run change from the inner func
iterate -- inner func that uses non local counter and fn to create the wanted iteration
:return: iterate the inner function
"""
counter = -1
def iterate():
"""
inner func that uses non local counter and fn to create the wanted iteration
:return:int number that calculate by operating fn on counter
"""
nonlocal counter
counter += 1
return fn(counter)
return iterate
''' func 1 tests
iterator = Make_iterator(fn)
for i in range(4):
print(iterator())
print(iterator())
it = Make_iterator(fn)
for i in range(4):
print(it())
'''
# ex 5
def isPrime(x):
"""
:param x:a given number to check if is prime
:return: false -- if it not prime
true -- if the number is prime
"""
for i in range(2, x):
if x % i == 0:
return False
return True
def PerfectSquare(x):
"""
checks if a number is perfect square -- the number sqrt is a integer.
:param x: the number that we want to check
:return: true -- if is perfect square
false -- if isn't
"""
s = int(math.sqrt(x))
return s * s == x
def isFib(x):
"""
check if a given number is in fibonacci sequence
:param x: number to check
:return: true -- if the number in fibo seq
false -- if is not
"""
return PerfectSquare(5 * x * x + 4) or PerfectSquare(5 * x * x - 4)
def listFilter(List, f):
"""
create a new filtered list that stand in given function definition
:param List: list that we want to filter
:param f: a func that decide which list argo we want to filter
:return: a new list that has been filtered
"""
size = len(List) - 1
while size >= 0:
if not f(List[size]):
List.pop(size)
size -= 1
return List
def listFilterMulti(List, fList):
"""
a function that filtering a list with a couple of function
:param List: sequence we want to filter
:param fList: list of function we want to filter the list with
:return: a new list that has been filtered by all the function in flist
"""
for i in range(len(fList)):
List = list(listFilter(List, fList[i]))
return List
# ex 5 test
# print(listFilterMulti([2, 4, 5, 6, 7, 13], [isPrime, isFib]))
# ex6
def approx_eq(x, y, tolerance=1e-3):
"""
check if two numbers are approximately equale
:param x: first number
:param y: second number
:param tolerance: the value that decide if the numbers are close enough
:return: true -- if close enough
false -- if not close enough
"""
return abs(x - y) < tolerance
def Fixed_point(f, x):
"""
check if x has a fixed point in relation to f function
:param f: function to check converging point
:param x: starting and improved guesses
:return: the number that the function converge to(fixed point)
or None if cant find
"""
for i in range(20):
if approx_eq(x, f(x)):
return x
x = f(x)
return None
print(Fixed_point(lambda n: math.sqrt(n), 2))