-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.py
More file actions
254 lines (216 loc) · 5.14 KB
/
algorithm.py
File metadata and controls
254 lines (216 loc) · 5.14 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from math import pi, e
#Number
##################################################
class measure:
def __init__(self):
self.deg = False
mse = measure()
def sin(x):
if mse.deg:
x = (x*pi)/180
while abs(x) > (2*pi):
x = x%(2*pi)
n = 1
degree = 1
result = 0
while degree<=10:
if degree % 2 == 1:
result += (exponential(x, n)/factorial(n))
else:
result -= (exponential(x, n)/factorial(n))
degree += 1
n += 2
if abs(result) < 1e-5:
result = 0
return result
def cos(x,):
if x == 0:
return 1
if mse.deg:
x = (x*pi)/180
while abs(x) > (2*pi):
x = x%(2*pi)
n = 0
degree = 1
result = 0
while degree<=10:
if degree % 2 == 1:
result += (exponential(x, n)/factorial(n))
else:
result -= (exponential(x, n)/factorial(n))
degree += 1
n += 2
if abs(result) < 1e-5:
result = 0
return result
def tan(x):
if mse.deg:
x = (x*pi)/180
if not cos(x):
return 1/0
return (sin(x)/cos(x))
def sec(x):
if mse.deg:
x = (x*pi)/180
if not cos(x):
return 1/0
return 1/cos(x)
def csc(x):
if mse.deg:
x = (x*pi)/180
if not sin(x):
return 1/0
return 1/sin(x)
def cot(x):
if mse.deg:
x = (x*pi)/180
if x/(pi/2)==1:
return 0
if not tan(x):
return 1/0
return 1/tan(x)
def root(n, x):
initial = x/2
result = 0
degree = 1
while degree<=1000:
result = initial - ((exponential(initial, n)-x)/(n*(exponential(initial, (n-1)))))
initial = result
degree += 1
return result
def exponential(x, n):
result = x
if n == 0 and x != 0:
return 1
if x ==0:
return 0
i = abs(n)
while i > 1:
result*=x
i -= 1
if n < 0:
return 1/result
return result
def ln(x):
if x == e:
return 1
if x == 1:
return 0
if x <= 0:
return 'Invaild input'
degree = 1
result = 0
pending = 0
if abs(-1+x) < 1:
while degree <= 100:
pending += exponential(-1,degree)*exponential((-1+x),degree)/degree
degree += 1
result = -pending
return result
else:
while degree <= 500:
pending += exponential(-1,degree)*exponential((-1+x),-degree)/degree
degree += 1
return ln(x-1)-pending
def log(b,x):
if x == b:
return 1
if x == 1:
return 0
if b <= 0 or x<= 0:
return 'Invalid input'
return ln(x)/ln(b)
def simple_frac(numer, denom):
for i in range(2, min(numer, denom)+1):
if numer%i == 0 and denom%i == 0:
numer, denom = numer//i, denom//i
for i in range(2, min(numer, denom)+1):
if numer%i == 0 and denom%i == 0:
return simple_frac(numer, denom)
return (str(numer) + '/' + str(denom))
def convert_to_frac(number):
numer = number
denom = 1
while numer % 1 > 1e-10:
numer *= 10
denom *= 10
return simple_frac(int(numer), denom)
##################################################
#End of Number
#Probability
##################################################
def factorial(n):
result = 1
for i in range(2, n+1):
result *= i
return result
def permutation(n, r):
if r>n:
return 0
elif r == 0:
return 1
return factorial(n)/factorial(n-r)
def combination(n, r):
if r>n:
return 0
elif r == 0:
return 1
return permutation(n, r)/factorial(r)
##################################################
#End of Probability
#Statistics
##################################################
def mean(lst):
result = 0
for data in lst:
result += data
return result/len(lst)
def mode(lst):
def counter(data, lst):
count = 0
while data in lst:
lst.remove(data)
count += 1
return count
sets = set(lst)
if len(sets)==1:
return lst[0]
time_counter = []
result = []
for data in sets:
time_counter.append(counter(data, lst))
max_time = max(time_counter)
new_lst = [i for i in sets]
for i in range(len(time_counter)):
if time_counter[i] == max_time:
result.append(new_lst[i])
if len(set(time_counter))==1:
return ('No mode.')
else:
if len(result)==1:
return result[0]
else:
for i in result:
print (i)
return result
def median(lst):
lst.sort()
if len(lst)%2 == 0:
return (lst[(len(lst)//2)-1]+lst[(len(lst)//2)])/2
else:
return lst[(len(lst)-1)//2]
def variance(lst):
result = 0
E = mean(lst)
for i in lst:
result += exponential((i-E) ,2)
return result/len(lst)
def S_Deviation(lst):
return root(variance(lst),2)
def sort(lst):
lst.sort()
for i in lst:
print (i)
return lst
##################################################
#End of Statistics