-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhashmap.py
62 lines (52 loc) · 1.76 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
# This is similar to another hashmap design problem, it's just that, here we don't handle collisions
# Also, we are given any prefixed size, so we are simply assuming size,
# This can be good question to ask or to assume (after asking interviewer) during the interview.
class MyHashMap:
def __init__(self):
"""
Initialize your data structure here.
"""
self.size = 10
self.map = [None] * self.size
def get_hash(self, key):
return key % self.size
def put(self, key: int, value: int) -> None:
"""
value will always be non-negative.
"""
index = self.get_hash(key)
if self.map[index] == None:
self.map[index] = list([key, value])
else:
self.map[index] = list([key, value])
def get(self, key: int) -> int:
"""
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
"""
index = self.get_hash(key)
if self.map[index] == None:
return -1
else:
return self.map[index][1]
def remove(self, key: int) -> None:
"""
Removes the mapping of the specified value key if this map contains a mapping for the key
"""
index = self.get_hash(key)
if self.map[index] == None:
return -1
else:
self.map[index] = None
def pretty_print(self):
print(self.map)
if __name__ == '__main__':
# Your MyHashMap object will be instantiated and called as such:
hashMap = MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
print(hashMap.get(1))
print(hashMap.get(3))
hashMap.put(2, 1);
print(hashMap.get(2))
hashMap.remove(2);
print(hashMap.get(2))