-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.py
50 lines (39 loc) · 1.15 KB
/
hash.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
import math
class HashMap(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.grid = {}
def key(self, point):
width = self.width
height = self.height
return (
int((math.floor(point[0] / width))),
int((math.floor(point[1] / height)))
)
def getPointsFromKey(self, key):
valueFromKey = self.grid.get(key, [])
if key not in self.grid:
self.grid[key] = []
return valueFromKey
def insert(self, point):
k = self.key(point)
list = self.getPointsFromKey(k)
list.append(point)
dict = {k: list}
self.grid.update(dict)
return
def delete(self, point):
k = self.key(point)
list = self.getPointsFromKey(k)
if point in list:
list.remove(point)
dict = {k: list}
self.grid.update(dict)
else:
raise ValueError('Point is not in hash table')
if __name__ == "__main__":
hashMap = HashMap(10, 10)
hashMap.insert((20, 20))
grid = hashMap.getPointsFromKey((2, 2))
print(grid)