-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.py
46 lines (39 loc) · 1.07 KB
/
hash_table.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
# Dictionary is python specific implementation of hash table
# Mode operation SUM / 100 (size) -> remainder
class HashTable:
def __init__(self):
self.MAX = 10
self.arr = [[] for i in range(self.MAX)]
def get_hash(self, key):
sum_of_asc = 0
for char in key:
sum_of_asc += ord(char)
return sum_of_asc % self.MAX
def __setitem__(self, key, val):
h = self.get_hash(key)
found = False
for idx, element in enumerate(self.arr[h]):
if len(element) == 2 and element[0] == key:
self.arr[h][idx] = (key, val)
found = True
break
if not found:
self.arr[h].append((key, val))
def __getitem__(self, key):
h = self.get_hash(key)
for element in self.arr[h]:
if element[0] == key:
return element[1]
def __delitem__(self, key):
h = self.get_hash(key)
for idx, element in enumerate(self.arr[h]):
if element[0] == key:
del self.arr[h][idx]
t = HashTable()
t['march 6'] = 12
t['march 9'] = 30
t['march 17'] = 66
t['march 9'] = 99
del t['march 9']
print(t['march 17'])
print(t.arr)