Invisible Friend Game

Invisible Friend Game

Python Problem with Solutions

 

The invisible friend game is a traditional gift exchange played in offices where everybody is randomly assigned to give a gift to other coworker. To make things exciting, the matching is random (you cannot pick your gift recipient) and nobody knows who got assigned until the day when the gifts are exchanged.
The problem arise when an employee is assigned to a manager or the other way round. It may cause trouble or embarrasing situations.

To fix this, you come up with an excellent idea, to automate the assignment process with a program. Hence, the goal of this Python problem with solutions is to write a program that randomly assigns to each worker the person they have to give a present, but prevents workers of different seniority to be assigned to each other.
The input of the program will be the workers and their role in the company. If he or she is an employee then the value is “e”. If he/she is a manager, then the value is ”m”:

Chris–>e,
Andrew–>e,
Dan–>m,
Steve–>e,
Alexis–>m,
Mateu->e,
Juan–>e,
Benoit–>e,
Michael–>e,
Adam–>e,
Maria–>e,
David–>e,
Angela–>e,
Philip–>e,
Iman–>e,
Luc–>e,
Simon–>e,
Lucas–>e,
Albert–>m,
Carmen–>m

The program will display all workers along with the person he/she has been assigned to give away the present.

This a Python problem with solutions to learn about the random module and practice data structures and problem solving skills. For more practice, visit other Practity’s projects.

Bonus

Add a condition to prevent being assigned to himself.
Include in the output, the gift to be given

Expected ouput

Iman is assigned to give a gift to Michael.
Juan is assigned to give a gift to Juan.
Simon is assigned to give a gift to Luc.
Chris is assigned to give a gift to Mateu.
Luc is assigned to give a gift to Andrew.
Adam is assigned to give a gift to Philip.
Steve is assigned to give a gift to Philip.
Michael is assigned to give a gift to Mateu.
Mateu is assigned to give a gift to Philip.
Philip is assigned to give a gift to Mateu.
David is assigned to give a gift to Simon.
Angela is assigned to give a gift to Maria.
Lucas is assigned to give a gift to Lucas.
Andrew is assigned to give a gift to Lucas.
Benoit is assigned to give a gift to Mateu.
Maria is assigned to give a gift to Chris.
Dan is assigned to give a gift to Carmen.
Alexis is assigned to give a gift to Carmen.
Albert is assigned to give a gift to Dan.
Carmen is assigned to give a gift to Albert.

Solutions


import random

# Define the workers and their positions
workers = {'Chris': 'e', 'Andrew': 'e', 'Dan': 'm', 'Steve': 'e', 'Alexis': 'm', 'Mateu': 'e', 'Juan': 'e', 'Benoit': 'e', 'Michael': 'e', 'Adam': 'e', 'Maria': 'e', 'David': 'e', 'Angela': 'e', 'Philip': 'e', 'Iman': 'e', 'Luc': 'e', 'Simon': 'e', 'Lucas': 'e', 'Albert': 'm', 'Carmen': 'm'}

# Separate employees and managers
employees = [worker for worker, position in workers.items() if position == 'e']
managers = [worker for worker, position in workers.items() if position == 'm']

# Shuffle the lists
random.shuffle(employees)
random.shuffle(managers)

# Assign gifts while preventing employees and managers from being assigned to each other
assignments = {}
if len(employees) == len(managers):
    for employee, manager in zip(employees, managers):
        assignments[employee] = manager
else:
    print("Unequal number of employees and managers. Please adjust the input.")

# Display the assigned gifts
for worker, recipient in assignments.items():
    print(f"{worker} is assigned to give a gift to {recipient}.")

 

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for Practice
Register New Account
Shopping cart