-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexo-array.ml
123 lines (89 loc) · 2.82 KB
/
exo-array.ml
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
let appartient x t =
let test = ref false in
let c = ref 0 in
while !c< Array.length t && not(!test) do
test:= t.(!c)=x ; c:=!c+1
done ; !test ;;
let croissant t =
let c = ref 0 in
while !c< Array.length t -1 && t.(!c)<=t.(!c+1) do
c:=!c+1
done ; !c=Array.length t -1 ;;
let est_palindrome t =
let c = ref 0 in
let n = Array.length t in let m = n/2 in
while !c<m && t.(!c)=t.(n-1-(!c)) do
c:=!c+1
done ; !c=m ;;
let swap t i j = let a = t.(i) in t.(i)<-t.(j) ; t.(j)<- a ;;
let renverse t =
let n = Array.length t -1 in
for c=0 to n/2 do
swap t c (n-c)
done ;;
let occurences x t =
let l = ref [] in
for c= 0 to Array.length t -1 do
if t.(c)= x then l:= c::(!l)
done ; !l ;;
let indices_mini t =
let l= ref [] in
let m = ref t.(0) in
for c =1 to Array.length t -1 do
if t.(c)<(!m) then (m:=t.(c) ; l:=[c]) else if t.(c)=(!m) then l:=c::(!l)
done ; !l ;;
let filtre pred t =
let l=ref [] in
for c=0 to Array.length t -1 do
if pred t.(c) then l:=t.(c)::(!l)
done ; !l ;;
let cherche_somme t s =
let test = ref None in
let n = Array.length t in
let i=ref 0 in
while !i<n && !test = None do
( let j = ref !i in
while !j<n && !test = None do
if t.(!j)+t.(!i)=s then test:= Some (!i,!j) ; j:=!j+1 done) ;
i:=!i+1
done ; !test ;;
let cherche_somme_tri t s =
let test = ref None in
let n = Array.length t in
let i=ref 0 in
while !i<n && t.(!i)<=s && !test = None do
(let j = ref (!i) in
while !j<n && t.(!j)<=s-t.(!i) && !test = None do
if t.(!j)+t.(!i)=s then test:= Some (!i,!j) ; j:=!j+1 done) ;
i:=!i+1
done ; !test ;;
let est_equilibre t a b =
let v = ref 0 in let f = ref 0 in
for c = a to b-1 do
if t.(c) then v:=!v+1 else f:=!f+1
done ; !v=(!f) ;;
let max_equilibre t =
let l = ref 0 in
for a= 0 to Array.length t -1 do
for b = 0 to Array.length t do
if est_equilibre t a b then if b-a>(!l) then l:=b-a
done
done ; !l ;;
let max_depuis t a =
let l = ref 0 in let v = ref 0 in let f = ref 0 in
for b=a to Array.length t-1 do
if t.(b) then v:=!v+1 else f:=!f+1 ; if !v-(!f)>(!l) then l:= b-a
done ; !l ;;
let max_equilibre_eff t =
let l = ref 0 in
for a = 0 to Array.length t-1 do
let mdp=max_depuis t a in if mdp>(!l) then l:=mdp
done ; !l ;;
let max_equilibre_ultraeff t = (* on trouve l tel qu'on maximise et tel qu'on minimise et bim on fait la différence en longueur *)
let lmax = ref 0 in
let lmin = ref 0 in
let v= ref 0 in
let f = ref 0 in
for x = 0 to Array.length t -1 do
if t.(x) then v:=!v+1 else f:=!f+1 ; let a = !v-(!f) in if a<(!lmin) then lmin:=a else if a>(!lmax) then lmax:=a
done ; (!lmax -(!lmin)) ;;