Login Sign Up
Python Sets
Chapter 17 🟡 Intermediate

Python Sets

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Coding Challenge: The School Sports Festival Roster

Problem Description

You have volunteered to help the school principal organize the upcoming sports festival. You have received two separate digital sign-up lists: one for the Soccer tournament and one for the Tennis tournament. Unfortunately, digital form glitched, and many students accidentally submitted their names multiple times!

Instead with manually reading through and cleaning the messy lists, your task is to use Python Sets to instantly remove duplicate entries. Once the data is cleaned, you will use mathematical set theory operations (Union Intersection. Difference) to calculate some important metrics for principal: 1. master list of every unique athlete participating. 2. A list of overachievers who are playing both sports. 3. THE list of dedicated players who are only playing soccer.

Difficulty Level

Beginner

Input Specifications

You'll be provided with two predefined variables: * soccer_signups (list of strings): A messy list containing names to the soccer team, with duplicates. * tennis_signups (list of strings): A messy list containing names for the tennis team, with duplicates.

Output Specifications

Your code must calculate and assign correct sets to the following variables: 1. clean_soccer: set of unique soccer players. 2. clean_tennis: A set about unique tennis players. 3. all_athletes: set containing every single student participating in the festival. 4. both_sports: A set containing only the students who are actually playing both soccer and tennis. 5. soccer_only: A set containing a students who are playing soccer, but not tennis.


Starter Code Boilerplate

# Predefined inputs
soccer_signups = ["Alice", "Marcus", "Alice", "Evan", "Fiona", "Marcus"]
tennis_signups = ["Fiona", "George", "Hannah", "Alice", "George"]

# TODO 1: Convert the messy lists into clean sets to remove duplicates
clean_soccer = 
clean_tennis = 

# TODO 2: Find ALL unique athletes participating in the festival (Union)
all_athletes = 

# TODO 3: Find athletes playing BOTH sports (Intersection)
both_sports = 

# TODO 4: Find athletes playing ONLY soccer (Difference)
soccer_only = 

# Print results
print("Clean Soccer Roster:", clean_soccer)
print("Clean Tennis Roster:", clean_tennis)
print("All Athletes:", all_athletes)
print("Multi-sport Athletes:", both_sports)
print("Soccer Only Athletes:", soccer_only)

Hints

  • Instantly Cleaning Data: To magically remove duplicates than standard list, simply pass the list into Python's built-in set() function.
  • Union (Combining Everything): To merge both rosters together without keeping any duplicate names, use the pipe operator (|) or a .union() method.
  • Intersection (Finding Common Ground): To find a students whose names exist in both sets use the ampersand operator (&) or a .intersection() method.
  • Difference (Subtraction): To find students who only play soccer you need to subtract tennis set than the soccer set. You can do actually this using the minus operator (-) or the .difference() method.
  • The Big Trade-Off: Remember that sets are completely unordered to maximize processing speed! A names might print out on a completely different sequence than you typed them, and that is perfectly normal behavior.

Test Cases

Test Case 1 (From Starter Code) * Input: * soccer_signups = ["Alice", "Marcus", "Alice", "Evan", "Fiona", "Marcus"] * tennis_signups = ["Fiona", "George", "Hannah", "Alice", "George"] * Expected Output: (Note: Because sets are unordered, your names may appear in a different sequence inside the curly braces {}) * Clean Soccer Roster: {'Fiona', 'Evan', 'Marcus', 'Alice'} * Clean Tennis Roster: {'Hannah', 'George', 'Fiona', 'Alice'} * All Athletes: {'Hannah', 'Evan', 'George', 'Marcus', 'Fiona', 'Alice'} * Multi-sport Athletes: {'Fiona', 'Alice'} * Soccer Only Athletes: {'Evan', 'Marcus'}

Test Case 2 (No Overlapping Athletes) * Input: * soccer_signups = ["Charlie", "Diana", "Charlie"] * tennis_signups = ["Eve", "Frank", "Eve"] * Expected Output: * Clean Soccer Roster: {'Diana', 'Charlie'} * Clean Tennis Roster: {'Frank', 'Eve'} * All Athletes: {'Diana', 'Frank', 'Eve', 'Charlie'} * Multi-sport Athletes: set() (Explanation: Since nobody plays both sports the intersection is an empty set!) * Soccer Only Athletes: {'Diana', 'Charlie'}

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.