Set Comparison & Applied Problems
Beyond basic unions and intersections, Python sets offer powerful comparison operators to test relationships like subsets, supersets, and disjoint sets. These operations solve real-world verification and filtering challenges in O(1) time complexity.
Learning Objectives
- 11.2.1.4 Compare sets
- 11.3.3.4 Solve applied problems from various subject areas using sets
Lesson Presentation
11.2A-lesson-11-set-comparison-problems.pdf · Slides for classroom use
Conceptual Anchor
The Security Clearance Checker
Imagine a high-security lab requiring clearances: {"biometrics", "badge", "escort"}. A visitor presents their clearances: {"badge", "escort"}. To check if they can enter, we test if required clearances are a subset of visitor clearances. If two groups have zero common members, they are disjoint.
Rules & Theory
Set Comparison Methods & Operators
| Relationship | Method | Operator | Meaning |
|---|---|---|---|
| Subset | A.issubset(B) |
A <= B |
True if every element of A is also in B |
| Proper Subset | None | A < B |
True if A is subset of B and A != B |
| Superset | A.issuperset(B) |
A >= B |
True if every element of B is also in A |
| Proper Superset | None | A > B |
True if A is superset of B and A != B |
| Disjoint | A.isdisjoint(B) |
None | True if A and B have no common elements (A & B == set()) |
| Equality | None | A == B |
True if both sets contain exactly the same elements |
Worked Examples
1 Role-Based Access Control (RBAC)
Verify if a user has all permissions required for an administrative action:
required_perms = {"read", "write", "delete"}
user_perms = {"read", "write", "comment", "share"}
admin_perms = {"read", "write", "delete", "export", "manage_users"}
def check_access(user_role, perms, required):
if required.issubset(perms):
print(f"✓ {user_role}: Access Granted")
else:
missing = required - perms
print(f"✗ {user_role}: Access Denied. Missing: {missing}")
check_access("Standard User", user_perms, required_perms)
# ✗ Standard User: Access Denied. Missing: {'delete'}
check_access("Admin User", admin_perms, required_perms)
# ✓ Admin User: Access Granted2 Disjoint Schedules (Exam Clash Detector)
Detect whether two students or teachers have scheduling conflicts:
slots_student_a = {"Mon-09:00", "Tue-11:00", "Wed-14:00"}
slots_student_b = {"Tue-09:00", "Wed-10:00", "Thu-14:00"}
slots_student_c = {"Mon-09:00", "Fri-12:00"}
# Check if schedules can run concurrently without collision
print("A & B clash?", not slots_student_a.isdisjoint(slots_student_b)) # False (No clash)
print("A & C clash?", not slots_student_a.isdisjoint(slots_student_c)) # True (Clash on Mon-09:00)3 Vocabulary Richness & Unique Word Analysis
Analyze unique vocabulary overlap between two essays:
text1 = "algorithm complexity efficiency runtime memory space"
text2 = "runtime latency throughput memory bandwidth"
words1 = set(text1.split())
words2 = set(text2.split())
jaccard_similarity = len(words1 & words2) / len(words1 | words2)
print(f"Common terms: {words1 & words2}")
print(f"Similarity index: {jaccard_similarity:.2f}")Pitfalls & Common Errors
Proper Subset (<) vs Subset (<=)
If A = {1, 2} and B = {1, 2}, then A <= B is True, but A < B is False because a proper subset must have strictly fewer elements.
isdisjoint() with Empty Set
set().isdisjoint(any_set) is always True! An empty set has no elements to share with anything.
Operator vs Method Argument Types
Methods like A.issubset([1, 2, 3]) accept any iterable (lists, tuples, strings). Operators like A <= [1, 2, 3] raise a TypeError because both sides must be sets.
Pro-Tips for Exams
Exam Checklist
- To verify if an array has duplicates:
has_duplicates = len(data) != len(set(data)) A.isdisjoint(B)is significantly faster thanlen(A & B) == 0because it stops at the first common element without computing the full intersection.- For checking if string contains all vowels:
set("aeiou").issubset(set(user_input.lower())).
Graded Tasks
Write the difference between A <= B and A < B in Python.
Explain why s.issubset([1, 2, 3]) is valid, but s <= [1, 2, 3] throws an error.
Write a program that takes a sentence from the user and checks if it is a pangram (contains all 26 letters of the English alphabet) using set comparison.
Compare the time complexity of checking membership x in my_list vs x in my_set when searching 1,000,000 items.
Build an automated "Course Prerequisite Validator": define course prerequisite sets, prompt the student for completed courses, and output which courses they are eligible to enroll in.
Self-Check Quiz
{1, 2}.issubset({1, 2}) evaluate to?{1, 2} < {1, 2} evaluate to?{10, 20}.isdisjoint({30, 40}) return?s contains every letter in 'abc' using sets?