-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
317 lines (171 loc) · 6.17 KB
/
main.py
File metadata and controls
317 lines (171 loc) · 6.17 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
Demonstrations that mathematics is really useful when coding competiting.
"""
import string
import numpy as np
# 1. Somme géométrique
total_sum = 0
n = 20
q = 3
for k in range(0, n + 1):
total_sum += 3 ** k
# 1. Formule Mathématique
result_somme_geometrique = (1 - q**(n+1)) / (1 - q)
# Mathematics, others formula
result = n * (n + 1) / 2
# Sum the number of square
result_square = n * (n + 1) * (2 * n + 1) / 6
#2. Congruence
# La planete Vegeta a une periode de revolution de 15 jours autour de la Terre, elle est visible seulement au 6ème jour de sa révolution
# La planete Namek a une periode de revolution de 7 jours autour de la Terre, elle est visible seulement au 5ème jour
# Quand les deux planetes seront-elles visibles ensemble ?
vegeta = range(1, 16)
namek = range(1, 8)
from itertools import cycle
cycle_vegeta = cycle(vegeta) # Creation of a cycle
cycle_namek = cycle(namek)
condition_not_met = True
# Cycle
number_days = 1
while condition_not_met:
day_vegeta = next(cycle_vegeta)
day_namek = next(cycle_namek)
if day_vegeta == 6 and day_namek == 5:
break
number_days += 1
# 3. Trouver ci deux couples sont dans le même ensemble
# Comment créer un nouvel ID, sans pour autant casser la mémoire
user_id_1 = 1234564
user_id_2 = 2356458
relation = {'user': user_id_1, 'follower': user_id_2}
3 + 4
5 + 2
id_relation = str(user_id_1) + str(user_id_2)
def bijection_n_2_to_n(i: int, j: int):
return int((i + j) * (i + j + 1) * 0.5 + j)
number_biject = bijection_n_2_to_n(57, 86)
number_biject_2 = bijection_n_2_to_n(86, 57)
# 4. Coins flipped
# Imagine you throw multiple times a random coins n = 20, the probability of even is 0.3
# What is the exact number of times, we get the 10
import random
n = 20
p = 0.3
number_trials = 1000
list_result = []
for trial in range(number_trials):
nb_times_even = 0
for i in range(n):
result_coin = random.randint(a=1, b=10)
if result_coin <= 3:
nb_times_even += 1
list_result.append(nb_times_even)
probability_10 = list_result.count(10) / len(list_result)
# Mathematics
from scipy.stats import binom
exact_result = binom.pmf(k=10, n=n, p=p)
# 5. On cherche à montrer que deux phrases sont équivalentes
sentence_1 = "question sans reponsea "
sentence_2 = "enquetons sans espoir"
dict_letters = {l: 0 for l in 'abcdefghijklmnopqrstuvwxyz '}
for letter in sentence_1:
if letter != ' ':
score_letter = dict_letters[letter]
dict_letters.update({letter: score_letter + 1})
for letter in sentence_2:
if letter != ' ':
score_letter = dict_letters[letter]
dict_letters.update({letter: score_letter - 1})
for k, v in dict_letters.items():
if v != 0:
print('This is not an anagramme.')
# Solution avec le theoreme fondamental de l'arithmetique
nombre_premiers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
def compute_product_sentence(sentence: str):
"""
Compute the sentence.
:param sentence:
:return:
"""
result_product = 1
for letter in sentence:
if letter != ' ':
position_alphabet = string.ascii_lowercase.index(letter)
result_product *= nombre_premiers[position_alphabet]
return result_product
result_product_1 = compute_product_sentence(sentence_1)
result_product_2 = compute_product_sentence(sentence_2)
# 6. Suite arithmetico-geometrique
# Quel est la valeur de u_100 sachant que
u_n = 1
u_n_plus_1 = u_n * 5 + 3
N = 100
u_k = 1
for k in range(10):
u_k_plus_1 = u_k * 5 + 3
u_k = u_k_plus_1
# Mathematics
# r = b / (1 - a)
r = 3 / (1 - 5)
# u_n = a ** n * (u_0 - r) + r
result_suite_arithmetico_geometrique = 5 ** 10 * (1 - r) + r
# 7. Piece truquee esperance
p = 0.15 # Obtenir face (piece truque)
r = 2 # Nombre succes
# On compte le nombre d'echecs avant d'obtenir le premier face, calculer le nombre
# En moyenne, au bout de lancer, il y aura-t-il le premier succes ?
nombre_lancers_liste = []
from scipy.stats import bernoulli
for i in range(100000):
nombre_lancer = 0
while True:
tirage = random.randint(a=1, b=100)
if tirage <= 15:
break # On obtient face
nombre_lancer += 1
nombre_lancers_liste.append(nombre_lancer)
result_proba_binomiale_negative = np.mean(nombre_lancers_liste)
# Mathematics
# E(X) = (1 - p) / p
result_formula_binomiale_negative = (1 - p) / p
# 8. Monty Hall
portes_initiales = ['chevre', 'chevre', 'ferrari']
number_trials_monty_python = 10000
counter_success = 0
counter_success_no_changing = 0
for _ in range(number_trials_monty_python):
portes = random.sample(portes_initiales, 3)
index_ferrari = portes.index('ferrari')
index_portes = [0, 1, 2]
choices_final = [0, 1, 2]
index_porte_initial = random.randint(a=0, b=2) # La personne selectionne une porte
index_portes.remove(index_porte_initial) #
if index_ferrari in index_portes:
index_portes.remove(index_ferrari)
index_presentateur = random.sample(index_portes, 1)[0] # The anchorman pick a door which is not
# print("The presentateur display the door number {} which is a {}".format(index_presentateur, portes[index_presentateur]))
choices_final.remove(index_presentateur)
choices_final.remove(index_porte_initial)
final_choice = choices_final[0]
final_choice_no_changing = portes[index_porte_initial]
# print('The result is the door number {} which is a {}'.format(final_choice, portes[final_choice]))
if portes[final_choice] == 'ferrari':
counter_success += 1
if final_choice_no_changing == 'ferrari':
counter_success_no_changing += 1
print('The probability when changing the strategy is: ', counter_success / number_trials_monty_python)
print('The probability when not changing the strategy is: ', counter_success_no_changing / number_trials_monty_python)
# Bayes Theorem
########
### 9. Suite de Fibonnacci
N = 8
f_0 = 1
f_1 = 1
a, b = f_0, f_1
for k in range(N):
c = a + b
a = b
b = c
phi = (1 + np.sqrt(5)) / 2
phi_prime = (1 - np.sqrt(5)) / 2
result_formula = int((phi ** N - phi_prime ** N) / np.sqrt(5))