tirkdo/tirkdo.py

83 lines
1.8 KiB
Python

#!/usr/bin/python
import random
participants = [
'Maman',
'Papa',
'Mémé',
'Charlotte',
'Nico',
'Didi',
'Johnny',
'Ben',
'Ludo',
]
on_evite = {
'Ben': 'Ludo',
'Charlotte': 'Nico',
'Maman': 'Papa',
'Didi': 'Johnny',
}
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
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 result.items():
print(f" - {offre} -> {recois}")