import random as rd n = 10
a = [rd.randint(0,4) for k in range(n)]
s, t = 0, [0]
for k in range(1, n):
s += rd.randint(1, 2) t.append(s)
print('a : ', a) print('t : ', t)
# Q1
def compte(x, a):
nombre = 0 for elt in a:
if elt == x:
nombre += 1 return nombre
# Q1 bis
def compte_bis(x, a):
return len([u for u in a if u == x])
# Q2
def occurrences(a):
return [compte(a[i], a) for i in range(len(a))]
# Q3
def maxconstant(a):
a.append(-2) # sentinelle resultat, constant_depuis = 0, 0
for j in range(1, len(a)):
if a[j] != a[j-1]:
resultat_courant = t[j-1] - t[constant_depuis]
if resultat_courant > resultat:
resultat = resultat_courant constant_depuis = j
a.pop()
return resultat
print('maxconstant(a) : ', maxconstant(a))
1
# Q4
def maxocurrences(a, occ):
i1, i2 = 0, -1
for j in range(1, len(a)):
if occ[j] > occ[i1]:
i1, i2 = j, i1
if (i2 == -1 or occ[j] > occ[i2]) and a[j] !=
a[i1]:
i2 = j return i1, i2
# Q5
def trier(a, m1, m2):
n = len(a)
d2, d3, d4 = 0, 0, n while d3 < d4:
if a[d3] == m1:
a[d2], a[d3] = a[d3], a[d2]
d2 += 1 d3 += 1 else:
if a[d3] == m2:
d3 += 1 else:
a[d4 - 1], a[d3] = a[d3], a[d4 - 1]
d4 -= 1
# Q6 : non
occ = occurrences(a)
i1, i2 = maxocurrences(a, occ) print('i1, i2 : ', i1, i2) m1, m2 = a[i1], a[i2]
print('m1, m2 : ', m1, m2)
trier(a, m1, m2) print(a)
2