Answered step by step
Verified Expert Solution
Question
1 Approved Answer
After squandering precious minutes last year drawing and redrawing names out of a hat, this year you resolve to write a program to assign pairs
After squandering precious minutes last year drawing and redrawing names out of a hat, this year you resolve to write a program to assign pairs for the annual Christmas gift exchange. Your gift exchange program has five requirements: The pairings should be random Each gifter should be matched with one recipient Each recipient should be matched with one gifter No person may be paired with himself or herself Other unpermitted combinations need to be avoided Write a function assign_pairs that takes a list of names and a list of unpermitted pairings (as unordered pairs of names) and returns a dictionary of pairs according to the above requirements. The keys of the dictionary designate the gifters and the corresponding value designates the recipient. Here is an example of the function usage: # List of names names = ["Li", "Fatima", "Jaime", "Jamila", "Vincentius", "Nike", "Jean", "Sheila"] # Unpermitted pairs as a list of unordered pairs unpermitted_pairs = [{"Li", "Sheila"},{"Fatima", "Jaime"},{"Jamila", "Vincentius"}] pairs = assign_pairs(names, unpermitted_pairs) print(pairs) # One possible output {'li': 'Jean', 'Fatima': 'Jamila', 'Jaime': 'Sheila', 'Jamila': 'Nike', 'Vincentius': 'li', 'Nike': Your function should be defined as follows: def assign_pairs (names, unpermitted_pairs): # write some code here You may use the example above for debugging. The grading code tests that your function fulfills the above requirements and uses other lists than the example provided. Make sure to not hard-code your results. Note: Unlike an ordered pair (i.e. a tuple) which sees (a, b) and (b, a) as different, an unordered pair (i.e. a set) sees {a, b} and {b, a} as equivalent. Hint: The shuffle function shuffles a list in-place
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started