[OPT TP3] Avant dernière question
This commit is contained in:
parent
41d8036710
commit
f278b85576
12
SPE/IPT/TP Avalanche/main.py
Executable file
12
SPE/IPT/TP Avalanche/main.py
Executable file
@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
import random
|
||||
|
||||
n = 50
|
||||
m = 50
|
||||
sol = [[2*(n-i) for j in range(n)] for i in range(n)]
|
||||
neige = [[100 if i<5*n/10 and i>2*n/10 and j>3*n/10 and j<7*n/10 else 0 for j in range(n)] for i in range(n)]
|
||||
positions = [(i,j) for i in (-1,0,1) for j in (-1,0,1)]
|
||||
|
||||
|
||||
for i in range(m+1):
|
||||
|
107
SPE/IPT/TP2 Tris/main.py
Normal file
107
SPE/IPT/TP2 Tris/main.py
Normal file
@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
import random
|
||||
|
||||
|
||||
def tab_alea(n, mini=0, maxi=1e6):
|
||||
return [random.randint(mini, maxi) for _ in range(n)]
|
||||
|
||||
|
||||
def tri_bulle(l):
|
||||
t = l[::]
|
||||
perm = True
|
||||
n = len(t)
|
||||
while perm:
|
||||
perm = False
|
||||
for j in range(0, n-1):
|
||||
if t[j] > t[j+1]:
|
||||
t[j], t[j+1] = t[j+1], t[j]
|
||||
perm = True
|
||||
return t
|
||||
|
||||
|
||||
def indice_mini(m, i, j):
|
||||
im = i
|
||||
for k in range(i, j):
|
||||
if m[k] < m[im]:
|
||||
im = k
|
||||
return im
|
||||
|
||||
|
||||
def tri_selection(l):
|
||||
t = l[::]
|
||||
n = len(t)
|
||||
for i in range(n):
|
||||
im = indice_mini(t, i, n)
|
||||
t[i], t[im] = t[im], t[i]
|
||||
return t
|
||||
|
||||
|
||||
def place(x, m):
|
||||
ind = 0
|
||||
n = len(m)
|
||||
while ind<n and x>m[ind]:
|
||||
ind += 1
|
||||
return ind
|
||||
|
||||
|
||||
def tri_insertion(l):
|
||||
t = l[::]
|
||||
n = len(t)
|
||||
triee = []
|
||||
for i in range(0, n):
|
||||
k = place(t[i], triee)
|
||||
triee = triee[:k] + [t[i]] + triee[k:]
|
||||
return triee
|
||||
|
||||
def tri_insertion_en_place(l):
|
||||
t = l[::]
|
||||
n = len(t)
|
||||
for i in range(0, n):
|
||||
k = 0
|
||||
while k<i and t[i]>t[k]:
|
||||
k+=1
|
||||
for j in range(i-1, k-1, -1):
|
||||
t[j+1], t[j] = t[j], t[j+1]
|
||||
return t
|
||||
|
||||
|
||||
def fusionne(t1, t2):
|
||||
w = []
|
||||
n1 = len(t1)
|
||||
n2 = len(t2)
|
||||
ind1 = ind2 = 0
|
||||
while ind1 < n1 and ind2 < n2:
|
||||
if t1[ind1] < t2[ind2]:
|
||||
w.append(t1[ind1])
|
||||
ind1 += 1
|
||||
else:
|
||||
w.append(t2[ind2])
|
||||
ind2 +=1
|
||||
for el in t1[ind1:]+t2[ind2:]:
|
||||
w.append(el)
|
||||
return w
|
||||
|
||||
def tri_fusion(t):
|
||||
n = len(t)
|
||||
if n <= 1:
|
||||
return t
|
||||
else:
|
||||
return fusionne(tri_fusion(t[:n//2]), tri_fusion(t[n//2:]))
|
||||
|
||||
def quicksort(t):
|
||||
n = len(t)
|
||||
if n <= 1:
|
||||
return t
|
||||
else:
|
||||
pivot = t[0]
|
||||
tg, t_pivots, td = [], [], []
|
||||
for i in range(n):
|
||||
if t[i] < pivot:
|
||||
tg.append(t[i])
|
||||
elif t[i] == pivot:
|
||||
t_pivots.append(t[i])
|
||||
else:
|
||||
td.append(t[i])
|
||||
return quicksort(tg) + t_pivots + quicksort(td)
|
||||
|
||||
|
67
SPE/OPT/TP 3/main.ml
Normal file
67
SPE/OPT/TP 3/main.ml
Normal file
@ -0,0 +1,67 @@
|
||||
type 'a arbre =
|
||||
| Vide
|
||||
| Noeud of int * 'a * 'a arbre * 'a arbre;;
|
||||
|
||||
let taille t = match t with
|
||||
| Vide -> 0
|
||||
| Noeud(i,_,_,_) -> i;;
|
||||
|
||||
let rec hauteur t = match t with
|
||||
| Vide -> -1
|
||||
| Noeud(_,_,fg, fd) -> 1 + max (hauteur fg) (hauteur fd);;
|
||||
|
||||
let creer_noeud a fg fd = Noeud((1+(taille fg)+(taille fd)), a, fg, fd);;
|
||||
|
||||
let rec recalculer_taille t = match t with
|
||||
|Vide -> Vide
|
||||
|Noeud(_,a,fg,fd) -> creer_noeud a (recalculer_taille fg) (recalculer_taille fd);;
|
||||
let test =recalculer_taille (creer_noeud 5 (Noeud(1,0,Noeud(2,-2,Vide,Vide),Noeud(1,2,Noeud(0,1,Vide,Vide),Noeud(1,3,Vide,Vide)))) (Noeud(0, 10, Vide, Vide)));;
|
||||
|
||||
let elements t =
|
||||
let rec aux acc f = match f with
|
||||
| Vide -> acc
|
||||
| Noeud(i, e, fg, fd) -> aux (e::(aux acc fg)) fd in
|
||||
List.rev (aux [] t);;
|
||||
|
||||
elements test;;
|
||||
|
||||
(* II ABR avec annotations de taille *)
|
||||
(*5 : Le parcours infixe est croissant si et seulement si l'arbre est un ABR *)
|
||||
let est_abr t=
|
||||
let rec croiss l = match l with
|
||||
| a::b::t -> (a < b) && croiss (b::t)
|
||||
| _ -> true in
|
||||
croiss (elements t);;
|
||||
est_abr test;;
|
||||
|
||||
let rec contient e t = match t with
|
||||
| Vide -> false
|
||||
| Noeud(i,a,fg,fd) -> if e = a then true
|
||||
else if e > a then contient e fd
|
||||
else contient e fg;;
|
||||
contient (-3) test;;
|
||||
|
||||
let rec insere e t = match t with
|
||||
| Vide -> creer_noeud e Vide Vide
|
||||
| Noeud(i, a, fg, fd) -> if e > a then creer_noeud a fg (insere e fd)
|
||||
else creer_noeud a (insere e fg) fd;;
|
||||
|
||||
let rec construit l n = match n with
|
||||
| 0 -> (Vide,l)
|
||||
| _ -> let (t1, h::reste) = construit l (n/2) in let (t2, r) = construit reste ((n-1)/2) in (creer_noeud h t1 t2, r);;
|
||||
|
||||
construit [1;2;3;4;5;6;7;8] 8;;
|
||||
|
||||
let alpha = 0.7;;
|
||||
|
||||
let reconstruit t = match t with
|
||||
|Vide -> Vide
|
||||
|Noeud(i, _, _ ,_) -> fst (construit (elements t) i);;
|
||||
|
||||
reconstruit test;;
|
||||
|
||||
let rebalance t = match t with
|
||||
|Vide -> Vide
|
||||
|Noeud(i,_,fg,fd) -> let n = float_of_int i in if ((taille fg) < alpha*.n ) && ((taille fd) < alpha*.n) then t else reconstruit t;;
|
||||
float_of_int;;
|
||||
alpha* float_of_int.5;;
|
Loading…
Reference in New Issue
Block a user