MPSI 832 Les listes : Correction des entrainements
# ================ E n t r a i n e m e n t 1 ================
def somme ( L ) : S = 0
f o r x in L : S += x return S
# ================ E n t r a i n e m e n t 2 ================
def s u i t e 2 ( n ) : L = [ 1 . ]
f o r k in range( n ) : u = 0
f o r i in range( k + 1 ) : u += L [ i ] / ( k + 1 − i ) L . append ( u )
return L[−1 ]
# ================ E n t r a i n e m e n t 3 ================
def indice_max ( L ) : r e c o r d = −1
f o r i in range(len( L ) ) : i f L [ i ] > r e c o r d :
i n d _ r e c o r d = i r e c o r d = L [ i ] return i n d _ r e c o r d
# ================ E n t r a i n e m e n t 4 ================
def a l t e r n e ( L , M) : P = [ ]
f o r i in range(len( L ) ) : P . append ( L [ i ] ) P . append (M[ i ] ) return P
# ================ E n t r a i n e m e n t 5 ================
def i m a g e s ( f , L ) : M = [ ]
f o r x in L :
M. append ( f ( x ) ) return M
def a p p l i q u e r ( f , L ) :
f o r i in range(len( L ) ) : L [ i ] = f ( L [ i ] )
1
MPSI 832 Les listes : Correction des entrainements
# ================ E n t r a i n e m e n t 6 ================
def e s t _ c r o i s s a n t ( L ) : i = 1
n = len( L )
while i < n and L [ i − 1 ] <= L [ i ] : i += 1
return i == n
def e s t _ c r o i s s a n t _ f o r ( L ) : f o r i in range( 1 , len( L ) ) :
i f L [ i − 1 ] > L [ i ] : return F a l s e return True
# ================ E n t r a i n e m e n t 7 ================
def supprime ( L , x ) : i = 0
while i < len( L ) : i f L [ i ] == x : del L [ i ] e l s e:
i += 1
# Une v e r s i o n naï v e a v e c f o r def supprime_faux ( L , x ) :
f o r k in range(len( L ) ) : i f L [ k ] == x :
del L [ k ]
# Ce c o d e ne f o n c t i o n n e p a s c a r on c h a n g e l e s i n d i c e s
# e t l ’ i t é r a t e u r , l u i , n ’ e s t p a s m o d i f i é .
# On o b t i e n t un " l i s t i n d e x o u t o f r a n g e "
# On p e u t u t i l i s e r une b o u c l e f o r à c o n d i t i o n de p a r c o u r r i r l a l i s t e
# à p a r t i r de l a f i n : def s u p p r i m e _ f o r ( L , x ) :
f o r k in range(len( L ) − 1 , −1, −1):
i f L [ k ] == x : del L [ k ]
# V e r s i o n a v e c r e t o u r de v a l e u r e t s a n s e f f e t de b o r d def s a n s _ v a l e u r ( L , x ) :
return [ a f o r a in L i f a != x ]