Design HashSet
Build a set of integers from an array of buckets, without a built-in set or dict.
The problem
Design a hash set for integer keys without using Python's built-in set or dict to store the keys. Implement the MyHashSet class: MyHashSet() creates an empty set; add(key) inserts key (adding a key that is already there changes nothing); remove(key) deletes key, and does nothing if key is not in the set; contains(key) returns True if key is in the set and False otherwise. Keys are between 0 and 1,000,000, and there are at most 10,000 calls in total.
Starter code
class MyHashSet:
def __init__(self):
passPublic examples
obj = MyHashSet() obj.add(1) obj.add(2) obj.contains(1) obj.contains(3) obj.add(2) obj.contains(2) obj.remove(2) obj.contains(2)gives[None, None, None, True, False, None, True, None, False]obj = MyHashSet() obj.add(0) obj.add(1000000) obj.contains(0) obj.contains(1000000) obj.contains(999999)gives[None, None, None, True, True, False]obj = MyHashSet() obj.add(4) obj.remove(1003) obj.contains(4)gives[None, None, None, True]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
