From e84a57dad0baf020206a332a9fef0dcae4fcff2e Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 26 Nov 2023 16:19:58 +0100 Subject: [PATCH] =?UTF-8?q?On=20fait=20mieux=20cette=20ann=C3=A9e=20et=20o?= =?UTF-8?q?n=20=C3=A9vite=20de=20tirer=20la=20m=C3=AAme=20chose=20que=20l'?= =?UTF-8?q?an=20pass=C3=A9=20:)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tirkdo.py | 79 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/tirkdo.py b/tirkdo.py index 91633e7..d1a3d4b 100644 --- a/tirkdo.py +++ b/tirkdo.py @@ -14,8 +14,6 @@ participants = [ 'Ludo', ] -participants_recois = participants.copy() - on_evite = { 'Ben': 'Ludo', 'Charlotte': 'Nico', @@ -23,29 +21,62 @@ on_evite = { 'Didi': 'Johnny', } -# On mélange les listes -random.shuffle(participants) -random.shuffle(participants_recois) +annee_passee = { + 'Nico': 'Ben', + 'Didi': 'Nico', + 'Mémé': 'Papa', + 'Maman': 'Mémé', + 'Charlotte': 'Ludo', + 'Johnny': 'Charlotte', + 'Papa': 'Didi', + 'Ludo': 'Maman', + 'Ben': 'Johnny', +} + +class EchecTirage(Exception): + pass + +# On défine notre méthode de tirage +def tirage(nb_tentatives_max=99): + participants_offre = participants.copy() + participants_recois = participants.copy() + + # On mélange les listes + random.shuffle(participants_offre) + random.shuffle(participants_recois) + + result = dict() + for offre in participants_offre: + count = 0 + while True: + count += 1 + if count == nb_tentatives_max: + raise EchecTirage + recois = random.choice(participants_recois) + # Pas à soi même + if recois == offre: + continue + # On évite entre couple + if on_evite.get(offre) == recois or on_evite.get(recois) == offre: + continue + # On évite de faire comme l'an passée + if annee_passee.get(offre) == recois: + continue + # Trouvé ! + result[offre] = recois + participants_recois.remove(recois) + break + return result # On procède au tirage -tirage = dict() -for offre in participants: - while True: - recois = random.choice(participants_recois) - - # Pas à soi même - if recois == offre: - continue - # On évite entre couple - if on_evite.get(offre) == recois or on_evite.get(recois) == offre: - continue - - # Trouvé ! - tirage[offre] = recois - participants_recois.remove(recois) - break - +result = None +while not result: + try: + result = tirage() + except EchecTirage: + print("Échec du tirage, on recommence !") + result = None print("Résultat du tirage :") -for offre, recois in tirage.items(): - print(" - %s -> %s" % (offre, recois)) +for offre, recois in result.items(): + print(f" - {offre} -> {recois}")