mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added notebook solving the following: Implement a hash table with set, get, and remove methods.
This commit is contained in:
parent
356d212a44
commit
77518b3a93
192
arrays-strings/hash-map.ipynb
Normal file
192
arrays-strings/hash-map.ipynb
Normal file
|
@ -0,0 +1,192 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Problem: Implement a hash table with set, get, and remove methods.\n",
|
||||||
|
"\n",
|
||||||
|
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||||
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
|
"* [Algorithm](#Algorithm)\n",
|
||||||
|
"* [Code](#Code)\n",
|
||||||
|
"* [Pythonic-Code](#Pythonic-Code)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Clarifying Questions\n",
|
||||||
|
"\n",
|
||||||
|
"* Are the keys integers only?\n",
|
||||||
|
" * Yes"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Test Cases\n",
|
||||||
|
"\n",
|
||||||
|
"* get on an empty hash table index\n",
|
||||||
|
"* set on an empty hash table index\n",
|
||||||
|
"* set on a non empty hash table index\n",
|
||||||
|
"* set on a key that already exists\n",
|
||||||
|
"* remove on a key with an entry\n",
|
||||||
|
"* remove on a key without an entry"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Algorithm\n",
|
||||||
|
"\n",
|
||||||
|
"### Hash Function\n",
|
||||||
|
"\n",
|
||||||
|
"* Return key % table size\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"* Time: O(1)\n",
|
||||||
|
"* Space: O(1)\n",
|
||||||
|
"\n",
|
||||||
|
"### Set\n",
|
||||||
|
"\n",
|
||||||
|
"* Get hash index for lookup\n",
|
||||||
|
"* If key exists, replace\n",
|
||||||
|
"* Else, add\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"* Time: O(1) average and best, O(n) worst\n",
|
||||||
|
"* Space: O(1) space for newly added element\n",
|
||||||
|
"\n",
|
||||||
|
"### Get\n",
|
||||||
|
"\n",
|
||||||
|
"* Get hash index for lookup\n",
|
||||||
|
"* If key exists, return value\n",
|
||||||
|
"* Else, return NULL\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"* Time: O(1) average and best, O(n) worst\n",
|
||||||
|
"* Space: O(1)\n",
|
||||||
|
"\n",
|
||||||
|
"### Remove\n",
|
||||||
|
"\n",
|
||||||
|
"* Get hash index for lookup\n",
|
||||||
|
"* If key exists, delete the item\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"* Time: O(1) average and best, O(n) worst\n",
|
||||||
|
"* Space: O(1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Code"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class Item(object):\n",
|
||||||
|
" def __init__(self, key, value):\n",
|
||||||
|
" self.key = key\n",
|
||||||
|
" self.value = value\n",
|
||||||
|
"\n",
|
||||||
|
"class HashTable(object):\n",
|
||||||
|
" def __init__(self, size):\n",
|
||||||
|
" self.size = size\n",
|
||||||
|
" self.table = [[] for _ in xrange(self.size)]\n",
|
||||||
|
"\n",
|
||||||
|
" def hash_function(self, key):\n",
|
||||||
|
" return key % self.size\n",
|
||||||
|
"\n",
|
||||||
|
" def set(self, key, value):\n",
|
||||||
|
" hash_index = self.hash_function(key)\n",
|
||||||
|
" for item in self.table[hash_index]:\n",
|
||||||
|
" if item.key == key:\n",
|
||||||
|
" item.value = value\n",
|
||||||
|
" return\n",
|
||||||
|
" self.table[hash_index].append(Item(key, value))\n",
|
||||||
|
"\n",
|
||||||
|
" def get(self, key):\n",
|
||||||
|
" hash_index = self.hash_function(key)\n",
|
||||||
|
" for item in self.table[hash_index]:\n",
|
||||||
|
" if item.key == key:\n",
|
||||||
|
" return item.value\n",
|
||||||
|
" return None\n",
|
||||||
|
"\n",
|
||||||
|
" def remove(self, key):\n",
|
||||||
|
" hash_index = self.hash_function(key)\n",
|
||||||
|
" for i, item in enumerate(self.table[hash_index]):\n",
|
||||||
|
" if item.key == key:\n",
|
||||||
|
" del self.table[hash_index][i]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#* get and set on an empty hash\n",
|
||||||
|
"#* get and set on a non empty hash\n",
|
||||||
|
"#* get and set on a hash index with collision\n",
|
||||||
|
"table_size = 10\n",
|
||||||
|
"hash_table = HashTable(table_size)\n",
|
||||||
|
"print(\"get on an empty hash table index\")\n",
|
||||||
|
"print(hash_table.get(0))\n",
|
||||||
|
"print(\"set on an empty hash table index\")\n",
|
||||||
|
"hash_table.set(0, 'foo')\n",
|
||||||
|
"print(hash_table.get(0))\n",
|
||||||
|
"hash_table.set(1, 'bar')\n",
|
||||||
|
"print(hash_table.get(1))\n",
|
||||||
|
"print(\"set on a non empty hash table index\")\n",
|
||||||
|
"hash_table.set(10, 'foo2')\n",
|
||||||
|
"print(hash_table.get(0))\n",
|
||||||
|
"print(hash_table.get(10))\n",
|
||||||
|
"print(\"set on a key that already exists\")\n",
|
||||||
|
"hash_table.set(10, 'foo3')\n",
|
||||||
|
"print(hash_table.get(0))\n",
|
||||||
|
"print(hash_table.get(10))\n",
|
||||||
|
"print(\"remove on a key that already exists\")\n",
|
||||||
|
"hash_table.remove(10)\n",
|
||||||
|
"print(hash_table.get(0))\n",
|
||||||
|
"print(hash_table.get(10))\n",
|
||||||
|
"print(\"remove on a key that doesn't exist\")\n",
|
||||||
|
"hash_table.remove(-1)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 2",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python2"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.9"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user