tirkdo/tirkdo.py

83 lines
1.8 KiB
Python
Raw Normal View History

2021-11-25 19:49:17 +01:00
#!/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',
}
2021-11-25 19:49:17 +01:00
class EchecTirage(Exception):
pass
2021-11-25 19:49:17 +01:00
# On défine notre méthode de tirage
def tirage(nb_tentatives_max=99):
participants_offre = participants.copy()
participants_recois = participants.copy()
2021-11-25 19:49:17 +01:00
# On mélange les listes
random.shuffle(participants_offre)
random.shuffle(participants_recois)
2021-11-25 19:49:17 +01:00
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
2021-11-25 19:49:17 +01:00
print("Résultat du tirage :")
for offre, recois in result.items():
print(f" - {offre} -> {recois}")