mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-13 12:58:02 +00:00
Update hash map (#104)
Update constraints, test cases, algorithm, code, and tests.
This commit is contained in:
@@ -39,7 +39,11 @@
|
||||
"* For collision resolution, can we use chaining?\n",
|
||||
" * Yes\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": [
|
||||
"## 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"
|
||||
"* `get` no matching key -> KeyError exception\n",
|
||||
"* `get` matching key -> value\n",
|
||||
"* `set` no matchin gkey -> new key, value\n",
|
||||
"* `set` matching key -> update value\n",
|
||||
"* `remove` no matching key -> KeyError exception\n",
|
||||
"* `remove` matching key -> remove key, value"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -135,7 +139,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_hash_map.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestHashMap(object):\n",
|
||||
@@ -146,7 +150,7 @@
|
||||
" hash_table = HashTable(10)\n",
|
||||
"\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",
|
||||
" print(\"Test: set on an empty hash table index\")\n",
|
||||
" hash_table.set(0, 'foo')\n",
|
||||
@@ -167,10 +171,10 @@
|
||||
" print(\"Test: remove on a key that already exists\")\n",
|
||||
" hash_table.remove(10)\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",
|
||||
" 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",
|
||||
" print('Success: test_end_to_end')\n",
|
||||
"\n",
|
||||
|
||||
Reference in New Issue
Block a user