Reworked notebook: Added more detail to clarifying questions and test cases. Reworked unit test.

This commit is contained in:
Donne Martin 2015-06-24 18:21:25 -04:00
parent afa2f60483
commit 786f33eb72

View File

@ -17,7 +17,8 @@
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Pythonic-Code](#Pythonic-Code)"
"* [Pythonic-Code](#Pythonic-Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
@ -26,8 +27,14 @@
"source": [
"## Clarifying Questions\n",
"\n",
"* Are the keys integers only?\n",
" * Yes"
"*Problem statements are sometimes intentionally ambiguous. Asking clarifying questions, identifying constraints, and stating assumptions help to ensure you code the intended solution.*\n",
"\n",
"* For simplicity, are the keys integers only?\n",
" * Yes\n",
"* For collision resolution, can we use linked lists?\n",
" * Yes\n",
"* Do we have to worry about load factors?\n",
" * No"
]
},
{
@ -36,6 +43,8 @@
"source": [
"## Test Cases\n",
"\n",
"*Identifying and running through general and edge cases are important. You generally will not be asked to write a unit test like what is shown below.*\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",
@ -97,9 +106,9 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
@ -138,37 +147,76 @@
" del self.table[hash_index][i]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test: get on an empty hash table index\n",
"Test: set on an empty hash table index\n",
"Test: set on a non empty hash table index\n",
"Test: set on a key that already exists\n",
"Test: remove on a key that already exists\n",
"Test: remove on a key that doesn't exist\n",
"Success: test_end_to_end\n"
]
}
],
"source": [
"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)"
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" \n",
" # TODO: It would be better if we had unit tests for each\n",
" # HashTable method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
" hash_table = HashTable(10)\n",
" \n",
" print(\"Test: get on an empty hash table index\")\n",
" assert_equal(hash_table.get(0), None)\n",
" \n",
" print(\"Test: set on an empty hash table index\")\n",
" hash_table.set(0, 'foo')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" hash_table.set(1, 'bar')\n",
" assert_equal(hash_table.get(1), 'bar')\n",
" \n",
" print(\"Test: set on a non empty hash table index\")\n",
" hash_table.set(10, 'foo2')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), 'foo2')\n",
" \n",
" print(\"Test: set on a key that already exists\")\n",
" hash_table.set(10, 'foo3')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), 'foo3')\n",
" \n",
" 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",
" \n",
" print(\"Test: remove on a key that doesn't exist\")\n",
" hash_table.remove(-1)\n",
" \n",
" print('Success: test_end_to_end')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_end_to_end()"
]
}
],