Skip to content

Commit bb1a64e

Browse files
committed
hash table - chaining
1 parent e51bcaf commit bb1a64e

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

hash-table/chaining.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
2+
class KeyValue:
3+
def __init__(self, key: int = None, value: str = None):
4+
self.key: int = key
5+
self.value: str = value
6+
7+
class HashMap:
8+
def __init__(self, length: int = 11):
9+
self._length: int = length
10+
self._items: list = [None] * self._length
11+
self._elements_count: int = 0
12+
13+
def put(self, key: int, value: str):
14+
hash: int = self.hash(key)
15+
if self._items[hash] is None:
16+
self._items[hash] = [KeyValue(key, value)]
17+
self._elements_count += 1
18+
else:
19+
i: int = 0
20+
found: bool = False
21+
while i < len(self._items[hash]) and not found:
22+
if self._items[hash][i].key == key:
23+
found = True
24+
else:
25+
i += 1
26+
if found:
27+
self._items[hash][i].value = value
28+
else:
29+
self._items[hash].append(KeyValue(key, value))
30+
self._elements_count += 1
31+
32+
def get(self, key: int) -> str:
33+
hash: int = self.hash(key)
34+
if self._items[hash] is None:
35+
return None
36+
else:
37+
i: int = 0
38+
found: bool = False
39+
while i < len(self._items[hash]) and not found:
40+
if self._items[hash][i].key == key:
41+
found = True
42+
else:
43+
i += 1
44+
if found:
45+
return self._items[hash][i].value
46+
else:
47+
return None
48+
49+
def contains(self, key: int) -> bool:
50+
hash: int = self.hash(key)
51+
if self._items[hash] is None:
52+
return False
53+
else:
54+
i: int = 0
55+
found: bool = False
56+
while i < len(self._items[hash]) and not found:
57+
if self._items[hash][i].key == key:
58+
found = True
59+
else:
60+
i += 1
61+
return found
62+
63+
def delete(self, key):
64+
hash: int = self.hash(key)
65+
if self._items[hash] is None:
66+
return
67+
else:
68+
i: int = 0
69+
found: bool = False
70+
while i < len(self._items[hash]) and not found:
71+
if self._items[hash][i].key == key:
72+
found = True
73+
else:
74+
i += 1
75+
if found:
76+
self._items[hash].pop(i)
77+
self._elements_count -= 1
78+
79+
def size(self) -> int:
80+
return self._elements_count
81+
82+
def hash(self, key: int) -> int:
83+
return key % self._length
84+
85+
86+
hm: HashMap = HashMap()
87+
hm.put(11, "string 11")
88+
hm.put(22, "string 22")
89+
hm.put(33, "string 33")
90+
hm.put(44, "string 44")
91+
hm.put(12, "string 12")
92+
hm.put(21, "string 21")
93+
94+
95+
print("Get 11", hm.get(11))
96+
print("Get 33", hm.get(33))
97+
print("Get 21", hm.get(21))
98+
print("Get 7", hm.get(7))
99+
100+
print("Contains key 7", hm.contains(7))
101+
print("Contains key 33", hm.contains(33))
102+
103+
104+
print("Delete key 7", hm.delete(7))
105+
print("Delete key 33", hm.delete(33))
106+
107+
print("Contains key 33", hm.contains(33))
108+
109+
110+
hm.put(14, "string 14")
111+
112+
print("Contains key 33", hm.contains(33))

0 commit comments

Comments
 (0)