Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//am good with the code to exchange gifts, I need help with 2-4, thanks P.S - in python A popular activity during holidays is for

//am good with the code to exchange gifts, I need help with 2-4, thanks

P.S - in python

A popular activity during holidays is for families or groups of friends or co-workers to setup up a gift exchange. For large groups especially, though, it can be a time-consuming task to determine who will buy a gift for whom in a fair system that is inclusive of everybody, so this is a ripe case for creating an automated helper program to make the gift assignments for us.

How can we take that idea and write a functional computer program around it?

First, well need to ask the user how many people will be participating in the gift exchange. Then, well need to ask for the names of all of the participants and add them to a list. Then, well have to figure out how to randomly assign one person to another in a way that ensures that no one is assigned to give a gift to themselves and that each person only gives one gift and only receives one gift (there cannot be any repeats).

Finally, well need to display the full list of final gift assignments to the user letting them know who is giving to whom in this gift exchange.

Use the following starter code In Python:

# Gift-assigner program 
import random 

numPeople = int(input("How many people are playing? ")) givers = [] receivers = []

print("Please enter their names: ") 

# Get all of the players' names and add them to the list

for i in range(numPeople): givers.append(input("")) 

# Randomly assign gift givers to gift receivers. Check to make sure that nobody is assigned themselves (which is no fun!), and that each person can only give one gift and can only receive one gift (no repeats). Keep trying (looping) until everyone is giving a gift to someone else.

for j in range(numPeople): receivers[j] = random.sample(givers, numPeople)

# Print results 
print() print("Gift Assignments...") for k in range(numPeople): 

print(givers[k], "will buy a gift for", receivers[k]) print()

  1. ASSIGNMENT: Add to the starter code above to make sure that 1) nobody is assigned to buy a gift for themselves, and 2) each person can only give one gift and can only receive one gift (no repeats). Have the program keep trying (looping) until both of these conditions are met. The program should work every time regardless of the number of people participating.

  2. CHALLENGE ASSIGNMENT: How can we avoid a closed loop where, for example, Player #1 buys a gift for Player #4, and vice versa effectively closing them off from everyone else?

  3. CHALLENGE ASSIGNMENT: Assuming that your solution was to completely restart from scratch and reassign everybody if even one person was assigned to give a gift to themselves - which is inefficient - can you design a better, more efficient algorithm? (HINT Its ok to create new lists!)

  4. CHALLENGE ASSIGNMENT: Sometimes, it's no fun for certain people in a big gift exchange to buy gifts for each other (like spouses who will probably buy gifts for each other anyway). Can you edit this program to account for such undesirable matchings? (HINT - Read up on different Matching algorithms!)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

Students also viewed these Databases questions