mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 10:28:03 +00:00
Added hash map challenge.
This commit is contained in:
@@ -4,7 +4,14 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Solution Notebook"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -17,7 +24,6 @@
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)\n",
|
||||
"* [Unit Test](#Unit-Test)"
|
||||
]
|
||||
},
|
||||
@@ -27,7 +33,7 @@
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"\n",
|
||||
"* For simplicity, are the keys integers only?\n",
|
||||
" * Yes\n",
|
||||
@@ -111,11 +117,13 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Item(object):\n",
|
||||
" \n",
|
||||
" def __init__(self, key, value):\n",
|
||||
" self.key = key\n",
|
||||
" self.value = value\n",
|
||||
"\n",
|
||||
"class HashTable(object):\n",
|
||||
" \n",
|
||||
" def __init__(self, size):\n",
|
||||
" self.size = size\n",
|
||||
" self.table = [[] for _ in xrange(self.size)]\n",
|
||||
@@ -152,13 +160,6 @@
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
@@ -170,20 +171,17 @@
|
||||
"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"
|
||||
"Overwriting test_hash_map.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_hash_map.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
"\n",
|
||||
"class TestHashMap(object):\n",
|
||||
" \n",
|
||||
" # TODO: It would be better if we had unit tests for each\n",
|
||||
" # method in addition to the following end-to-end test\n",
|
||||
" def test_end_to_end(self):\n",
|
||||
@@ -218,9 +216,37 @@
|
||||
" \n",
|
||||
" print('Success: test_end_to_end')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestHashMap()\n",
|
||||
" test.test_end_to_end()\n",
|
||||
" \n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
" test.test_end_to_end()"
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"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": [
|
||||
"run -i test_hash_map.py"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user