You are on page 1of 6

serie 3

exercice 1:
----------------------------------------
from math import *
from array import array
def chercher(x, T):
p = 0
for e in T:
if e == x :
return print(p)
p = p + 1
return print("-1")
T = array('i', [2, 6, 8, 9, 1])
chercher(0, T)

exercice 2:
---------------------------------------
1)
from math import *
from array import array
def moyenne(T):
p = len(T)
som=0
for e in T:
som=som+e
return (som/p)
T = array('i', [2, 0, 1, 3, 4])
moyenne(T)

2)
from math import *
from array import array
def minTableau(T):
min=T[0]
for e in T:
if e < min:
min = e
return (min)
T = array('i', [2, 9, 1, 4])
minTableau(T)

3) (fausse reflx)
from math import *
from array import array
def sommeVecteur(U, V):
T=array('f')
for i in range( len(U)):
aide =U[i]+V[i]
T.append(aide)
return T
U = array('i', [2, 9, 1])
V = array('i', [3, 7, 5])
sommeVecteur(U, V)

exercice 3:
---------------------------------------------
from math import *
from array import array
def suppOcc(x, V):
for e in V:
if e == x:
V.remove(e)
for a in V:
print(a)
V = array('i', [2, 9, 1, 3, 9, 8, 5, 0, 9])
suppOcc(9, V)

exercice 4:
---------------------------------------------
from math import *
from array import array
def trieListe(L):
L.sort()
print(L)
A = [25,13,6,17,9]
trieListe(A)

exercice 5:
--------------------------------------------
1)
from math import *
from array import array
def grands(L, x):
p=0
for i in range (len(L)):
if L[i] > x:
p= p+1
return p
A = [25,13,6,17,9]
grands(A, 10)

2)
from math import *
from array import array
def petits(L, x):
p=0
for i in range (len(L)):
if L[i] < x:
p= p+1
print(L[i])
return print("le nb des elemants inferieurs est: ", p)
A = [25,13,6,17,9]
petits(A, 10)

3)
from math import *
from array import array

def petits(L, x):


p=0
for i in range (len(L)):
if L[i] < x:
p= p+1
return p
def grands(L, x):
p=0
for i in range (len(L)):
if L[i] < x:
p= p+1
return p

def medianL(L):
for e in L:
if (petits(L, e) <= len(L)//2) and (grands(L, e) <= len(L)//2):
return e
A = [25,13,6,17,9,6]
c= medianL(A)
print("le nb des elements inferieurs est: ", c)

exercice 6:
-----------------------------------------
from math import*
from array import array
def afficheSousSequences(T):
L = [T[0]]
Z = []
for i in range(len(T)-1):
if(T[i+1] >= T[i]):
L.append(T[i+1])
else:
Z.append(L)
L=[T[i+1]]
return Z

T = [1,2,5,3,12,25,13,8,4,7,24,28,32,11,14]
A = afficheSousSequences(T)
print(A)

exercice7:
----------------------------------------------
1)-
from math import *
from array import array
def insertion (T1, T2):
L=[]
for e in T1:
for m in T2:
if(T1 == T2):
L.append(T1)

from math import *


from array import array
def Insertion(T1, T2):
return list(set(T1)&set(T2))
T1=[0.3, 6, 8, 26, 19, 33, 5, 11]
T2=[0.3, 8, 16, 12, 33]
print(Insertion(T1, T2))

Exercice 8:
--------------------------------------------------------
from math import *
def frequence(L):
A=set(L)
L1=[]
for i in A:
L1.append((i,L.count(i)))
return(L1)
L=[12,29,46,29,31,8,12,55,29,8,12,29,31,29,8,29,8]
print(frequence(L))

Exercice 9:
-------------------------------------------------------
1)
from math import *
def fibo(n):
if(n >= 2):
return (n-1)+(n-2)
else:
return 1
print(fibo(5))

2)
def SuiteFibo(n):
F = []
Z = []
x=0
y=1
while y <= n:
F.append(y)
x=y
y=x+y
Z = [0] * len(F)
return [F, Z]
print(SuiteFibo(50))

3)
def coder(n):
F = []
C = []
x=0
y=1
while y <= n:
F.append(y)
x=y
y=x+y
aide = n
for fibo in F:
if aide >= fibo:
C.append(1)
aide = aide - fibo
else:
C.append(0)
return [F, C]
print(Coder(50))

4)
def Decoder(X):
F = [0, 1]
v = 0
for i, x in enumerate(X):
if x == 1:
v = v + F[i]
if i < (len(X) - 1):
F.append(F[-1] + F[-2])
return v
X=[0, 0, 1, 0, 0, 1, 0, 1]
print(Decoder(X))

exercice 10:
---------------------------------------------------
1)
def Compresser(ch):
sc = ''
count = 1
for i in range (0, len(ch)):
if ch[i] != ch[i-1]:
sc = sc + ch[i]
return sc
print(Compresser('VVVVUUCCCJHHJJJJJ'))
_________________________________________________________
from math import *
def Compresser(S):
SC = ""
L = []
count = 1
for i, c in enumerate(S):
if i < len(S) - 1 and c == S[i+1]:
count += 1
else:
SC += c
L.append(count)
count = 1
return (SC, L)
print(Compresser('AAAMMRRRRXAA'))

3)
def Decompresser(sc, L):
S = ""
for i in range(len(sc)):
if i % 2 == 0:
char = sc[i]
count = L[i//2]
S = S + char * compteur
return S
print(Decompresser('AMRXA', [3,2,4;1,2]))

4)
def Coder(S):
sc = ""
compteur = 1
for i in range(1, len(S)):
if S[i] == S[i-1]:
compteur += 1
else:
sc = sc + S[i-1] + str(compteur)
compteur = 1
sc = sc + S[-1] + str(compteur)
return sc
print(Coder('AAAMMRRRRXAA'))

5)
def Decoder(sc):
S = ""
compteur = 0
for i in range(0, len(sc), 2):
aide = sc[i]
compteur = int(sc[i+1])
S = S + aide * compteur
return S
print(Decoder(M5D3X2R1T3))

You might also like