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 constraints and test cases. Reworked unit test.
This commit is contained in:
parent
5719b04c10
commit
dd0e94fdd1
|
@ -13,10 +13,11 @@
|
|||
"source": [
|
||||
"## Problem: Implement a stack with push, pop, peek, and is_empty methods using a linked list.\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Unit Test](#Unit-Test)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)"
|
||||
]
|
||||
},
|
||||
|
@ -24,7 +25,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Clarifying Questions\n",
|
||||
"## Constraints and Assumptions\n",
|
||||
"\n",
|
||||
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"\n",
|
||||
"* None"
|
||||
]
|
||||
|
@ -112,11 +115,19 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Overwriting stack.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile stack.py\n",
|
||||
"\n",
|
||||
|
@ -152,7 +163,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -161,36 +172,70 @@
|
|||
"%run stack.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test\n",
|
||||
"\n",
|
||||
"*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": null,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test: Empty stack\n",
|
||||
"Test: One element\n",
|
||||
"Test: More than one element\n",
|
||||
"Success: test_end_to_end\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print('Empty stack')\n",
|
||||
"stack = Stack()\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.pop())\n",
|
||||
"print('One element')\n",
|
||||
"top = Node(5)\n",
|
||||
"stack = Stack(top)\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print('More than one element')\n",
|
||||
"stack = Stack()\n",
|
||||
"stack.push(1)\n",
|
||||
"stack.push(2)\n",
|
||||
"stack.push(3)\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.is_empty())\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.is_empty())"
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\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",
|
||||
" print('Test: Empty stack')\n",
|
||||
" stack = Stack()\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" assert_equal(stack.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Test: One element')\n",
|
||||
" top = Node(5)\n",
|
||||
" stack = Stack(top)\n",
|
||||
" assert_equal(stack.pop(), 5)\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
"\n",
|
||||
" print('Test: More than one element')\n",
|
||||
" stack = Stack()\n",
|
||||
" stack.push(1)\n",
|
||||
" stack.push(2)\n",
|
||||
" stack.push(3)\n",
|
||||
" assert_equal(stack.pop(), 3)\n",
|
||||
" assert_equal(stack.peek(), 2)\n",
|
||||
" assert_equal(stack.pop(), 2)\n",
|
||||
" assert_equal(stack.peek(), 1)\n",
|
||||
" assert_equal(stack.is_empty(), False)\n",
|
||||
" assert_equal(stack.pop(), 1)\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" assert_equal(stack.is_empty(), True)\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