Skip to content

Commit 7595e2a

Browse files
committed
Add Hash Table solution
1 parent 0f517c7 commit 7595e2a

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

solutions/object_oriented_design/hash_table/__init__.py

Whitespace-only changes.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer-primer)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Design a hash map"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Constraints and assumptions\n",
22+
"\n",
23+
"* For simplicity, are the keys integers only?\n",
24+
" * Yes\n",
25+
"* For collision resolution, can we use chaining?\n",
26+
" * Yes\n",
27+
"* Do we have to worry about load factors?\n",
28+
" * No\n",
29+
"* Can we assume inputs are valid or do we have to validate them?\n",
30+
" * Assume they're valid\n",
31+
"* Can we assume this fits memory?\n",
32+
" * Yes"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"## Solution"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 1,
45+
"metadata": {
46+
"collapsed": false
47+
},
48+
"outputs": [
49+
{
50+
"name": "stdout",
51+
"output_type": "stream",
52+
"text": [
53+
"Overwriting hash_map.py\n"
54+
]
55+
}
56+
],
57+
"source": [
58+
"%%writefile hash_map.py\n",
59+
"class Item(object):\n",
60+
"\n",
61+
" def __init__(self, key, value):\n",
62+
" self.key = key\n",
63+
" self.value = value\n",
64+
"\n",
65+
"\n",
66+
"class HashTable(object):\n",
67+
"\n",
68+
" def __init__(self, size):\n",
69+
" self.size = size\n",
70+
" self.table = [[] for _ in range(self.size)]\n",
71+
"\n",
72+
" def _hash_function(self, key):\n",
73+
" return key % self.size\n",
74+
"\n",
75+
" def set(self, key, value):\n",
76+
" hash_index = self._hash_function(key)\n",
77+
" for item in self.table[hash_index]:\n",
78+
" if item.key == key:\n",
79+
" item.value = value\n",
80+
" return\n",
81+
" self.table[hash_index].append(Item(key, value))\n",
82+
"\n",
83+
" def get(self, key):\n",
84+
" hash_index = self._hash_function(key)\n",
85+
" for item in self.table[hash_index]:\n",
86+
" if item.key == key:\n",
87+
" return item.value\n",
88+
" raise KeyError('Key not found')\n",
89+
"\n",
90+
" def remove(self, key):\n",
91+
" hash_index = self._hash_function(key)\n",
92+
" for index, item in enumerate(self.table[hash_index]):\n",
93+
" if item.key == key:\n",
94+
" del self.table[hash_index][index]\n",
95+
" return\n",
96+
" raise KeyError('Key not found')"
97+
]
98+
}
99+
],
100+
"metadata": {
101+
"kernelspec": {
102+
"display_name": "Python 3",
103+
"language": "python",
104+
"name": "python3"
105+
},
106+
"language_info": {
107+
"codemirror_mode": {
108+
"name": "ipython",
109+
"version": 3
110+
},
111+
"file_extension": ".py",
112+
"mimetype": "text/x-python",
113+
"name": "python",
114+
"nbconvert_exporter": "python",
115+
"pygments_lexer": "ipython3",
116+
"version": "3.4.3"
117+
}
118+
},
119+
"nbformat": 4,
120+
"nbformat_minor": 0
121+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Item(object):
2+
3+
def __init__(self, key, value):
4+
self.key = key
5+
self.value = value
6+
7+
8+
class HashTable(object):
9+
10+
def __init__(self, size):
11+
self.size = size
12+
self.table = [[] for _ in range(self.size)]
13+
14+
def _hash_function(self, key):
15+
return key % self.size
16+
17+
def set(self, key, value):
18+
hash_index = self._hash_function(key)
19+
for item in self.table[hash_index]:
20+
if item.key == key:
21+
item.value = value
22+
return
23+
self.table[hash_index].append(Item(key, value))
24+
25+
def get(self, key):
26+
hash_index = self._hash_function(key)
27+
for item in self.table[hash_index]:
28+
if item.key == key:
29+
return item.value
30+
raise KeyError('Key not found')
31+
32+
def remove(self, key):
33+
hash_index = self._hash_function(key)
34+
for index, item in enumerate(self.table[hash_index]):
35+
if item.key == key:
36+
del self.table[hash_index][index]
37+
return
38+
raise KeyError('Key not found')

0 commit comments

Comments
 (0)