HW 2: The Social Setwork
Due: September 21, 2021 at 9:00PM EDT.
The goal of this assignment is to get some practice using the set
data structure.
Setup and Handin
Setup
Download hw2 here: https://cs.brown.edu/courses/csci0112/fall-2021/homeworks/hw2.zip. This file, hw2.zip
, unzips into a directory called hw2
which contains two stencils - setwork.py
and setwork_test.py
. Move hw2.zip
into the location where you would like your hw2
file, then unzip it. You can use Finder on Mac, File Explorer on Windows, or File System on Linux to do this.
To open this folder in VSCode, start by opening VSCode and then Opening the Explorer. The Explorer can be opened with the top-most button on the left sidebar which looks like this:
Once you've opened the explorer, your VSCode window should look something like this:
Click on the button which says "Open a Folder" and find the hw2 folder. Open that folder, and your VSCode window will look something like this:
Double-click the file names to open them in new tabs. You can also click the Explorer icon again to close that sidebar.
Since you opened the hw2 folder with VSCode, the VSCode terminal will automatically be in the hw2 directory. This means that you can use the terminal to run pytest without having to navigate to this directory.
If you try to run your test suite and find that pytest isn't working, try installing it following the instructions here: https://docs.pytest.org/en/6.2.x/getting-started.html. (When this website refers to a "command line," that means a terminal - the VSCode terminal works well for this). Note that sometimes even if you follow all the directions correctly sometimes it doesn't work. If pytest gives you trouble, please let us know - we can help out.
Handin
You may submit as many times as you want. Only your latest submission will be graded. This means that if you submit after the deadline, you will be using a late day – so do NOT submit after the deadline unless you plan on using late days.
README.txt
setwork.py
setwork_test.py
Because all we need are the files, you do not need to submit the whole project folder. As long as the file you create has the same name as what needs to be submitted, you’re good to go!
If you are using late days, make sure to make a note of that in your README. Remember, you may only use a maximum of 3 late days per assignment. If the assignment is late (and you do NOT have anymore late days) no credit will be given.
Please don't put your name anywhere in any of the handin files--we grade assigments anonymously!
You can follow this step-by-step guide for submitting assignments through gradescope here.
Helpful Things
Documentation
Staff Assistance
Your friendly TAs and Professor are here to help with this assignment! You can find our schedule for office hours here.
The Assignment
You're working on managing the data for a social networking website. The website's data are organized as a dictionary where the keys are usernames (strings) and the values are sets of interests (also strings). For example, the data might look like this:
{
"tim": {"reading", "cooking", "logic"},
"ashley": {"books", "piano", "animation"},
"ben": {"chocolate", "pretzels", "chocolate-covered pretzels"}
}
You've been asked to implement and test several functions that either query or modify data in this format.
Each function you're implementing takes a users
dictionary (formatted as above)
as its first argument.
Implement the functions in setwork.py
. You should test all of your functions in setwork_test.py
. These stencils are both in the hw2
directory which can be downloaded in the zip file from the Setup section.
Make sure to test edge cases! Writing a good set of tests for each function before you start implementing it is always a good idea--that way you can make sure you understand what your function is supposed to do.
Functions that modify the dictionary
add_user
The add_user
function should add a user with the given name to the dictionary
with no interests. If there is already a user with that name, it should not
modify the dictionary.
Note that in Python, set()
is the empty set (whereas {}
is the empty
dictionary).
add_interest
The add_interest
function should record that the user named name
is
interested in interest
. If the user doesn't exist, the function should add a
user with that name.
copy_interests
The copy_interests
function should add all of user name_from
's interests
as interests of name_to
. If a user named name_to
doesn't exist, it should be
created. If a user named name_from
doesn't exist, pretend it's a user with no
interests (i.e., don't modify name_to
's interests).
Functions that query the dictionary
interest_exists
The interest_exists
function should return True
if any user is interested
in interest
, and False
otherwise.
interests_match
The interests_match
function should return a set
of users who share at
least n
interests with the user named name
. If user name
is not present,
it should return the empty set.
Note that every user will share all of their interests with themselves.
General tips
The set
documentation
will be useful--some of the problems can be solved in fewer lines of code with a
judicious use of the operations described!
Note that sets do not contain duplicates, i.e {"A", "B"}
and {"A", "B", "A", "A"}
are the same set.
Running your Code and Tests
If you opened the hw2 directory through VSCode using the instructions in the Setup section of this handout, then your VSCode terminal will already be in the correct directory.
To run setwork.py
, you can use VSCode's run button or type python3 setwork.py
in the terminal. To use pytest to run your tests, run pytest setwork_test.py
in the terminal.
To open the Terminal in VSCode, click the button which something like this:
This will open a sidebar on the bottom of your screen which looks like this:
Click on the Terminal
tab to open your terminal.
README
In your README.txt
, include answers to the following questions:
- Did you discuss this assignment with any other students? Please list their cs logins.
- How many late days are you using on this assignment?