Challenge - 5 Problems
Subset and Superset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this subset check?
Consider the following Python code:
What is the output of
set_a = {1, 2, 3, 4}
set_b = {2, 3}What is the output of
set_b.issubset(set_a)?Python
set_a = {1, 2, 3, 4}
set_b = {2, 3}
print(set_b.issubset(set_a))Attempts:
2 left
๐ก Hint
Remember, a subset means all elements of one set are in the other.
โ Incorrect
set_b contains elements 2 and 3, both of which are in set_a. So set_b is a subset of set_a, and the method returns True.
โ Predict Output
intermediate2:00remaining
What does this superset check return?
Look at this code:
What is the output of
set_x = {5, 6, 7, 8}
set_y = {6, 7}What is the output of
set_x.issuperset(set_y)?Python
set_x = {5, 6, 7, 8}
set_y = {6, 7}
print(set_x.issuperset(set_y))Attempts:
2 left
๐ก Hint
Superset means the first set contains all elements of the second.
โ Incorrect
set_x contains 5,6,7,8 which includes all elements of set_y (6,7), so it returns True.
โ Predict Output
advanced2:00remaining
What is the output of this subset check with an empty set?
Given:
What does
set_m = set()
set_n = {1, 2, 3}
What does
set_m.issubset(set_n) print?Python
set_m = set() set_n = {1, 2, 3} print(set_m.issubset(set_n))
Attempts:
2 left
๐ก Hint
Think about whether an empty set is a subset of any set.
โ Incorrect
An empty set is considered a subset of every set, so the method returns True.
โ Predict Output
advanced2:00remaining
What does this superset check return?
Look at this code:
What happens when running
set_a = {1, 2, 3}
not_a_set = [1, 2]What happens when running
set_a.issuperset(not_a_set)?Python
set_a = {1, 2, 3}
not_a_set = [1, 2]
print(set_a.issuperset(not_a_set))Attempts:
2 left
๐ก Hint
issuperset accepts any iterable, not just sets.
โ Incorrect
issuperset accepts any iterable argument, so it checks if all elements of the list are in the set. Since 1 and 2 are in set_a, it returns True.
๐ง Conceptual
expert3:00remaining
Which option shows the correct way to check if set A is a strict subset of set B?
You want to check if set A is a subset of set B but not equal to B. Which code snippet does this correctly?
Attempts:
2 left
๐ก Hint
Python has operators for strict subset and subset or equal.
โ Incorrect
The < operator checks for strict subset (subset but not equal). The <= operator allows equality. Option D is logically correct but less concise. Option D checks superset, which is the opposite.