checkpoint
This commit is contained in:
parent
3d90faaf89
commit
ffa1adea1f
@ -100,5 +100,10 @@ ajouteRoutes(plan3, [(1,6),(1,2),(2,7),(6,7),(6,5),(5,10),(10,11),(11,9),(9,8),(
|
||||
|
||||
#Troisième partie
|
||||
|
||||
#Préliminaire
|
||||
import random
|
||||
def entierAleatoire(k):
|
||||
return random.randint(1,k)
|
||||
#1.
|
||||
|
||||
|
||||
def coloriageAleatoire(plan, couleur, k, s, t):
|
||||
|
BIN
obligatoire/TP5_Bonus/TP5 listes bonus.pdf
Normal file
BIN
obligatoire/TP5_Bonus/TP5 listes bonus.pdf
Normal file
Binary file not shown.
148
obligatoire/TP5_Bonus/main.py
Executable file
148
obligatoire/TP5_Bonus/main.py
Executable file
@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Thu Feb 6 08:23:46 2020
|
||||
|
||||
@author: suwako
|
||||
"""
|
||||
|
||||
#Exercice 1
|
||||
#a)
|
||||
"""La fonction len permet de connaître la durée de la randonnée d'Alice."""
|
||||
#b)
|
||||
def altmax(alts):
|
||||
res = alts[0]
|
||||
for i in range(1, len(alts)):
|
||||
if alts[i] > res :
|
||||
res = alts[i]
|
||||
return res
|
||||
#c)
|
||||
def denivmax(alts):
|
||||
res = max(0, alts[0])
|
||||
for i, j in enumerate(range(1, len(alts))):
|
||||
deniv = alts[j] - alts[i]
|
||||
if deniv > res :
|
||||
res = deniv
|
||||
return res
|
||||
|
||||
def heure_denivmax(alts):
|
||||
res = (max(0, alts[0]), 0)
|
||||
for i, j in enumerate(range(1, len(alts))):
|
||||
deniv = alts[j] - alts[i]
|
||||
if deniv > res[0] :
|
||||
res = (deniv, j)
|
||||
return res[1]
|
||||
|
||||
def denivtotal(alts):
|
||||
res = max(0, alts[0])
|
||||
for i, j in enumerate(range(1, len(alts))):
|
||||
deniv = alts[j] - alts[i]
|
||||
if deniv > 0:
|
||||
res += deniv
|
||||
return res
|
||||
|
||||
def sommets(alts):
|
||||
res = []
|
||||
for a, b, c in zip(range(len(alts)-2),
|
||||
range(1, len(alts) - 1),
|
||||
range(2, len(alts))):
|
||||
if alts[a]<alts[b] and alts[b]>alts[c]:
|
||||
res.append(alts[b])
|
||||
return res
|
||||
|
||||
#Exercice 2
|
||||
|
||||
def pgplateau(tab):
|
||||
grand_plateau = 0
|
||||
plateau = 0
|
||||
for el in tab:
|
||||
if el == 0:
|
||||
plateau += 1
|
||||
else:
|
||||
grand_plateau = max(grand_plateau, plateau)
|
||||
plateau = 0
|
||||
return grand_plateau
|
||||
#Exercice 3:
|
||||
import random
|
||||
#a)
|
||||
def tableau_hasard(n=100): return [random.randint(0,n-1) for i in range(n)]
|
||||
#b)
|
||||
def absents(tab):
|
||||
presents = {tab[0]}
|
||||
for el in tab[1:]:
|
||||
presents.update({el})
|
||||
return(100 - len(list(presents)))
|
||||
#c)
|
||||
def moyenne(*args):
|
||||
n=100
|
||||
if args:
|
||||
n = args[0]
|
||||
somme = 0
|
||||
for i in range(n):
|
||||
somme += absents(tableau_hasard())
|
||||
return somme/n
|
||||
|
||||
#Exercice 4
|
||||
#a)
|
||||
def premiere_motie(tab):
|
||||
return tab[:len(tab)//2 + 1]
|
||||
def deuxieme_motie(tab):
|
||||
return tab[len(tab)//2 - 1:]
|
||||
def pairs(tab):
|
||||
return tab[::2]
|
||||
def impairs(tab):
|
||||
return tab[1::2]
|
||||
#b)
|
||||
|
||||
def out_shuffle(tab):
|
||||
tab[::2], tab[1::2] = tab[:len(tab)//2], tab[len(tab)//2:]
|
||||
return tab
|
||||
#c)
|
||||
def nb_shuffle(n=52, shuffle=out_shuffle):
|
||||
ori_tab = list(range(n))
|
||||
tab = shuffle(ori_tab.copy())
|
||||
i = 1
|
||||
while tab != ori_tab:
|
||||
shuffle(tab)
|
||||
i+=1
|
||||
return i
|
||||
#On est à 8 pour le out_shuffle
|
||||
#d)
|
||||
def in_shuffle(tab):
|
||||
tab[::2], tab[1::2] = tab[len(tab)//2:], tab[:len(tab)//2]
|
||||
return tab
|
||||
#On est à 52 pour le in_shuffle
|
||||
|
||||
#Exercice 5
|
||||
def pppremier(n):
|
||||
i = n+1
|
||||
while True:
|
||||
j = 2
|
||||
while j*j <= i:
|
||||
if i % j == 0:
|
||||
break
|
||||
j += 1
|
||||
if j*j > i:
|
||||
return i
|
||||
i += 1
|
||||
|
||||
def premiers1(n=1000):
|
||||
premiers=[2]
|
||||
while premiers[-1]<1000:
|
||||
premiers.append(pppremier(premiers[-1]))
|
||||
if premiers[-1] > 1000:
|
||||
del premiers[-1]
|
||||
return premiers
|
||||
|
||||
def erathostene(n=1000):
|
||||
premiers = list(range(2,1001))
|
||||
i=0
|
||||
while i<len(premiers):
|
||||
j = i+1
|
||||
while j<len(premiers):
|
||||
if premiers[j] % premiers[i] == 0:
|
||||
del premiers[j]
|
||||
else:
|
||||
j+=1
|
||||
i+=1
|
||||
return premiers
|
104
obligatoire/TP6/main.py
Executable file
104
obligatoire/TP6/main.py
Executable file
@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Tue Mar 3 10:11:04 2020
|
||||
|
||||
@author: suwako
|
||||
"""
|
||||
"""
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.image as mpimg
|
||||
import numpy as np
|
||||
import copy as c
|
||||
M=np.array([[1,0,1],[0,1,1],[1,0,1],[0,1,1]])
|
||||
|
||||
## ajoute une ligne tout autour de la matrice M
|
||||
def bord(M):
|
||||
p=np.size(M[:,0])
|
||||
q=np.size(M[0,:])
|
||||
N=np.zeros((p+2,q+2))
|
||||
c=0.3
|
||||
N[:,0]=c
|
||||
N[:,q+1]=c
|
||||
N[0,:]=c
|
||||
N[p+1,:]=c
|
||||
N[1:p+1,1:q+1]=M[0:p,0:q]
|
||||
return(N)
|
||||
##agrandissement de la matrice et création de l'image
|
||||
def lab(M):
|
||||
p=np.size(M[:,0])
|
||||
q=np.size(M[0,:])
|
||||
r=min(512//p,512//q)
|
||||
img=np.zeros((r*p,r*q))
|
||||
for i in range(p):
|
||||
for j in range(q):
|
||||
img[i*r:i*r+r,j*r:j*r+r]=int(M[i,j]*255)
|
||||
return(img)
|
||||
|
||||
img=lab(bord(M))
|
||||
plt.imshow(img,cmap=plt.cm.gray)
|
||||
plt.show()
|
||||
|
||||
##passage à la matrice des triplets et retour
|
||||
def tr1(M):
|
||||
p=np.size(M[:,0])
|
||||
q=np.size(M[0,:])
|
||||
M1=np.array([[[M[i,j],0,0] for j in range(q)] for i in range(p)])
|
||||
return M1
|
||||
|
||||
def tr(M):
|
||||
p=np.size(M[:,0])
|
||||
q=np.size(M[0,:])
|
||||
M1=np.array([[M[i,j][0] for j in range(q//3)] for i in range(p//3)])
|
||||
return M1
|
||||
|
||||
print(tr(tr1(M)))"""
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import random
|
||||
|
||||
def bord(M):
|
||||
return np.pad(np.array(M, dtype='float64'), pad_width=1, mode='constant', constant_values=0.3)
|
||||
|
||||
def img(M):
|
||||
return np.kron(M, np.full((512, 512), 255))
|
||||
|
||||
|
||||
def tr(M):
|
||||
return np.array([[j[0] for j in i] for i in M])
|
||||
|
||||
def tr1(M):
|
||||
return np.array([[(j, 0, 0) for j in i] for i in M])
|
||||
|
||||
def voisin(M, i,j):
|
||||
cases=[case for case in [(i,j-1), (i-1,j), (i+1,j), (i,j+1)] if M[case[0]][case[1]][0]==1]
|
||||
for case in cases:
|
||||
M[case[0]][case[1]]=[0.5, i, j]
|
||||
return cases
|
||||
|
||||
def suivant(M, chemin):
|
||||
return [vois for case in chemin for vois in voisin(M, *case)]
|
||||
|
||||
def chemin(M, depart, fin):
|
||||
voisins=voisin(M, *depart)
|
||||
i=0
|
||||
while fin not in voisins:
|
||||
voisins = suivant(M, voisins)
|
||||
i+=1
|
||||
if i>1000:
|
||||
raise
|
||||
chemin = [fin]
|
||||
while depart != chemin[-1]:
|
||||
i,j=chemin[-1]
|
||||
chemin.append(tuple(map(int,M[i][j]))[1:])
|
||||
return chemin[::-1]
|
||||
def val(p):
|
||||
if random.random()<p:
|
||||
return(0)
|
||||
else:
|
||||
return(1)
|
||||
p=0.35
|
||||
m=15
|
||||
M=tr1(bord(np.array([[val(p) for j in range(m)] for i in range(m)])))
|
||||
print(chemin(M, (1,3), (4,2)))
|
||||
plt.imshow(img(tr(M)), cmap=plt.cm.gray)
|
8
obligatoire/TP6/sanstitre1.py
Executable file
8
obligatoire/TP6/sanstitre1.py
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Fri Mar 6 08:06:39 2020
|
||||
|
||||
@author: suwako
|
||||
"""
|
||||
|
29
obligatoire/TP_Intégrale/main.py
Executable file
29
obligatoire/TP_Intégrale/main.py
Executable file
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Tue Feb 4 10:32:17 2020
|
||||
|
||||
@author: suwako
|
||||
"""
|
||||
|
||||
|
||||
def rectangle(f, a, b, n):
|
||||
h = (b - a) / n
|
||||
aire = 0
|
||||
for i in range(n):
|
||||
aire += f(a+i*h)
|
||||
return aire*h
|
||||
|
||||
def trapeze(f, a, b, n):
|
||||
h = (b - a) / n
|
||||
aire = 0
|
||||
for i in range(n):
|
||||
aire += (f(i*h) + f((i+1)*h))
|
||||
return aire*h/2
|
||||
|
||||
def milieu(f, a, b, n):
|
||||
h = (b - a) / n
|
||||
aire = 0
|
||||
for i in range(n):
|
||||
aire += f((a+h*i + a + h*(i+1))/2)
|
||||
return aire*h
|
BIN
obligatoire/TP_Newton/TP Cours 8.méthode de Newton.pdf
Normal file
BIN
obligatoire/TP_Newton/TP Cours 8.méthode de Newton.pdf
Normal file
Binary file not shown.
94
obligatoire/TP_Newton/main.py
Executable file
94
obligatoire/TP_Newton/main.py
Executable file
@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Fri Mar 6 08:06:39 2020
|
||||
|
||||
@author: suwako
|
||||
"""
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
def newton(f, df, u, xtol=1e-12, Nmax=100):
|
||||
n = 1
|
||||
v = u - f(u) / df(u)
|
||||
while abs(u - v) >= xtol:
|
||||
u, v = v, v - f(v) / df(v)
|
||||
n += 1
|
||||
if n > Nmax:
|
||||
return None
|
||||
return v, n
|
||||
|
||||
def secante(f, u, v, xtol=1e-12, Nmax=100):
|
||||
n = 1
|
||||
while abs(u - v) >= xtol:
|
||||
u, v = v, (u * f(v) - v * f(u)) / (f(v) - f(u))
|
||||
n += 1
|
||||
if n > Nmax:
|
||||
return None
|
||||
return v, n
|
||||
|
||||
def f(x): return x**3 - 2 * x - 5
|
||||
def df(x): return 3 * x * x - 2
|
||||
plt.figure(0)
|
||||
X = np.linspace(-3, 3, 256)
|
||||
Y = [f(x) for x in X]
|
||||
plt.plot(X,Y)
|
||||
plt.grid()
|
||||
plt.show()
|
||||
|
||||
for u in range(-3, 4):
|
||||
x, n = newton(f, df, u)
|
||||
print(f"Pour u0 = {u} on obtient {x} au bout de {n} itérations'")
|
||||
|
||||
nmax = 0
|
||||
for u in range(-3, 4):
|
||||
for v in range(-3, 4):
|
||||
if u != v:
|
||||
x, n = secante(f, u, v)
|
||||
if n > nmax:
|
||||
nmax = n
|
||||
(a, b) = (u, v)
|
||||
print(f"pour u0 = {a} et u1 = {b}, {nmax} itérations sont nécéssaires")
|
||||
|
||||
print(secante(df, 0, 1)[0])
|
||||
print(newton(f, df, secante(df, 0, 1)[0], Nmax=200))
|
||||
|
||||
def f(x): return 3 * np.sin(4*x)+x*x - 2
|
||||
def df(x): return 12*np.cos(4*x)+2*x
|
||||
plt.figure(1)
|
||||
X = np.linspace(-3, 3, 256)
|
||||
Y = [f(x) for x in X]
|
||||
plt.plot(X,Y)
|
||||
plt.grid()
|
||||
plt.show()
|
||||
|
||||
def zeros(f, df, a, b, n=100):
|
||||
zer = []
|
||||
for k in range(n):
|
||||
u = a + np.random.random() * (b - a)
|
||||
x = newton(f, df, u)
|
||||
if x is not None and round(x[0], 12) not in zer:
|
||||
zer.append(round(x[0], 12))
|
||||
return zer
|
||||
from scipy.integrate import quad
|
||||
|
||||
def f(x):
|
||||
return quad(lambda t : np.sqrt(1-t*t)*np.cos(x*t),-1,1)[0]
|
||||
def df(x):
|
||||
return quad(lambda t : -t*np.sqrt(1-t*t)*np.sin(x*t),-1,1)[0]
|
||||
plt.figure(2)
|
||||
X = np.linspace(0, 16, 256)
|
||||
Y = [f(x) for x in X]
|
||||
plt.plot(X,Y)
|
||||
plt.grid()
|
||||
plt.show()
|
||||
|
||||
def derivee(f,x,h):
|
||||
return (f(x+h)-f(x-h)/(2*h))
|
||||
def delta(p):
|
||||
return abs(np.cos(1)-derivee(np.sin,1,10**(-p)))
|
||||
plt.figure(3)
|
||||
P= np.arange(4,8,.1)
|
||||
D=[delta(p) for p in P]
|
||||
print(D)
|
||||
plt.plot(P,D)
|
||||
plt.show()
|
18
obligatoire/TP_Newton/sanstitre1.py
Executable file
18
obligatoire/TP_Newton/sanstitre1.py
Executable file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Sat Mar 28 09:14:39 2020
|
||||
|
||||
@author: suwako
|
||||
"""
|
||||
|
||||
def genere(n):
|
||||
u=[83 for _ in range(n)]
|
||||
l=[83%2 for _ in range(n)]
|
||||
for i in range(1,n):
|
||||
u[i] = (16365*u[i-1]) % 65521
|
||||
l[i] = u[i]%2
|
||||
return l
|
||||
|
||||
def palindrome(s):
|
||||
return s[:len(s)//2] == s[:len(s)//2:-1]
|
51
spe/DS.ml
Normal file
51
spe/DS.ml
Normal file
@ -0,0 +1,51 @@
|
||||
let indice_ming t =
|
||||
let min = ref t.(0) in
|
||||
let imin = ref 0 in
|
||||
for i=1 to (Array.length t) - 1 do
|
||||
if t.(i) <= !min then
|
||||
begin
|
||||
min := t.(i);
|
||||
imin := i
|
||||
end
|
||||
done;
|
||||
!imin;;
|
||||
|
||||
indice_ming [|1;5;3;2;-1;9|];;
|
||||
[|1;5;3;2;-1;9|].(4);;
|
||||
|
||||
let premier_indice_minl t =
|
||||
let i = ref 0 in
|
||||
let n = Array.length t in
|
||||
let continue = ref true in
|
||||
while !continue do
|
||||
if !i = (n - 1) then
|
||||
continue := false
|
||||
else if (!i = 0) then
|
||||
if (t.(!i) <= t.(!i + 1)) then
|
||||
continue := false
|
||||
else i := !i + 1
|
||||
else if (t.(!i) <= t.(!i - 1)) && (t.(!i) <= t.(!i + 1)) then
|
||||
continue := false
|
||||
else
|
||||
i := !i + 1
|
||||
done;
|
||||
!i;;
|
||||
premier_indice_minl [|7;5;3;2;1;|];;
|
||||
|
||||
let indice_minl t =
|
||||
let rec aux is ie =
|
||||
let k = (is + ie)/2 in
|
||||
if (ie - is) <= 1 then
|
||||
if t.(is) <= t.(ie) then
|
||||
is
|
||||
else ie
|
||||
else if t.(k) >= t.(k-1) then
|
||||
aux is (k - 1)
|
||||
else if t.(k) >= t.(k+1) then
|
||||
aux (k + 1) ie
|
||||
else
|
||||
k in
|
||||
aux 0 (Array.length t - 1);;
|
||||
let t = [|0;1;2;3;4;|];;
|
||||
indice_minl t;;
|
||||
t.(indice_minl t);;
|
84
spe/DS1.ml
Normal file
84
spe/DS1.ml
Normal file
@ -0,0 +1,84 @@
|
||||
(* Exercice 1 *)
|
||||
|
||||
let dichotomie x t = ();;
|
||||
|
||||
(* Exercice 2 *)
|
||||
|
||||
let rec f x n = match n with
|
||||
| 0 -> 1
|
||||
| _ -> x * (f x (n - 1));;
|
||||
|
||||
f 10 2;;
|
||||
|
||||
let puissance x n =
|
||||
let res = ref 1 in
|
||||
for i=0 to n-1 do
|
||||
res := !res * x
|
||||
done;
|
||||
!res;;
|
||||
|
||||
puissance 2 2;;
|
||||
|
||||
(* Exercice 3 *)
|
||||
|
||||
(* 5 *)
|
||||
let rec pgcd a b = match min (abs a) (abs b) with
|
||||
| 0 -> max (abs a) (abs b)
|
||||
| 1 -> 1
|
||||
| _ -> pgcd ((max (abs a) (abs b)) mod (min (abs a) (abs b))) (min (abs a) (abs b));;
|
||||
|
||||
let rec pgcd a b = match b with
|
||||
| 0 -> a
|
||||
| -> pgcd b (a mod b);;
|
||||
|
||||
|
||||
(* 7 *)
|
||||
let bezout a b =
|
||||
let rec aux u0 v0 u1 v1 r0 r1 = match r0 mod r1 with
|
||||
| 0 -> r1,u1,v1
|
||||
| _ -> aux u1 v1 (u0 - ( (r0 / r1) * u1 ) ) (v0 - ( (r0 / r1) * v1 )) r1 (r0 mod r1) in
|
||||
aux 1 0 0 1 a b;;
|
||||
|
||||
bezout 24 65;;
|
||||
bezout 6 6;;
|
||||
|
||||
pgcd (-1024) 4096;;
|
||||
|
||||
max (abs 1) (abs 2) ;;
|
||||
abs;;
|
||||
|
||||
(* Exercice 4 *)
|
||||
|
||||
let next n = match n mod 2 with
|
||||
| 0 -> n/2
|
||||
| _ -> 3 * n + 1;;
|
||||
let
|
||||
next 1;;
|
||||
|
||||
let rec syracuse n = match n with
|
||||
| 1 -> [1]
|
||||
| _ -> n::syracuse (next n);;
|
||||
syracuse 5;;
|
||||
|
||||
let rec altitude_maximale n = match n with
|
||||
| 1 -> 1
|
||||
| _ -> max n (altitude_maximale (next n));;
|
||||
|
||||
syracuse 9;;
|
||||
altitude_maximale 9;;
|
||||
|
||||
let dav n =
|
||||
let vmax = ref 0 in
|
||||
let v = ref 0 in
|
||||
let i = ref n in
|
||||
while !i <> 1 do
|
||||
i := next !i;
|
||||
if !i > n then
|
||||
v := !v + 1
|
||||
else
|
||||
v := 0;
|
||||
if !v > !vmax then
|
||||
vmax := !v
|
||||
done;
|
||||
!vmax;;
|
||||
dav 9;;
|
55
spe/TD2/main.ml
Normal file
55
spe/TD2/main.ml
Normal file
@ -0,0 +1,55 @@
|
||||
(* Exercice 1 *)
|
||||
(* 1 *)
|
||||
let selection t =
|
||||
let rec maxi fin =
|
||||
if fin = 0 then
|
||||
0
|
||||
else
|
||||
begin
|
||||
let m = maxi (fin - 1) in
|
||||
if t.(fin) > t.(m) then
|
||||
fin
|
||||
else
|
||||
m
|
||||
end in
|
||||
|
||||
let rec aux fin =
|
||||
if fin = 0 then ()
|
||||
else
|
||||
begin
|
||||
let f = t.(fin) in
|
||||
let m = maxi fin in
|
||||
t.(fin) <- t.(m);
|
||||
t.(m) <- f;
|
||||
aux (fin - 1)
|
||||
end in
|
||||
|
||||
aux (Array.length t - 1);;
|
||||
|
||||
let a = [|1;4;2;-1;2;3;4;2;9|];;
|
||||
selection a;;
|
||||
a;;
|
||||
(* 2 *)
|
||||
(* Dans le pire des cas, la fonction effectuera en tout n! comparaisons *)
|
||||
(* 3 *)
|
||||
(* Dans le meilleur des cas, la fonction effectuera également n! comparaisons *)
|
||||
|
||||
(* Exercice 2 *)
|
||||
let rec min_liste l = match l with
|
||||
| [] -> failwith "La liiste estttt"
|
||||
| [a] -> (a, [])
|
||||
| h::t ->
|
||||
let m, l = min_liste t in
|
||||
if h < m then
|
||||
(h, t)
|
||||
else
|
||||
(m, h::l);;
|
||||
min_liste [4;2;4;1;5];;
|
||||
let rec l_selection l = match l with
|
||||
| [] -> []
|
||||
| [a] -> [a]
|
||||
| _ -> let m, lst = min_liste l in m::(l_selection lst);;
|
||||
l_selection [1;2;6;7;3;2;-6;3];;
|
||||
|
||||
let rec insertion l = match l with
|
||||
|
51
spe/TD3/main.ml
Normal file
51
spe/TD3/main.ml
Normal file
@ -0,0 +1,51 @@
|
||||
(* Exercice 1 *)
|
||||
|
||||
let scinder l =
|
||||
let rec aux acc1 acc2 n l = match l with
|
||||
| [] -> (acc1, acc2)
|
||||
| h::t -> if n = 0 then aux (h::acc1) acc2 1 t
|
||||
else aux acc1 (h::acc2) 0 t in
|
||||
aux [] [] 0 l;;
|
||||
scinder [8;5;4;3;1];;
|
||||
|
||||
let fusionner l1 l2 =
|
||||
let rec aux acc l1 l2 = match (l1,l2) with
|
||||
|([],[]) -> acc
|
||||
|([],h::t) -> aux h::acc [] t
|
||||
|(h::t,[]) -> aux h::acc t []
|
||||
|(h1::t1,h2::t2) -> if h1>h2 then aux (h2::acc) l1 t2
|
||||
else aux (h1::acc) t1 l2 in
|
||||
let rec reverse acc l = match l with
|
||||
| [] -> acc
|
||||
| h::t -> reverse (h::acc) t in
|
||||
reverse [] (aux [] l1 l2);;
|
||||
|
||||
fusionner [5;7;11] [2;4;8;10];;
|
||||
|
||||
let rec tri_fusion l = match l with
|
||||
|[] -> []
|
||||
|[a] -> [a]
|
||||
|_ -> let (l1,l2) = scinder l in fusionner (tri_fusion l1) (tri_fusion l2);;
|
||||
|
||||
tri_fusion [3;1;2;666];;
|
||||
|
||||
(* Exerice 2 *)
|
||||
|
||||
let partition k l =
|
||||
let rec aux acc1 acc2 l = match l with
|
||||
|[] -> (acc1,acc2)
|
||||
|h::t -> if h>=k then aux acc1 (h::acc2) t
|
||||
else aux (h::acc1) acc2 t in
|
||||
aux [] [] l;;
|
||||
|
||||
partition2 2 [3;1;666;2;0;-1];;
|
||||
|
||||
let rec tri_pivot l = match l with
|
||||
|[] -> []
|
||||
|h::t -> let (l1,l2) = partition h t in (tri_pivot l1)@(h::(tri_pivot l2));;
|
||||
|
||||
tri_pivot [3;1;666;2;0;-1];;
|
||||
|
||||
let a = ref 1;;
|
||||
decr a;;
|
||||
a;;
|
@ -55,7 +55,7 @@ let rec recherche_dicho d f a v =
|
||||
let recherche_dichotomique a v =
|
||||
recherche_dicho 0 (Array.length v) a v;;
|
||||
|
||||
recherche_dichotomique (-5) [|1;2;3;4;5;6;7;8;9;10;11|];;
|
||||
recherche_dichotomique (-5) [|1;2;3;4;5;6;7;8;9;10;5|];;
|
||||
|
||||
|
||||
(*Exercice 5*)
|
||||
|
92
spe/TP3/main.ml
Normal file
92
spe/TP3/main.ml
Normal file
@ -0,0 +1,92 @@
|
||||
(* Exercice 1 *)
|
||||
let rec fibo n = match n with
|
||||
|0 -> 0
|
||||
|1 -> 1
|
||||
|_ -> (fibo (n-1)) + (fibo (n-2));;
|
||||
let fibot n =
|
||||
let rec aux acc1 acc2 n = match n with
|
||||
| 0 -> acc1
|
||||
| _ -> aux acc2 (acc2+acc1) (n-1) in
|
||||
aux 0 1 n;;
|
||||
|
||||
let comp fa fb i j =
|
||||
let debuta =Sys.time () in
|
||||
fa i j;
|
||||
let fina = Sys.time () in
|
||||
let debutb = Sys.time () in
|
||||
fb i j;
|
||||
let finb = Sys.time() in
|
||||
print_float (fina -. debuta);
|
||||
print_newline ();
|
||||
print_float (finb -. debutb);;
|
||||
fibo 20;;
|
||||
Sys.time ();;
|
||||
comp fibot fibot 50000000;;
|
||||
|
||||
(* Exercice 2 *)
|
||||
|
||||
let rec numero x y = match y with
|
||||
|0 when x = 0 -> 0
|
||||
|0 -> numero 0 (x-1) + 1
|
||||
|_ -> numero (x+1) (y-1) + 1;;
|
||||
let numero_ter x y =
|
||||
let rec aux acc x y = match y with
|
||||
|0 when x = 0 -> acc
|
||||
|0 -> aux (acc+1) 0 (x-1)
|
||||
|_ -> aux (acc+1) (x+1) (y-1) in
|
||||
aux 0 x y;;
|
||||
numero_ter 2 2;;
|
||||
let rec suite x y = match x with
|
||||
|0 -> [numero_ter 0 y]
|
||||
|_ -> (numero_ter x y)::(suite (x-1) y);;
|
||||
suite 100 0;;
|
||||
let numero_formule x y = (x+y)*(x+y+1)/2 + y ;;
|
||||
numero 6 2;;
|
||||
|
||||
(* Exercice 3 *)
|
||||
|
||||
let binome k n =
|
||||
let m = Array.make_matrix (n+1) (k+1) 0 in
|
||||
for i = 0 to n do
|
||||
for j = 0 to k do
|
||||
if j = 0 then m.(i).(j) <- 1
|
||||
else if i = 0 then m.(i).(j) <- 0
|
||||
else m.(i).(j) <- m.(i-1).(j) + m.(i-1).(j-1)
|
||||
done
|
||||
done;
|
||||
m.(n).(k);;
|
||||
let rec binrec k n = match (k,n) with
|
||||
| (0,n) -> 1
|
||||
| (k,0) -> 0
|
||||
| _ -> binrec (k-1) (n-1) + binrec k (n-1);;
|
||||
binrec 2 7;;
|
||||
binome 2 7;;
|
||||
|
||||
comp binrec binome 12 22;;
|
||||
(* La version itérative est plus rapide. *)
|
||||
(* En effet, la version itérative ne calcule qu'une seule fois chacun
|
||||
des coefficients là où la version récursive calcule à plusieures reprises
|
||||
les mêmes coefficients *)
|
||||
|
||||
(* Exo Bonus *)
|
||||
|
||||
let rec binr n = match n with
|
||||
| 0 -> [""]
|
||||
| _ -> List.map (fun s -> "1"^s) (binr (n-1)) @ List.map (fun s -> "0"^s) (binr (n-1));;
|
||||
binr 5;;
|
||||
|
||||
let binit n =
|
||||
let rec puis x n = match n with
|
||||
| 0 -> 1
|
||||
| _ -> x * (puis x (n-1)) in
|
||||
let aux = fun x -> match x with | true -> 0 | false -> 1 in
|
||||
let res = Array.make (puis 2 n) "" in
|
||||
for i = 0 to (puis 2 n) - 1 do
|
||||
let cur = ref "" in
|
||||
for j = n downto 1 do
|
||||
cur:= !cur ^ string_of_int (aux ( (i / (j)) mod 2 = 0))
|
||||
done;
|
||||
res.(i) <- !cur
|
||||
done;
|
||||
res;;
|
||||
binit 3;;
|
23
spe/TP4/main.ml
Normal file
23
spe/TP4/main.ml
Normal file
@ -0,0 +1,23 @@
|
||||
(* Exercice 1 *)
|
||||
|
||||
let rec invnaif l =
|
||||
let rec perm c l = match l with
|
||||
| [] -> 0
|
||||
| h::t -> if c > h then (perm c t) + 1 else (perm c t) in
|
||||
match l with
|
||||
| [] -> 0
|
||||
| h::t -> (perm h t) + (invnaif t);;
|
||||
|
||||
invnaif [3;2;1];;
|
||||
|
||||
(* Exercice 2 *)
|
||||
|
||||
let scinder l =
|
||||
let rec aux acc1 c l = match l with
|
||||
| [] -> ((List.rev acc1), [])
|
||||
| h::t when c=0 -> ((List.rev acc1), t)
|
||||
| h::t -> aux (h::acc1) (c-1) t
|
||||
in
|
||||
aux [] ((List.length l)/2) l ;;
|
||||
scinder [1;2;3;4;5;6;7];;
|
||||
|
BIN
spe/decomp
Executable file
BIN
spe/decomp
Executable file
Binary file not shown.
BIN
spe/decomp.cmi
Normal file
BIN
spe/decomp.cmi
Normal file
Binary file not shown.
BIN
spe/decomp.cmx
Normal file
BIN
spe/decomp.cmx
Normal file
Binary file not shown.
53
spe/decomp.ml
Normal file
53
spe/decomp.ml
Normal file
@ -0,0 +1,53 @@
|
||||
open Format
|
||||
let premiers = [1;2;4;8;16;32;64;128;256;512;1024;2048;4096;8192];;
|
||||
let rec print_int_list l = match l with
|
||||
|[] -> printf "\n"
|
||||
|h::t -> begin printf "%d " h;print_int_list t end;;
|
||||
let print_sum l =
|
||||
let rec aux l = match l with
|
||||
| [] -> printf "\n"
|
||||
| h::t -> begin printf " + %d" h;aux t end in
|
||||
match l with
|
||||
|[] -> printf "0\n"
|
||||
|h::t -> begin printf "%d" h;aux t end;;
|
||||
let rec sum l = match l with
|
||||
| [] -> 0
|
||||
| h::t -> h + sum t;;
|
||||
let rec dans a l = match l with
|
||||
| [] -> false
|
||||
| h::t -> if h=a then true else dans a t ;;
|
||||
let rec retirer acc base modulo = match base with
|
||||
| [] -> acc
|
||||
| h::t -> if dans h modulo then retirer acc t modulo
|
||||
else retirer (h::acc) t modulo ;;
|
||||
let rec somme accn accl ban lim l = match l with
|
||||
| [] -> (accn,accl,true)
|
||||
| h::t -> if accn < lim then somme (accn+h) (h::accl) ban lim t
|
||||
else if dans (accn - lim) ban then somme (accn+h) (h::accl) ban lim t
|
||||
else (accn,accl,false);;
|
||||
let decomp n =
|
||||
let imp = ref [1] in
|
||||
let rec aux impp n =
|
||||
if dans n !imp then raise Not_found else
|
||||
if dans n premiers then [n] else
|
||||
let (a,l,f) = somme 0 [] !imp n premiers in
|
||||
print_int_list (n::a::0::l);
|
||||
match a - n with
|
||||
|0 -> l
|
||||
|d (*when d < n*) -> begin try retirer [] l (aux impp d) with
|
||||
| Not_found when f -> raise Not_found
|
||||
| Not_found -> begin imp:=(d::(!imp)); aux (d::impp) n end end
|
||||
(*|d -> raise Not_found*) in
|
||||
aux [1] n;;
|
||||
for i = 0 to sum premiers do
|
||||
try begin let d = decomp i in printf "%d = " i; print_sum d end with
|
||||
| Not_found -> printf "%d : Pas de decomposition\n" i done;;
|
||||
(*
|
||||
let max = ref 0 in
|
||||
let imax = ref 0 in
|
||||
for i = 0 to sum premiers do
|
||||
let c = ref 0 in
|
||||
try let a=sum (decomp c i) in printf "Oui :%d : %d\n" i !c with
|
||||
| Not_found -> printf "Non :%d : %d\n" i !c;
|
||||
if !c > !max then begin max := !c; imax := i end done;
|
||||
printf "%d : %d" !imax !max;; *)
|
BIN
spe/decomp.o
Normal file
BIN
spe/decomp.o
Normal file
Binary file not shown.
24134
spe/liste.txt
Normal file
24134
spe/liste.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user