-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset.py
69 lines (55 loc) · 1.71 KB
/
set.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from hashtable import HashTable
class HashSet (object):
# initializer
def __init__(self, elements=None):
self.container = HashTable()
self.size = self.container.size
if elements is not None:
for item in elements:
self.add(item)
def __str__(self):
"""Return a formatted string representation of this hash table."""
items = ["{!r}".format(key) for key, _ in self.container.items()]
return "{" + ", ".join(items) + "}"
# Part 1
def add(self, element):
if self.contains(element):
return # simply do nothing - Anisha
self.size += 1
self.container.set(element, None)
def remove(self, element):
if self.size == 0:
raise ValueError("Nothing to remove, set is empty")
self.size -= 1
self.container.delete(element)
def contains(self, element):
return self.container.contains(element)
# Part 2
def union(self, other_set):
union = HashSet()
for key, _ in self.container.items():
union.add(key)
for key, _ in other_set.container.items():
if not self.contains(key):
union.add(key)
return union
def intersection(self, other_set):
union = HashSet()
for key, _ in self.container.items():
if other_set.contains(key):
union.add(key)
return union
def difference(self, other_set):
union = HashSet()
for key, _ in self.container.items():
if not other_set.contains(key):
union.add(key)
return union
def is_subset(self, other_set):
for key, _ in self.container.items():
if not other_set.contains(key):
return False
return True
if __name__ == "__main__":
names = ["Winnie", "Kojin", "Brian", "Nabil", "Julia", "Alex", "Nick"]
s = HashSet(names)