[IPT] TD 1: Exos 1 à 3.

This commit is contained in:
Suwako Moriya 2020-09-15 18:55:13 +02:00
parent 3767f69745
commit a85881db27
4 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,79 @@
#!/usr/bin/env python3
import random
import numpy as np
import matplotlib.pyplot as plt
class Question:
def __init__(self,i=1, l=0, n=''):
self.name=n
self.level=l
self.number=i
def __enter__(self):
print('\n' + (self.level*2)*' ' + f"-> {self.number}. : {self.name} -- Début")
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
print((self.level*2)*' ' + f"<- {self.number}. : {self.name} -- Fin\n")
with Question(1):
def tirer(n):
item = 0
for _ in range(n):
if random.random()<.5:
item +=1
return item
def proportion(n , N, k):
s = 0
for i in range(N):
if tirer(n) == k:
s += 1
return s/N
print('proportion(10, 100, 1) :', proportion(10, 100, 5))
n = 10
N = 100
plt.figure(0)
x = np.asarray(range(n+1))
y = np.asarray([proportion(n, N, k) for k in x])
plt.plot(x, y)
def binom(k, n):
return np.math.factorial(n)/(np.math.factorial(k)*np.math.factorial(n-k))
plt.figure(1)
x = np.asarray(range(n+1))
y = np.asarray([binom(k, n) for k in x])
plt.plot(x, y)
#Version avec une proportion p :
def tirer2(n, p):
item = 0
for _ in range(n):
if random.random()<p:
item +=1
return item
def proportion2(n, p, N, k):
s = 0
for i in range(N):
if tirer2(n, p) == k:
s += 1
return s/N
p = .3
print('proportion2(10, p, 100, 1) :', proportion2(10, p, 100, 5))
n = 10
N = 100
plt.figure(2)
x = np.asarray(range(n+1))
y = np.asarray([proportion2(n, p, N, k) for k in x])
plt.plot(x, y)
def prop(k, n, p):
return binom(k, n) * (p**k) * (1-p)**(n-k)
plt.figure(3)
x = np.asarray(range(n+1))
y = np.asarray([prop(k, n, p) for k in x])
plt.plot(x, y)
plt.show()

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python3
class Question:
def __init__(self,i=1, l=0, n=''):
self.name=n
self.level=l
self.number=i
def __enter__(self):
print('\n' + (self.level*2)*' ' + f"-> {self.number}. : {self.name} -- Début")
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
print((self.level*2)*' ' + f"<- {self.number}. : {self.name} -- Fin\n")
with Question(2):
L = [True]*31
def modifier(L, p):
if p in (0,1):
L[p] = False
else:
for i in range(2*p,len(L), p):
L[i] = False
def premiers(n):
L = [True]*(n+1)
for i in range(n):
if L[i]:
modifier(L, i)
return [i for i, premier in enumerate(L) if premier]
print(premiers(823417)[-1])
print("Donc 823417 n'est pas premier.")

View File

@ -0,0 +1,55 @@
#!/usr/bin/env python3
class Question:
def __init__(self,i=1, l=0, n=''):
self.name=n
self.level=l
self.number=i
def __enter__(self):
print('\n' + (self.level*2)*' ' + f"-> {self.number}. : {self.name} -- Début")
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
print((self.level*2)*' ' + f"<- {self.number}. : {self.name} -- Fin\n")
import numpy as np
import random
with Question(3):
A = np.reshape(np.array([random.randint(0,9) for _ in range(9*9)]), (9,9))
B = np.reshape(np.array([random.randint(0,9) for _ in range(2*2)]), (2,2))
print(A)
print(B)
def motif(B, A):
for i in range(A.shape[0] - B.shape[0] + 1):
for j in range(A.shape[1] - B.shape[1] + 1):
diff = False
for k in range(B.shape[0]):
for l in range(B.shape[1]):
if B[k][l]!=A[i+k][j+l]:
diff = True
break
if diff:
break
if not diff:
return True
return False
print(motif(B, A))
def S(A, B):
s = 0
for i in range(A.shape[0]):
for j in range(A.shape[1]):
s += (A[i][j] - B[i][j])**2
return s
def proche_motif(B, A):
M = np.zeros((A.shape[0] - B.shape[0] + 1, A.shape[1] - B.shape[1] + 1))
mini = (0,0)
for i in range(A.shape[0] - B.shape[0] + 1):
for j in range(A.shape[1] - B.shape[1] + 1):
M[i][j] = S(B, A[i: i +B.shape[0], j: j + B.shape[1]])
if M[mini[0]][mini[1]] > M[i][j]:
mini = (i,j)
return A[mini[0]: mini[0] +B.shape[0], mini[1]: mini[1] + B.shape[1]]
print(proche_motif(B,A))

View File

@ -0,0 +1,5 @@
#!/usr/bin/env python3
from exo1 import *
from exo2 import *
from exo3 import *