mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Reworked notebook: Added more detail to clarifying questions and test cases. Reworked unit test.
This commit is contained in:
parent
afa2f60483
commit
786f33eb72
|
@ -17,7 +17,8 @@
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm](#Algorithm)\n",
|
||||||
"* [Code](#Code)\n",
|
"* [Code](#Code)\n",
|
||||||
"* [Pythonic-Code](#Pythonic-Code)"
|
"* [Pythonic-Code](#Pythonic-Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -26,8 +27,14 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Clarifying Questions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Are the keys integers only?\n",
|
"*Problem statements are sometimes intentionally ambiguous. Asking clarifying questions, identifying constraints, and stating assumptions help to ensure you code the intended solution.*\n",
|
||||||
" * Yes"
|
"\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": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\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",
|
"* get on an empty hash table index\n",
|
||||||
"* set 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 non empty hash table index\n",
|
||||||
|
@ -97,9 +106,9 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -138,37 +147,76 @@
|
||||||
" del self.table[hash_index][i]"
|
" del self.table[hash_index][i]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"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": [
|
"source": [
|
||||||
"table_size = 10\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"hash_table = HashTable(table_size)\n",
|
"\n",
|
||||||
"print(\"get on an empty hash table index\")\n",
|
"class Test(object):\n",
|
||||||
"print(hash_table.get(0))\n",
|
" \n",
|
||||||
"print(\"set on an empty hash table index\")\n",
|
" # TODO: It would be better if we had unit tests for each\n",
|
||||||
"hash_table.set(0, 'foo')\n",
|
" # HashTable method in addition to the following end-to-end test\n",
|
||||||
"print(hash_table.get(0))\n",
|
" def test_end_to_end(self):\n",
|
||||||
"hash_table.set(1, 'bar')\n",
|
" hash_table = HashTable(10)\n",
|
||||||
"print(hash_table.get(1))\n",
|
" \n",
|
||||||
"print(\"set on a non empty hash table index\")\n",
|
" print(\"Test: get on an empty hash table index\")\n",
|
||||||
"hash_table.set(10, 'foo2')\n",
|
" assert_equal(hash_table.get(0), None)\n",
|
||||||
"print(hash_table.get(0))\n",
|
" \n",
|
||||||
"print(hash_table.get(10))\n",
|
" print(\"Test: set on an empty hash table index\")\n",
|
||||||
"print(\"set on a key that already exists\")\n",
|
" hash_table.set(0, 'foo')\n",
|
||||||
"hash_table.set(10, 'foo3')\n",
|
" assert_equal(hash_table.get(0), 'foo')\n",
|
||||||
"print(hash_table.get(0))\n",
|
" hash_table.set(1, 'bar')\n",
|
||||||
"print(hash_table.get(10))\n",
|
" assert_equal(hash_table.get(1), 'bar')\n",
|
||||||
"print(\"remove on a key that already exists\")\n",
|
" \n",
|
||||||
"hash_table.remove(10)\n",
|
" print(\"Test: set on a non empty hash table index\")\n",
|
||||||
"print(hash_table.get(0))\n",
|
" hash_table.set(10, 'foo2')\n",
|
||||||
"print(hash_table.get(10))\n",
|
" assert_equal(hash_table.get(0), 'foo')\n",
|
||||||
"print(\"remove on a key that doesn't exist\")\n",
|
" assert_equal(hash_table.get(10), 'foo2')\n",
|
||||||
"hash_table.remove(-1)"
|
" \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()"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user