-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign_HashSet_705.py
More file actions
39 lines (32 loc) · 915 Bytes
/
Copy pathDesign_HashSet_705.py
File metadata and controls
39 lines (32 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class MyHashSet:
def __init__(self):
self.list = []
def add(self, key: int) -> None:
if key not in self.list:
self.list.append(key)
def remove(self, key: int) -> None:
if key in self.list:
self.list.remove(key)
def contains(self, key: int) -> bool:
return key in self.list
def __str__(self) -> str:
set = "{"
for i in self.list:
set += f"{i} ,"
set += "}"
return set
if __name__ == '__main__':
myHashSet = MyHashSet()
myHashSet.add(1)
myHashSet.add(2)
print(myHashSet)
print(myHashSet.contains(1))
print(myHashSet.contains(3))
myHashSet.add(2)
print(myHashSet)
print(myHashSet.contains(2))
myHashSet.remove(2)
print(myHashSet)
myHashSet.remove(6)
print(myHashSet)
print(myHashSet.contains(2))