-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashmap.py
More file actions
206 lines (161 loc) · 5.54 KB
/
Hashmap.py
File metadata and controls
206 lines (161 loc) · 5.54 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from LinkedList import linkedList,Node
class HashMap(object):
def __init__(self):
self._table = HashTable()
def __setitem__(self, key, value):
self._table[key] = value
#
def __getitem__(self,key):
#return self._table.__getitem__(key)
return self._table[key]
def __contains__(self,key):
#think proxy
return key in self._table
def delete(self, key):
return self._table.delete(key)
class HashSet(object):
def __init__(self, initial_values = []):
self.index = 0
self.container = HashTable()
for v in initial_values:
self.add(v)
def add(self,item):
self.container[item] = item
def __contains__(self,item):
# it passed, Yay !!!
return item in self.container
def __len__(self):
return len(self.container)
def __iter__(self):
for k, _ in self.container:
yield k
# return iterator
#
# def __next__(self):
# if self.index >= len(self.container):
# raise StopIteration
#
# obj = self.container[self.index]
# self.index += 1
# return obj
#
# for k, _ in self.container:
# yield k
def __hash__(self):
return hash(self.container)
def __eq__(self, other):
return isinstance(other,HashSet) and self.container == other.container
def isSubset(self,set1):
# true if each element in self is in set1
for element in self:
if element not in set1:
return False
return True
def __repr__(self):
#self.container=
return str(self.container)
def isSuperSet(self, set1):
#Should simply return TRUE OR False
for element in set1:
if element not in self:
return False
return True
def union(self, set1):
bucket = HashSet()
for idx in self:
bucket.add(idx)
#should copy all the values into bucket
for other in set1:
if other not in self:
bucket.add(other)
#should check if value is not in self,
#then add value to bucket
# s = [ x for x in self if x in set1 ]
return bucket
def intersect(self,set1):
if len(set1) > len(self):
set1,self = self,set1
li_dif = [i for i in self if i in self and i in set1]
return li_dif
def my_difference(self,other):
bucket = []
for item in self:
if item not in other:
bucket.append(item)
return bucket
def map(my_function, my_list):
result = []
for i in my_list:
result.append(my_function(i))
return result
class HashTable(object):
def __init__(self):
#pigeonhole principle, used when you have more than one item in a bucket
#it gets counted correctly !
self.size = 8
self._buckets = [[] for _ in range(0, self.size)]
def __len__(self):
return sum(len(b) for b in self._buckets)
def __iter__(self):
#just need to iterate over the key value
for bucket in self._buckets:
for pair in bucket:
yield pair
def __eq__(self, other):
return isinstance(other,HashTable) and self._buckets == other._buckets
def get_hash(self,key):
hash = 0
for character in str(key):
hash += ord(character)
calculatated_hash_ref_num_in_buckets = hash % self.size
return calculatated_hash_ref_num_in_buckets
def __setitem__(self, key, value):
key_index = self.get_hash(key)
new_pair = [key, value]
# At this point, there's already a bucket with items
# So we iterate through the _buckets
for pair in self._buckets[key_index]:
if pair[0] == key:
pair[1] = value
#only returns if we found a match, per 125 (match found!)
return
self._buckets[key_index].append(new_pair)
# if the key is not in the bucket we add it to the bucket
# Note that if there's a hash collision we don't add the item
def __repr__(self):
return str(self._buckets)
def __getitem__(self, key):
key_index = self.get_hash(key)
for pair in self._buckets[key_index]:
if pair[0] == key:
return pair[1]
raise KeyError(key)
#important lesson here regrading writing unittest, if you expect to raise a KeyError then it has to be present
#Also,None is a good choice to return but the unittest is expecting you to raise a KeyError given a test case
def delete(self,key):
key_index = self.get_hash(key)
for index in range (0,len(self._buckets[key_index])):
if self._buckets[key_index][index][0] == key:
self._buckets[key_index].pop(index)
return True
return None
def __contains__(self, key):
try:
self.__getitem__(key)
return True
except KeyError:
return False
def remove_empty_list(self,empty_list):
empty_list = [t for t in enum(empty_list) if t ]
return empty_list
def main():
h = HashMap_practice()
h.__setitem__(10,"Nope")
h.__setitem__(13,"Almost")
h.__setitem__(12,"Gone")
h.__getitem__(12)
print(h)
h.delete(13)
print("Print New Hashmap")
print(h)
print(h.__getitem__(10))