-
-
Save PatWg/a6e437ba555aae053d85c5b57588b67e to your computer and use it in GitHub Desktop.
ICC 23-24 Série 5 – Correction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
from random import randint | |
''' | |
Exercice 1 : Fonctions, retours multiples | |
''' | |
def min_max(l: List[int]) -> int: | |
# Il est important d'initialiser ces variables avec une valeur qui soit prise | |
# dans la liste, afin de pouvoir effectivement comparer des valeurs de la liste. | |
low: int = l[0] | |
high: int = l[0] | |
for element in l: | |
if element < low: | |
low = element | |
if element > high: | |
high = element | |
return low, high | |
def create_list() -> List[int]: | |
# Création de la liste, élément après élément | |
l: List[int] = [] | |
for i in range(10): | |
l.append(randint(0, 20)) | |
return l | |
l = create_list() | |
print(l) | |
low, high = min_max(l) | |
print(low, high) | |
''' | |
Exercice 2: Divisibilité par 3 | |
''' | |
def sum_of_digits(n: int) -> int: | |
# Ici, on utilise la conversion en str puis un parcours caractère après caractère | |
# Il ne faut pas oublier de reconvertir en entier lorsqu'il s'agit de calculer la somme | |
n_str: str = str(n) | |
s: int = 0 | |
for digit in n_str: | |
s += int(digit) | |
return s | |
def divisible_by_3(n: int): | |
# Tant que n est un nombre à plusieurs chiffres, on recalcule la somme des chiffres | |
while n >= 10: | |
n = sum_of_digits(n) | |
return n in (3, 6, 9) | |
for i in range(50): | |
if divisible_by_3(i): | |
print(f"{i} est divisible par 3") | |
else: | |
print(f"{i} n'est pas divisible par 3") | |
''' | |
Exercice 3: Liste de listes | |
Partie papier-crayon | |
a) La variable i parcourra l'intervalle [0;1]. La variable j parcourra l'intervalle [0;2] | |
Il y aura donc deux lignes et trois colonnes dans la matrice. | |
b) Exemple de remplissage du tableau | |
| | matrix | row | | |
|----------------+----------------------------+-----------| | |
| i = 0, j = 0 | [] | [0] | | |
| i = 0, j = 1 | [] | [0, 1] | | |
| i = 0, j = 2 | [] | [0, 1, 2] | | |
| i = 1, j = 0 | [[0, 1, 2]] | [1] | | |
| i = 1, j = 1 | [[0, 1, 2]] | [1, 2] | | |
| i = 1, j = 2 | [[0, 1, 2]] | [1, 2, 3] | | |
| end of program | [[0, 1, 2], [1, 2, 3]] | [] | | |
''' | |
def pretty_print(m: List[List[int]]) -> None: | |
# On affiche en dur le crochet ouvrant | |
print("[") | |
# Ici, on utilise une boucle for qui récupère les éléments (et pas les indices) | |
# Puisque m est une liste de listes, chaque élément de m est également une liste. | |
for row in m: | |
# On crée une variable qui contient les éléments d'une ligne | |
output: str = " " | |
# C'est pourquoi on peut ici encore écrire une boucle for qui récupère les éléments d'une ligne | |
for column in row: | |
output += str(column) + " " | |
# On affiche uniquement lorsque tous les éléments d'une ligne ont été parcourus | |
print(output) | |
# On affiche en dur le crochet fermant | |
print("]") | |
matrix: List[List[int]] = [] | |
rows: int = 3 | |
columns: int = 4 | |
for i in range(rows): | |
row: List[int] = [] | |
for j in range(columns): | |
row.append(i+j) | |
matrix.append(row) | |
print(matrix) | |
pretty_print(matrix) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment