""" 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é" def analyser_caractere(c,majuscule,minuscule,chiffre,special): if c.isupper(): majuscule = True elif c.islower(): minuscule = True elif c.isdigit(): chiffre = True else: special = True return majuscule,minuscule,chiffre,special def verif_petit(len, problemes): if len <12: problemes.append("trop court") def verif_no_min_maj(min,maj,problemes): if not (maj and min): problemes.append("mélanger majuscules et minuscules") def verif_chiffre(chiffre,problemes): if not chiffre: problemes.append("ajouter un chiffre") def verif_special(special,problemes): if not special: problemes.append("ajouter un caractère spécial") def verif_mdp_is_not_login(login,mdp,problemes): if login and login.lower() in mdp.lower(): problemes.append("ne pas contenir le login") def verif_carac_identique(mdp,problemes): for i in range(len(mdp) - 2): if mdp[i] == mdp[i + 1] == mdp[i + 2]: problemes.append("éviter 3 caractères identiques") return def verif_fort(problemes): if not problemes: return "fort", [] return ("moyen" if len(problemes) <= 2 else "faible"), problemes def analyser_mot_de_passe(mdp, login=None): problemes = [] majuscule = minuscule = chiffre = special = False for c in mdp: majuscule,minuscule,chiffre,special = analyser_caractere(c,majuscule,minuscule,chiffre,special) print(majuscule,minuscule,chiffre,special) verif_petit(len(mdp), problemes) verif_no_min_maj(minuscule,majuscule,problemes) verif_chiffre(chiffre,problemes) verif_special(special,problemes) verif_mdp_is_not_login(login,mdp,problemes) verif_carac_identique(mdp,problemes) return verif_fort(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"))