Première NSI
Exercices sur les Listes - Correction

Exercice 1

In [1]:
"""
Dans cette version, 
On utilise l'index des valeurs d'une liste
"""
def plus_un(lst):
    for i in range(len(lst)):
        lst[i] +=  1
    return lst
  
    
# Jeu d'essai
print("Pass test1 --> ",plus_un([6, 12, 18, 5]) == [7, 13, 19, 6])
print("Pass test1 --> ",plus_un([78]) == [79])
print("Pass test2 --> ",plus_un([]) == [])
In [2]:
"""
Dans cette version, 
On utilise une version propre à Python
"""
def plus_un(lst): 
    nv_liste = []
    for element in lst :
        nv_liste.append(element + 1)
    return nv_liste

# Jeu d'essai
print("Pass test1 --> ",plus_un([6, 12, 18, 5]) == [7, 13, 19, 6])
print("Pass test1 --> ",plus_un([78]) == [79])
print("Pass test2 --> ",plus_un([]) == [])    
Pass test1 -->  True
Pass test1 -->  True
Pass test2 -->  True

Définition par compréhension

Définition de la liste demandée par compréhension

In [3]:
def plus_un(lst):
    return [p + 1 for p in lst]

# Jeu d'essai
print("Pass test1 --> ",plus_un([6, 12, 18, 5]) == [7, 13, 19, 6])
print("Pass test1 --> ",plus_un([78]) == [79])
print("Pass test2 --> ",plus_un([]) == [])
Pass test1 -->  True
Pass test1 -->  True
Pass test2 -->  True

Exercice 2

In [4]:
"""
Dans cette version, 
On utilise l'index des valeurs d'une liste
"""

def somme_lst(lst):
    somme =0
    for i in range (len(lst)):
        somme += lst[i] 
    return somme 
   
    
# Jeu d'essai
print("Pass test1 --> ",somme_lst([6, 12, 18, 5]) == 41)
print("Pass test1 --> ",somme_lst([45]) == 45)
print("Pass test2 --> ",somme_lst([]) == 0)  
Pass test1 -->  True
Pass test1 -->  True
Pass test2 -->  True
In [5]:
"""
Dans cette version, 
On utilise une version propre à Python
"""
def somme_lst_1(lst):
    somme = 0
    for element in lst:
        somme += element 
    return somme 
   
    
# Jeu d'essai
print("Pass test1 --> ",somme_lst_1([6, 12, 18, 5]) == 41)
print("Pass test1 --> ",somme_lst_1([45]) == 45)
print("Pass test2 --> ",somme_lst_1([]) == 0)  
Pass test1 -->  True
Pass test1 -->  True
Pass test2 -->  True

Exercice 3

In [6]:
"""
Dans cette version, 
On utilise l'index des valeurs d'une liste
"""

def nb3(lst):
    cpt = 0
    for i in range(len(lst)):
        if lst[i] % 3 == 0:
            cpt = cpt + 1 
    return cpt    
    
# Jeu d'essai
print("Pass test1 --> ",nb3([]) == 0)
print("Pass test2 --> ",nb3([1]) == 0)
print("Pass test3 --> ",nb3([6]) == 1)
print("Pass test4 --> ",nb3([1,5,7]) == 0)
print("Pass test5 --> ",nb3([1,3,5,7,9,12,17,21]) == 4)
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
Pass test5 -->  True
In [7]:
"""
Dans cette version, 
On utilise une version propre à Python
"""

def nb3(lst):
    cpt = 0
    for element in lst:
        if element % 3 == 0:
            cpt = cpt + 1 
    return cpt
      
    
# Jeu d'essai
print("Pass test1 --> ",nb3([]) == 0)
print("Pass test2 --> ",nb3([1]) == 0)
print("Pass test3 --> ",nb3([6]) == 1)
print("Pass test4 --> ",nb3([1,5,7]) == 0)
print("Pass test5 --> ",nb3([1,3,5,7,9,12,17,21]) == 4)
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
Pass test5 -->  True

Exercice 4

In [8]:
"""
Dans cette version, 
On utilise l'index des valeurs d'une liste
"""
def minmax_lst(lst):
    if lst == []:
        resultat = None
    else:
        min_lst = lst[0]
        max_lst = lst[0]
        for i in range(len(lst)):
            if lst[i] > max_lst:
                max_lst = lst[i]
            if lst[i] < min_lst:
                min_lst = lst[i]
        return min_lst, max_lst



# Jeu d'essai
print("Pass test1 --> ",minmax_lst([]) == None)
print("Pass test2 --> ",minmax_lst([8,8]) == (8,8))
print("Pass test3 --> ",minmax_lst([1,3,5,7,9,12,17,21]) == (1,21))
print("Pass test4 --> ",minmax_lst([12,56,3,25,102,56,876,90,7]) == (3,876))
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
In [9]:
"""
Dans cette version, 
On utilise une version propre à Python
"""
def minmax_lst(lst):
    if lst == []:
        resultat = None
    else:
        min_lst = lst[0]
        max_lst = lst[0]
        for element in lst:
            if element > max_lst:
                max_lst = element
            if element < min_lst:
                min_lst = element
        return min_lst, max_lst



# Jeu d'essai
print("Pass test1 --> ",minmax_lst([]) == None)
print("Pass test2 --> ",minmax_lst([8,8]) == (8,8))
print("Pass test3 --> ",minmax_lst([1,3,5,7,9,12,17,21]) == (1,21))
print("Pass test4 --> ",minmax_lst([12,56,3,25,102,56,876,90,7]) == (3,876))
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
In [10]:
"""
Dans cette version, 
On utilise une version propre à Python, avec des méthodes
"""
def minmax_lst(lst):
    if lst == []:
        resultat = None
    else:
        lst = sorted(lst)
        resultat = lst[0],lst[-1]
    return resultat



# Jeu d'essai
print("Pass test1 --> ",minmax_lst([]) == None)
print("Pass test2 --> ",minmax_lst([8,8]) == (8,8))
print("Pass test3 --> ",minmax_lst([1,3,5,7,9,12,17,21]) == (1,21))
print("Pass test4 --> ",minmax_lst([12,56,3,25,102,56,876,90,7]) == (3,876))
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True

Exercice 5

In [11]:
"""
Dans cette version, 
On utilise l'index des valeurs d'une liste
"""

def prod5070(lst):
    produit = 0
    val = 1
    for i in range(len(lst)):
        if 50 <= lst[i] and lst[i] <= 70:
            val  = val * lst[i]
            produit = val
    return produit  
      
    
# Jeu d'essai
print("Pass test1 --> ",prod5070([]) == 0)
print("Pass test2 --> ",prod5070([8]) == 0)
print("Pass test3 --> ",prod5070([58]) == 58)
print("Pass test4 --> ",prod5070([12,45,78,36,59,65,14,3,78,62]) == 237770) 
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
In [12]:
"""
Dans cette version, 
On utilise une version propre à Python
"""

def prod5070(lst):
    produit = 0
    val = 1
    for element in  lst:
        if 50 <= element and element <= 70:
            val = val * element
            produit = val
    return produit
    
# Jeu d'essai
print("Pass test1 --> ",prod5070([]) == 0)
print("Pass test2 --> ",prod5070([8]) == 0)
print("Pass test3 --> ",prod5070([58]) == 58)
print("Pass test4 --> ",prod5070([12,45,78,36,59,65,14,3,78,62]) == 237770) 
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True

Exercice 6

In [13]:
"""
Dans cette version, 
On utilise l'index des valeurs d'une liste
"""

# Une solution possible
def recherche(lst,val):
    for i in range(len(lst)):
        if val == lst[i]:
            return i
    else:
        return None
        
# Jeu d'essai
print("Pass test1 --> ",recherche([12,45,78,36,59,65,14,3,78,62],80) == None)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],12) == 0)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],62) == 9)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],78) == 2)
Pass test1 -->  True
Pass test2 -->  True
Pass test2 -->  True
Pass test2 -->  True
In [14]:
"""
Dans ces versions, 
On utilise une version propre à Python
"""

# Une solution possible

def recherche(lst,val):
    if val in lst :
        return lst.index(val)
    else:
        return None
        
# Jeu d'essai
print("Pass test1 --> ",recherche([12,45,78,36,59,65,14,3,78,62],80) == None)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],12) == 0)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],62) == 9)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],78) == 2)



# Une autre solution : un sel retour dans la fonction
def recherche(lst,val):
    if val in lst :
        index = lst.index(val)
    else:
        index = None
    return index
        
# Jeu d'essai
print("Pass test1 --> ",recherche([12,45,78,36,59,65,14,3,78,62],80) == None)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],12) == 0)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],62) == 9)
print("Pass test2 --> ",recherche([12,45,78,36,59,65,14,3,78,62],78) == 2)
Pass test1 -->  True
Pass test2 -->  True
Pass test2 -->  True
Pass test2 -->  True
Pass test1 -->  True
Pass test2 -->  True
Pass test2 -->  True
Pass test2 -->  True

Exercice 7

In [15]:
"""
Dans ces versions, 
On utilise l'index des valeurs d'une liste
"""
# Une première version
def reverso(lst):
    nvl_liste = []
    for i in range(len(lst)):
        nvl_liste = [lst[i]] + nvl_liste
    return nvl_liste
        
# Jeu d'essai
print("Pass test1 --> ",reverso([])==[])
print("Pass test2 --> ",reverso([1,2,3,4])==[4,3,2,1])
print("Pass test3 --> ",reverso(["chat","chien","cheval"])==["cheval","chien","chat"])
print("Pass test4 --> ",reverso(["aa"])==["aa"])   

# Une autre version en utlisant une boucle décroissante
def reverso(lst):
    nvl_liste = []
    for i in range(len(lst)-1, -1, -1):
        nvl_liste.append(lst[i])
    return nvl_liste
        
# Jeu d'essai
print("Pass test1 --> ",reverso([])==[])
print("Pass test2 --> ",reverso([1,2,3,4])==[4,3,2,1])
print("Pass test3 --> ",reverso(["chat","chien","cheval"])==["cheval","chien","chat"])
print("Pass test4 --> ",reverso(["aa"])==["aa"])   
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
In [16]:
"""
Dans ces versions, 
On utilise une version propre à Python
"""

# Une première version
def reverso(lst):
    nvl_liste = []
    for element in lst:
        nvl_liste = [element] + nvl_liste
    return nvl_liste

# Jeu d'essai
print("Pass test1 --> ",reverso([]) == [])
print("Pass test2 --> ",reverso([1,2,3,4]) == [4,3,2,1])
print("Pass test3 --> ",reverso(["chat","chien","cheval"]) == ["cheval","chien","chat"])
print("Pass test4 --> ",reverso(["aa"]) == ["aa"])   


# Utilisation des slices (voir chaînes de caractères)
def reverso(lst):
        return lst[::-1]
    
# Jeu d'essai
print("Pass test1 --> ",reverso([]) == [])
print("Pass test2 --> ",reverso([1,2,3,4]) == [4,3,2,1])
print("Pass test3 --> ",reverso(["chat","chien","cheval"]) == ["cheval","chien","chat"])
print("Pass test4 --> ",reverso(["aa"]) == ["aa"])

# Utilisation de la méthode reverse()
def reverso(lst):
        lst.reverse()
        return lst
    
# Jeu d'essai
print("Pass test1 --> ",reverso([])==[])
print("Pass test2 --> ",reverso([1,2,3,4]) == [4,3,2,1])
print("Pass test3 --> ",reverso(["chat","chien","cheval"]) == ["cheval","chien","chat"])
print("Pass test4 --> ",reverso(["aa"]) == ["aa"])
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True

Exercice 8

In [17]:
"""
Dans cette version, 
On utilise l'index des valeurs d'une liste
"""

def enum(lst):
    couples = []
    for i in range(len(lst)):
        couples.append((i , lst[i]))
    return couples

    
# Jeu d'essai
print("enum / validation tests")
print("=======================")
print("Pass test1 --> ",enum([1,1,1])==[(0,1),(1,1),(2,1)])
print("Pass test2 --> ",enum(["chat","chien","cheval"])==[(0,"chat"),(1,"chien"),(2,"cheval")])
print("Pass test3 --> ",enum([1])==[(0,1)])
print("Pass test4 --> ",enum([])==[])
enum / validation tests
=======================
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
In [18]:
"""
Dans cette version, 
On utilise une version propre à Python - la methode enumerate()
"""
def enum(lst):
    return list(enumerate(lst))
    
# Jeu d'essai
print("enum / validation tests")
print("=======================")
print("Pass test1 --> ",enum([1,1,1])==[(0,1),(1,1),(2,1)])
print("Pass test2 --> ",enum(["chat","chien","cheval"])==[(0,"chat"),(1,"chien"),(2,"cheval")])
print("Pass test3 --> ",enum([1])==[(0,1)])
print("Pass test4 --> ",enum([])==[])
enum / validation tests
=======================
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True

Exercice 9

In [19]:
"""
Dans ces versions, 
On utilise l'index des valeurs d'une liste
"""
def  map_list(f,lst):
    nvl_liste = []
    for i in range(len(lst)):
        nvl_liste.append(f(lst[i]))
    return nvl_liste

# Jeu d'essai
print("map_list / validation tests")
print("===========================")
print("Pass test1 --> ",map_list(lambda x: 2*x, [1,2,3,4])== [2,4,6,8])
print("Pass test2 --> ",map_list(lambda x: None, [1,2,3,4])== [None,None,None,None])
print("Pass test3 --> ",map_list(lambda x: x+5, [1,2,3,4])== [6,7,8,9])
print("Pass test4 --> ",map_list(lambda x: x+5, [])== [])
map_list / validation tests
===========================
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
In [20]:
"""
Dans cette version, 
On utilise une version propre à Python
"""
def  map_list(f,lst):
    nvl_liste = []
    for elt in lst:
        nvl_liste.append(f(elt))
    return nvl_liste
           
# Jeu d'essai
print("map_list / validation tests")
print("===========================")
print("Pass test1 --> ",map_list(lambda x: 2*x, [1,2,3,4])== [2,4,6,8])
print("Pass test2 --> ",map_list(lambda x: None, [1,2,3,4])== [None,None,None,None])
print("Pass test3 --> ",map_list(lambda x: x+5, [1,2,3,4])== [6,7,8,9])
print("Pass test4 --> ",map_list(lambda x: x+5, [])== [])
map_list / validation tests
===========================
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
In [21]:
# Exemples avec des fonctions extérieures

def g(x):
    return x**2 + 3

def h(x):
    return x**(1/2)
In [22]:
map_list(g, [1, 2, 3, 4])
Out[22]:
[4, 7, 12, 19]
In [23]:
map_list(h, [1, 2, 3, 4])
Out[23]:
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0]

Définition par compréhension

Définition de la liste demandée par compréhension

In [24]:
def  map_list(f,lst):
    return [f(p) for p in lst]
           
# Jeu d'essai
print("map_list / validation tests")
print("===========================")
print("Pass test1 --> ",map_list(lambda x: 2*x, [1,2,3,4])== [2,4,6,8])
print("Pass test2 --> ",map_list(lambda x: None, [1,2,3,4])== [None,None,None,None])
print("Pass test3 --> ",map_list(lambda x: x+5, [1,2,3,4])== [6,7,8,9])
print("Pass test4 --> ",map_list(lambda x: x+5, [])== [])
map_list / validation tests
===========================
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True

Exercice 10

In [25]:
"""
Dans cette version, 
On utilise une version propre à Python
"""
def  fusion_list(lst1,lst2):
    for elt in lst2:
        if elt not in lst1:
            lst1.append(elt)
    lst1.sort()
    return lst1
             
# Jeu d'essai
print("fusion_list / validation tests")
print("==============================")
print("Pass test1 --> ",fusion_list([1,5,7],[2,6,9,14])== [1,2,5,6,7,9,14])
print("Pass test2 --> ",fusion_list([1,5,7,20],[2,5,9,14])== [1,2,5,7,9,14,20])
print("Pass test3 --> ",fusion_list([1,2,3,4],[])== [1,2,3,4])
print("Pass test4 --> ",fusion_list([],[1,2,3,4])== [1,2,3,4])
print("Pass test5 --> ",fusion_list([1,2,3,4],[1,2,3,4])== [1,2,3,4])
fusion_list / validation tests
==============================
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
Pass test5 -->  True

Définition par compréhension

Définition de la liste demandée par compréhension

In [26]:
def  fusion_list(lst1,lst2):
    return sorted(lst1 + [p for p in lst2  if p not in lst1])

# Jeu d'essai
print("fusion_list / validation tests")
print("==============================")
print("Pass test1 --> ",fusion_list([1,5,7],[2,6,9,14])== [1,2,5,6,7,9,14])
print("Pass test2 --> ",fusion_list([1,5,7,20],[2,5,9,14])== [1,2,5,7,9,14,20])
print("Pass test3 --> ",fusion_list([1,2,3,4],[])== [1,2,3,4])
print("Pass test4 --> ",fusion_list([],[1,2,3,4])== [1,2,3,4])
print("Pass test5 --> ",fusion_list([1,2,3,4],[1,2,3,4])== [1,2,3,4])
fusion_list / validation tests
==============================
Pass test1 -->  True
Pass test2 -->  True
Pass test3 -->  True
Pass test4 -->  True
Pass test5 -->  True
In [ ]:
 
In [ ]: