Files
2026_TD2_DEV51_Qualite_Algo/exercices.py
T

72 lines
1.9 KiB
Python

"""
R5.A.04 - Qualité Algorithmique
TD Complexité cyclomatique - Partie Python
Pour chaque fonction, calculez la complexité cyclomatique M.
"""
# Exercice P1
def celsius_vers_fahrenheit(celsius):
return celsius * 9 / 5 + 32
# Exercice P2
def signe(x):
if x > 0:
return 1
elif x < 0:
return -1
return 0
# Exercice P3
def notes_valides(notes):
return [n for n in notes if 0 <= n <= 20]
# Exercice P4
def mention(moyenne):
mentions = {16: "Très bien", 14: "Bien", 12: "Assez bien", 10: "Passable"}
for seuil, libelle in mentions.items():
if moyenne >= seuil:
return libelle
return "Ajourné"
# Exercice P5
def analyser_mot_de_passe(mdp, login=None):
problemes = []
if len(mdp) < 12:
problemes.append("trop court")
majuscule = minuscule = chiffre = special = False
for c in mdp:
if c.isupper():
majuscule = True
elif c.islower():
minuscule = True
elif c.isdigit():
chiffre = True
else:
special = True
if not (majuscule and minuscule):
problemes.append("mélanger majuscules et minuscules")
if not chiffre:
problemes.append("ajouter un chiffre")
if not special:
problemes.append("ajouter un caractère spécial")
if login and login.lower() in mdp.lower():
problemes.append("ne pas contenir le login")
for i in range(len(mdp) - 2):
if mdp[i] == mdp[i + 1] == mdp[i + 2]:
problemes.append("éviter 3 caractères identiques")
break
if not problemes:
return "fort", []
return ("moyen" if len(problemes) <= 2 else "faible"), problemes
if __name__ == "__main__":
print(celsius_vers_fahrenheit(20), signe(-3), notes_valides([12, 25, -1, 8]),
mention(13.5), analyser_mot_de_passe("Motdepasse123!", "maxime"))