The goal of this assignment is to get some practice using the set data structure.

Setup and Handin

Setup

If you are using PyCharm, set up a project as in HW1.

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 (very) simple socil networking website. The website’s data are organized as a hashtable where the keys are usernames (strings) and the values are sets of interests (also strings). For example, the data might look like this:

{
    "doug": {"music", "baseball", "books"},
    "brantley": {"coding", "super smash bros", "books"},
    "jessica": {"music", "photography", "painting"}
}

You’ve been asked to implement and test several functions that either query or modify data in this format. Here’s some code to start with; you should copy this code into a file called setwork.py:

def add_user(users: dict, name: str):
    """adds a user to the data with no interests"""
    pass

def add_interest(users: dict, name: str, interest: str):
    """adds an interest for the named user"""
    pass

def copy_interests(users: dict, name_to: str, name_from: str):
    """adds all of name_from's interests to name_to"""
    pass

def interest_exists(users: dict, interest: str) -> bool:
    """returns True if any user has this interest, False otherwise"""
    pass

def interests_match(users: dict, name: str, n: int) -> set:
    """returns a set of all users who share at least n 
       interests with the named user"""
    pass

Each function you’re implementing takes a users hashtable (formatted as above) as its first argument.

You should test all of your functions in a file called setwork_test.py. 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 hashtable

add_user

The add_user function should add a user with the given name to the hashtable with no interests. If there is already a user with that name, it should not modify the hashtable.

Note that in Python, set() is the empty set (whereas {} is the empty hashtable).

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 hashtable

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.

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!

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?