Lab: Sets
In this lab, you will develop a program to perform basic set operations.
Instructions
You should complete this lab individually.
Background
Python set objects support the set operations we discussed in class. Read about the union, intersection, and difference operations in the documentation.
Step 1: Review Starter Code
Download the starter code, and review the argparse configuration.
Use the --help
option to see program’s command-line interface:
> python sets.py --help
usage: sets.py [-h] --operation {union,intersection,difference}
[--a [A ...]] [--b [B ...]]
Perform operations on sets A and B.
options:
-h, --help show this help message and exit
--operation {union,intersection,difference}
operation to perform on sets A and B
--a [A ...] members of set A
--b [B ...] members of set B
Step 2: Implementation
Modify the starter code to support the union, intersection, and difference operations. If implemented correctly, you should see output like:
> python sets.py --operation union --a 1 2 3 --b 2 3 4
1
2
3
4
> python sets.py --operation intersection --a 1 2 3 --b 2 3 4
3
2
> python sets.py --operation difference --a 1 2 3 --b 2 3 4
1
Note: You should print each member of the resulting set on a separate line.
Optional: Support More Operations
Set objects support other operations, including isdisjoint()
, issubset()
, issuperset()
, and symmetric_difference()
.
Consider modifying your program to support these additional operations. Rather than add more if-statements to your code, try using getattr()
to call the appropriate methods.
Submit
Upload sets.py
to Gradescope.
Learning Goals
- Perform set operations in Python
- Use advanced argparse configurations