Update hash map (#104)

Update constraints, test cases, algorithm, code, and tests.
This commit is contained in:
Donne Martin 2016-10-16 21:28:31 -04:00 committed by GitHub
parent 0f49f67a72
commit 400dc8b55a
3 changed files with 42 additions and 32 deletions

View File

@ -39,7 +39,11 @@
"* For collision resolution, can we use chaining?\n", "* For collision resolution, can we use chaining?\n",
" * Yes\n", " * Yes\n",
"* Do we have to worry about load factors?\n", "* Do we have to worry about load factors?\n",
" * No" " * No\n",
"* Do we have to validate inputs?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
] ]
}, },
{ {
@ -48,12 +52,12 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* get on an empty hash table index\n", "* `get` no matching key -> KeyError exception\n",
"* set on an empty hash table index\n", "* `get` matching key -> value\n",
"* set on a non empty hash table index\n", "* `set` no matchin gkey -> new key, value\n",
"* set on a key that already exists\n", "* `set` matching key -> update value\n",
"* remove on a key with an entry\n", "* `remove` no matching key -> KeyError exception\n",
"* remove on a key without an entry" "* `remove` matching key -> remove key, value"
] ]
}, },
{ {
@ -135,7 +139,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_hash_map.py\n", "# %load test_hash_map.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestHashMap(object):\n", "class TestHashMap(object):\n",
@ -146,7 +150,7 @@
" hash_table = HashTable(10)\n", " hash_table = HashTable(10)\n",
"\n", "\n",
" print(\"Test: get on an empty hash table index\")\n", " print(\"Test: get on an empty hash table index\")\n",
" assert_equal(hash_table.get(0), None)\n", " assert_raises(KeyError, hash_table.get, 0)\n",
"\n", "\n",
" print(\"Test: set on an empty hash table index\")\n", " print(\"Test: set on an empty hash table index\")\n",
" hash_table.set(0, 'foo')\n", " hash_table.set(0, 'foo')\n",
@ -167,10 +171,10 @@
" print(\"Test: remove on a key that already exists\")\n", " print(\"Test: remove on a key that already exists\")\n",
" hash_table.remove(10)\n", " hash_table.remove(10)\n",
" assert_equal(hash_table.get(0), 'foo')\n", " assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), None)\n", " assert_raises(KeyError, hash_table.get, 10)\n",
"\n", "\n",
" print(\"Test: remove on a key that doesn't exist\")\n", " print(\"Test: remove on a key that doesn't exist\")\n",
" hash_table.remove(-1)\n", " assert_raises(KeyError, hash_table.remove, -1)\n",
"\n", "\n",
" print('Success: test_end_to_end')\n", " print('Success: test_end_to_end')\n",
"\n", "\n",

View File

@ -35,10 +35,14 @@
"\n", "\n",
"* For simplicity, are the keys integers only?\n", "* For simplicity, are the keys integers only?\n",
" * Yes\n", " * Yes\n",
"* For collision resolution, can we use linked lists?\n", "* For collision resolution, can we use chaining?\n",
" * Yes\n", " * Yes\n",
"* Do we have to worry about load factors?\n", "* Do we have to worry about load factors?\n",
" * No" " * No\n",
"* Do we have to validate inputs?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
] ]
}, },
{ {
@ -47,12 +51,12 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* get on an empty hash table index\n", "* `get` no matching key -> KeyError exception\n",
"* set on an empty hash table index\n", "* `get` matching key -> value\n",
"* set on a non empty hash table index\n", "* `set` no matchin gkey -> new key, value\n",
"* set on a key that already exists\n", "* `set` matching key -> update value\n",
"* remove on a key with an entry\n", "* `remove` no matching key -> KeyError exception\n",
"* remove on a key without an entry" "* `remove` matching key -> remove key, value"
] ]
}, },
{ {
@ -83,7 +87,7 @@
"\n", "\n",
"* Get hash index for lookup\n", "* Get hash index for lookup\n",
"* If key exists, return value\n", "* If key exists, return value\n",
"* Else, return None\n", "* Else, raise KeyError\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(1) average and best, O(n) worst\n", "* Time: O(1) average and best, O(n) worst\n",
@ -93,6 +97,7 @@
"\n", "\n",
"* Get hash index for lookup\n", "* Get hash index for lookup\n",
"* If key exists, delete the item\n", "* If key exists, delete the item\n",
"* Else, raise KeyError\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(1) average and best, O(n) worst\n", "* Time: O(1) average and best, O(n) worst\n",
@ -143,14 +148,15 @@
" for item in self.table[hash_index]:\n", " for item in self.table[hash_index]:\n",
" if item.key == key:\n", " if item.key == key:\n",
" return item.value\n", " return item.value\n",
" return None\n", " raise KeyError('Key not found')\n",
"\n", "\n",
" def remove(self, key):\n", " def remove(self, key):\n",
" hash_index = self.hash_function(key)\n", " hash_index = self.hash_function(key)\n",
" for i, item in enumerate(self.table[hash_index]):\n", " for index, item in enumerate(self.table[hash_index]):\n",
" if item.key == key:\n", " if item.key == key:\n",
" del self.table[hash_index][i]\n", " del self.table[hash_index][index]\n",
" return" " return\n",
" raise KeyError('Key not found')"
] ]
}, },
{ {
@ -177,7 +183,7 @@
], ],
"source": [ "source": [
"%%writefile test_hash_map.py\n", "%%writefile test_hash_map.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestHashMap(object):\n", "class TestHashMap(object):\n",
@ -188,7 +194,7 @@
" hash_table = HashTable(10)\n", " hash_table = HashTable(10)\n",
"\n", "\n",
" print(\"Test: get on an empty hash table index\")\n", " print(\"Test: get on an empty hash table index\")\n",
" assert_equal(hash_table.get(0), None)\n", " assert_raises(KeyError, hash_table.get, 0)\n",
"\n", "\n",
" print(\"Test: set on an empty hash table index\")\n", " print(\"Test: set on an empty hash table index\")\n",
" hash_table.set(0, 'foo')\n", " hash_table.set(0, 'foo')\n",
@ -209,10 +215,10 @@
" print(\"Test: remove on a key that already exists\")\n", " print(\"Test: remove on a key that already exists\")\n",
" hash_table.remove(10)\n", " hash_table.remove(10)\n",
" assert_equal(hash_table.get(0), 'foo')\n", " assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), None)\n", " assert_raises(KeyError, hash_table.get, 10)\n",
"\n", "\n",
" print(\"Test: remove on a key that doesn't exist\")\n", " print(\"Test: remove on a key that doesn't exist\")\n",
" hash_table.remove(-1)\n", " assert_raises(KeyError, hash_table.remove, -1)\n",
"\n", "\n",
" print('Success: test_end_to_end')\n", " print('Success: test_end_to_end')\n",
"\n", "\n",

View File

@ -1,4 +1,4 @@
from nose.tools import assert_equal from nose.tools import assert_equal, assert_raises
class TestHashMap(object): class TestHashMap(object):
@ -9,7 +9,7 @@ class TestHashMap(object):
hash_table = HashTable(10) hash_table = HashTable(10)
print("Test: get on an empty hash table index") print("Test: get on an empty hash table index")
assert_equal(hash_table.get(0), None) assert_raises(KeyError, hash_table.get, 0)
print("Test: set on an empty hash table index") print("Test: set on an empty hash table index")
hash_table.set(0, 'foo') hash_table.set(0, 'foo')
@ -30,10 +30,10 @@ class TestHashMap(object):
print("Test: remove on a key that already exists") print("Test: remove on a key that already exists")
hash_table.remove(10) hash_table.remove(10)
assert_equal(hash_table.get(0), 'foo') assert_equal(hash_table.get(0), 'foo')
assert_equal(hash_table.get(10), None) assert_raises(KeyError, hash_table.get, 10)
print("Test: remove on a key that doesn't exist") print("Test: remove on a key that doesn't exist")
hash_table.remove(-1) assert_raises(KeyError, hash_table.remove, -1)
print('Success: test_end_to_end') print('Success: test_end_to_end')