diff --git a/SPE/IPT/TD1 Exos programmation/exo1.py b/SPE/IPT/TD1 Exos programmation/exo1.py new file mode 100755 index 0000000..32e29bf --- /dev/null +++ b/SPE/IPT/TD1 Exos programmation/exo1.py @@ -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() {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.") diff --git a/SPE/IPT/TD1 Exos programmation/exo3.py b/SPE/IPT/TD1 Exos programmation/exo3.py new file mode 100755 index 0000000..6640504 --- /dev/null +++ b/SPE/IPT/TD1 Exos programmation/exo3.py @@ -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)) + diff --git a/SPE/IPT/TD1 Exos programmation/main.py b/SPE/IPT/TD1 Exos programmation/main.py new file mode 100755 index 0000000..7cdb156 --- /dev/null +++ b/SPE/IPT/TD1 Exos programmation/main.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +from exo1 import * +from exo2 import * +from exo3 import *