Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: MySet In the file my_set.py, write a class named MySet that implements something like the set implemented with a hash table that we

Part 1: MySet

In the file my_set.py, write a class named MySet that implements something like the set implemented with a hash table that we have been describing in this reading.

The MySet class should only store int values in its hash table (which should be a list). You may assume the input values don't "collide" in the hash table. A collision is when two different values hash to the same index.

Make your hash table be size 10 and use the hash function h(v) = v % 10 to determine the index. You should not use a set to implement this problem.

To implement the proper syntax to support Part 2, you should implement the following methods.

  • __init__(self) that sets up any initial state you need for the other functions.

    • Hint: Recall you can make a list of a certain length by using the syntax [1] * n which will make a list of n 1s.

  • add(self, v) that adds a given value to this MySet

    • Hint: Make sure you think about the case if we add the same value twice!

    • Notice that this does not have underscores before/after the name. We are not trying to define the behavior for one of the pre-defined "special functions" (like __init__), instead, we are defining a new function called add.

  • __contains__(self, v) returns True or False depending on whether or not the given value is in this MySet.

    • This allows the client to use the syntax 5 in ms where ms = MySet().

  • __len__(self) returns the number of elements in this MySet.

    • This allows the client to use the syntax len(ms) where ms = MySet().

Requirements

  • You should implement these functions so that they all have complexity O(1). For this checkpoint, this means your program should not have any loops!

  • All fields should be private.

  • You should not use the set class in any way in your implementation. You should implement MySet using a list as your hash table.

Part 2: my_set_client.py

In the file my_set_client.py, write a client of MySet that does the following things:

  • Imports the MySet class.

  • Constructs a MySet instance and adds the numbers 14, 2, and 17 and 2.

  • Prints the result of querying the MySet if it contains the value 2.

  • Prints the result of querying the MySet if it contains the value 4.

  • Prints the len of the MySet.

Requirements

  • Your program should use the main-method pattern.

A Note About Naming Convention

Python uses snake_case for almost everything. All your functions, variables, fields, and even file names should use snake_case. The one major exception is class names which should be CapitalCase (where the first letter of each word is capitalized). This is why we will call the class MySet but the file name will be my_set.py. with python

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions