-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhashMap.py
235 lines (191 loc) · 4.41 KB
/
hashMap.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
class Node:
# for creating Node Instances
def __init__(self, key, item, *args, **kwargs):
# uses val attribute as key
self.val = key
self.item = item
self.next = None
# def __eq__(self, other):
# return self.val==other.val
# def __hash__(self):
# return sum(ord(i for i in str(self.val)))
class LinkedList:
def __init__(self, *args, **kwargs):
self.head = None
self.length = 0
def __len__(self):
return self.length
def insert(self, key, item):
"""
inserts the Node at tail if key is not present
there else update the item of that key
"""
if not self.head:
# if there is no head create one
self.head = Node(key, item)
# self.temp = self.head
else:
# if head is there then link node to last
temp=self.head
while temp:
if temp.val==key:
temp.item = item
return
if temp.next==None:
temp.next=Node(key, item)
else:
temp=temp.next
self.length+=1
def find(self, val, head=None, prev=None):
"""
returns the previous node if value exists else returns None
"""
if head==None:
head=self.head
while head:
if head.val == val:
return prev
prev, head = head, head.next
return 0
def delete(self, val):
"""
finds and deletes the node from linked list if it exists
"""
if self.head==None: return
if self.head.val == val:
self.head = self.head.next
self.length-=1
return
prevNode = self.find(val, head = self.head, prev=None)
if prevNode!=0:
self.length-=1
if prevNode.next.next:
prevNode.next = prevNode.next.next
else:
prevNode.next=None
def reverse(self):
"""
reversese the node in linked list and changes the
head with tail
"""
curr = self.head
prev = None
def helper(curr, prev, next):
if curr==None:
self.head = prev
return
next = curr.next
curr.next = prev
prev = curr
curr = next
return helper(curr, prev, next)
helper(curr, prev, None)
def __repr__(self):
temp=self.head
s=""
while temp:
s+=f"{temp.val} "
temp=temp.next
return s
def __iter__(self):
# used for "for" loops or creating iterables
temp=self.head
while temp:
yield temp
temp=temp.next
def __next__(self):
# for "next()" operator
temp=self.head
while temp:
yield temp
temp=temp.next
def __contains__(self, val):
# used for "in" operator
return self.find(val)!=0
def __getitem__(self, index):
# used for '[]' notation
if index>=self.length:
raise IndexError("index does not exist")
temp=self.head
while temp and index:
temp=temp.next
index-=1
return temp
def __eq__(self, other):
return self.__repr__()==other.__repr__()
def get(self, val):
temp=self.head
while temp:
if temp.val==val:
return temp
temp=temp.next
class HashMap:
# Hash Map only for integers, string and
# tuples with integers or strings
def __init__(self):
self.size=20
self.map=[None]*self.size
def discard(self, key):
"""
tries to delete node with key=="key"
"""
valHash = self.hash(key)
ll = self.map[valHash]
if ll==None: return
ll.delete(key)
def hash(self, val):
return sum(ord(i) for i in str(val)) % self.size
def __setitem__(self, key, item):
valHash = self.hash(key)
if self.map[valHash]==None:
self.map[valHash]=LinkedList()
self.map[valHash].insert(key, item)
def __contains__(self, key):
valHash = self.hash(key)
ll = self.map[valHash]
if ll==None: return False
node = ll.get(key)
if node: return True
return False
def __repr__(self):
s=[]
for ll in self.map:
if ll!=None:
for node in ll:
s.append(f"{node.val}:{node.item}")
return "{"+", ".join(s)+"}"
def __iter__(self):
# used for "for" loops or creating iterables
for ll in self.map:
if ll!=None:
for node in ll:
yield node.val, node.item
# def __next__(self):
# # for "next()" operator
# temp=self.head
# while temp:
# yield temp
# temp=temp.next
def __getitem__(self, key):
# used for "in" operator
valHash = self.hash(key)
ll = self.map[valHash]
if ll==None:
raise KeyError("key does not exists")
node = ll.get(key)
if node:
return node.item
else:
raise KeyError("key does not exists")
if __name__ == "__main__":
h=HashMap()
for i in range(10):
h[i]=f"{i}"
# for i in h:
# print(i)
print(h)
h[0]=100
print(h)
for i in range(1):
h.discard(i)
print(h)