-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap_class.py
More file actions
33 lines (28 loc) · 907 Bytes
/
hashmap_class.py
File metadata and controls
33 lines (28 loc) · 907 Bytes
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
class Hashmap:
def __init__(self):
self.MAX = 100
self.arr = [None for i in range(self.MAX)]
def get_hash(self, key):
sum = 0
for char in key:
sum += ord(char)
return sum % self.MAX # MAX is the size of array. So we want indices from 0 to 9 that's why we did sum%10
def add(self, key, value):
index = self.get_hash(key)
self.arr[index] = value
def get_data(self, key):
index = self.get_hash(key)
return self.arr[index]
def delete(self, key):
index = self.get_hash(key)
self.arr[index] = None
hash_map = Hashmap()
hash_map.add('march 6', 200)
hash_map.add('march 5', 300)
hash_map.add('march 13', 400)
hash_map.add('march 12', 400)
hash_map.add('feb 11', 400)
hash_map.add('jan 12', 400)
hash_map.add('april 13', 400)
hash_map.delete('april 13')
print(hash_map.get_data('april 13'))