0% found this document useful (0 votes)
9 views

TP2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

TP2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TP 2

#EX1
L1=[1,2,3] # cree et initialiser la liste L1
print(L1[0]) # afficher le premier element 1
print(L1[-1]) # afficher le dernier element 3
print(L1) # afficher tous les elements de L1
L2=["Ali",20, "casa", 'A'] # cree et initialiser la liste L2
L2[2] #retourner le troisieme element de L2
L3=L1+L2 #cree une liste qui contienne les elements de L1 et L2
print(L3) # afficher tous les element de L3
L4=L1*4 # cree une liste L4 qui contienne les elements de L1 4 fois
L4
L5=L1 # l5 pernd une copy de L1
L5-L1 #erreur
L5*L1 # erreur
L1.append(10) # ajouter 10 a la fin de L1
L=[] # cree liste vide
L=L+[2] # ajouter 2 a L
print(L)# afficher [2]

#EX2
li[3:7] # [5,89,92.7]
li[3:] # [5,89,92.7,2]
li[0:-1] # [10,67,45,5,89,92.7]
li[:-2] ## [10,67,45,5,89,92]
li[5:6]=[] ;li # li=[10,67,45,5,89,7,2]
li[:0]=['a','b','c']#['a','b','c',10,67,45,5,89,7,2]
li[2:2]=['a','b','c']#['a','b','a','b','c','c',10,67,45,5,89,7,2]
#EX3
from random import randint
#Q1
def InitialiserTab(n):
T=[]
for i in range (n+1):
T.append(0)
#Q2
def InitialiserTabAlea(n):
T=[]
for i in range (n+1):
Num= randint(0,100)
T.append(Num)
#EX4
#Q1
def AjoutDebut(L,x):
L[0:0] = [x]
#Q2
def AjoutFin(L,x):
L.append(x)
#Q3
def AjoutMilieu (L,x,p):
TP 2

L[p:p] = [x]
#EX5
#Q1
def SupprimerDebut(L):
L[0:1] = []
#Q2
def SupprimerFin(L):
L.pop()
#Q3
def SupprimerElement(L,x):
# Methode 1
L.remove(x)
#Methode 2
for i in range (len(L)):
if L[i] == x :
L[i:i+1]=[]
break
#EX6
#Q1
def makelist(n, x):
L = [x] * n
return L
#Q2
def liste_puissances_2(n):
L = []
for i in range(n):
L.append(2**i)
return L
#Q3
def eleMax(L):
Max = -inf
for i in range(len(L)):
if L[i] > Max:
Max = L[i]
return Max
#Q4
def eleMaxIn(L, debut, fin):
L2 = L[debut:fin+1]
return eleMax(L2)
#EX7
#Q1
def sommeElt(L):
som = 0
for i in range(len(L)):
som += L[i]
return som

#Q2
def Produit(L):
TP 2

prdt = 1
for i in range(len(L)):
prdt *= L[i]
return prdt

#Q3
def MoyenneElt(L):
return sommeElt(L) / len(L)
#EX8
#Q1
def sommeListe(L1, L2):
L3 = []
if len(L1) > len(L2):
Tsup = L1 ; Tinf = L2
else:
Tsup = L2 ; Tinf = L1

for i in range(len(Tinf)):
L3.append(L1[i] + L2[i])

L3 = L3 + Tsup[len(Tinf):]

return L3
#Q2
def MultiSca(L, a):
for i in range(len(L)):
L[i] *= a
return L
#EX9
#Q1
def U(n, a):
if n == 0:
return a
elif n % 2 == 0:
return (U((n-1),a)/2)
elif n % 2 != 0:
return (3*U((n-1),a)+1)
#Q2
def ListeU(n, a):
L = []
for i in range(n):
L.append(U(i,a))
return L
#EX10
#Q1
def existe(T, x):
for i in range(len(T)):
if T[i] == x: return True
return False
TP 2

#Q2
def renverser(T):
return T[::-1]
# Méthode 2
i = 0
while i < len(T):
if T[i] == 0:
T[i:i+1] = []
i -= 1
i += 1
#EX11
#Q1
def singletons(L):
i = len(L) - 1
while i >= 0:
if L.count(L[i]) > 1:
L[i:i+1] = []
i -= 1
return L
#Q2
def nbOccurences(x, L):
Com = 0
for i in range(len(L)):
if L[i] == x:
Com += 1
return Com
#Q3
def plusFrequent(L):
PFrq = 0
IndexPFrq = 0
for i in range(len(L)):
if nbOccurences(L[i], L) == PFrq and L[i] > L[IndexPFrq]:
IndexPFrq = i
elif nbOccurences(L[i], L) > PFrq:
PFrq = nbOccurences(L[i], L)
IndexPFrq = i
return L[IndexPFrq]

You might also like