On fait mieux cette année et on évite de tirer la même chose que l'an passé :)

This commit is contained in:
Benjamin Renard 2023-11-26 16:19:58 +01:00
parent d56516931f
commit e84a57dad0

View file

@ -14,8 +14,6 @@ participants = [
'Ludo', 'Ludo',
] ]
participants_recois = participants.copy()
on_evite = { on_evite = {
'Ben': 'Ludo', 'Ben': 'Ludo',
'Charlotte': 'Nico', 'Charlotte': 'Nico',
@ -23,29 +21,62 @@ on_evite = {
'Didi': 'Johnny', 'Didi': 'Johnny',
} }
# On mélange les listes annee_passee = {
random.shuffle(participants) 'Nico': 'Ben',
random.shuffle(participants_recois) '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 # On procède au tirage
tirage = dict() result = None
for offre in participants: while not result:
while True: try:
recois = random.choice(participants_recois) result = tirage()
except EchecTirage:
# Pas à soi même print("Échec du tirage, on recommence !")
if recois == offre: result = None
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
print("Résultat du tirage :") print("Résultat du tirage :")
for offre, recois in tirage.items(): for offre, recois in result.items():
print(" - %s -> %s" % (offre, recois)) print(f" - {offre} -> {recois}")