Assign equal number chores to each person (assertion made elsewhere to ensure chores % number of people == 0) with the stipulation that each chore must not be one which the person was assigned the previous time.
My simpleton solution (there has to be a cleverer way):
import random
chores = ["hoovering", "dusting", "wash dishes", "tidy garden",
"clean windows", "empty dishwasher", "dust", "cook"]
people_data = {"David": {"email": "[email protected]",
"chores" : []},
"Mark": {"email": "[email protected]",
"chores": []},
"Anaya": {"email": "[email protected]",
"chores": []},
"Cooper": {"email": "[email protected]",
"chores": []}
}
prev_assignment_data = None
def assign_chores(chore_list, prev_assignment_data, people_data):
"""Returns a dict with assign chores to each person
Arguments:
chore_list -- a list of the chores to be assigned
prev_assignment_data -- if there has been prev chore assignments this
will be a dict containing said assignments.
Otherwise, it will have no value ('None')
people_data -- a dict of the names and the email address
associated with those names. Assigned chores
will be appended to this dict and dict
returned.
"""
# if no previous data available, assign each a random chore
if not prev_assignment_data:
while chore_list:
for name in people_data.keys():
chore = random.choice(chore_list)
chore_list.remove(chore)
people_data[name]["chores"].append(chore)
# if prev data available, assign each chores not the same as previous
else:
new_chores = False
while not new_chores:
chore_list_copy = chore_list
for name in people_data.keys():
# get all chores still available and which are not those
# the person was assigned the previous time
avail_chores = [chore for chore in chore_list_copy if chore not
in prev_assignment_data[name]["chores"]]
if avail_chores:
chore = random.choice(avail_chores)
people_data[name]["chores"].append(chore)
chore_list_copy.remove(chore)
# if only chores remaining are those the person did previously,
# restart entire assignment process
else:
break
# all chores assigned and are different to those person did prev
if not chore_list_copy:
new_chores = True
return people_data
assigned_chores = assign_chores(chores, prev_assignment_data,
people_data)
print(assigned_chores)