2015-05-15 18:08:46 +08:00
{
" cells " : [
2015-06-18 04:39:43 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-07-05 19:59:00 +08:00
" <small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small> "
2015-07-03 11:06:52 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Solution Notebook "
2015-06-18 04:39:43 +08:00
]
} ,
2015-05-15 18:08:46 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-05-24 05:01:45 +08:00
" ## Problem: Implement a stack with push, pop, peek, and is_empty methods using a linked list. \n " ,
2015-05-15 18:08:46 +08:00
" \n " ,
2015-06-30 17:55:58 +08:00
" * [Constraints](#Constraints) \n " ,
2015-05-15 18:08:46 +08:00
" * [Test Cases](#Test-Cases) \n " ,
" * [Algorithm](#Algorithm) \n " ,
" * [Code](#Code) \n " ,
2015-06-27 18:35:21 +08:00
" * [Unit Test](#Unit-Test) \n " ,
2015-05-15 18:08:46 +08:00
" * [Pythonic-Code](#Pythonic-Code) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-06-28 19:13:27 +08:00
" ## Constraints \n " ,
2015-06-27 18:35:21 +08:00
" \n " ,
2015-05-15 18:08:46 +08:00
" * None "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
" ### Push \n " ,
" \n " ,
" * Push to empty stack \n " ,
" * Push to non-empty stack \n " ,
" \n " ,
" ### Pop \n " ,
" \n " ,
" * Pop on empty stack \n " ,
" * Pop on single element stack \n " ,
" * Pop on multiple element stack \n " ,
" \n " ,
" ### Peek \n " ,
" \n " ,
" * Peek on empty stack \n " ,
2015-05-24 05:01:45 +08:00
" * Peek on one or more element stack \n " ,
" \n " ,
" ### Is Empty \n " ,
" \n " ,
" * Is empty on empty stack \n " ,
" * Is empty on one or more element stack "
2015-05-15 18:08:46 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Algorithm \n " ,
" \n " ,
" ### Push \n " ,
" \n " ,
" * Create new node with value \n " ,
" * Set node ' s next to top \n " ,
" * Set top to node \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) \n " ,
" \n " ,
" ### Pop \n " ,
" \n " ,
2015-07-06 17:44:06 +08:00
" * If stack is empty, return None \n " ,
2015-05-15 18:08:46 +08:00
" * Else \n " ,
" * Save top ' s value \n " ,
" * Set top to top.next \n " ,
" * Return saved value \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) \n " ,
" \n " ,
" ### Peek \n " ,
" \n " ,
2015-07-06 17:44:06 +08:00
" * If stack is empty, return None \n " ,
2015-05-15 18:08:46 +08:00
" * Else return top ' s value \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
2015-05-24 05:01:45 +08:00
" * Space: O(1) \n " ,
" \n " ,
" ### Is Empty \n " ,
" * If peek has a value, return False \n " ,
" * Else return True \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
2015-05-15 18:08:46 +08:00
" * Space: O(1) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
2015-06-27 18:35:21 +08:00
" execution_count " : 1 ,
2015-05-15 18:08:46 +08:00
" metadata " : {
" collapsed " : false
} ,
2015-06-27 18:35:21 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting stack.py \n "
]
}
] ,
2015-05-15 18:08:46 +08:00
" source " : [
" %% writefile stack.py \n " ,
" class Node(object): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2016-02-20 21:11:49 +08:00
" def __init__(self, data, next=None): \n " ,
2015-05-15 18:08:46 +08:00
" self.data = data \n " ,
2016-02-20 21:11:49 +08:00
" self.next = next \n " ,
2015-05-15 18:08:46 +08:00
" \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-05-15 18:08:46 +08:00
" class Stack(object): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-05-15 18:08:46 +08:00
" def __init__(self, top=None): \n " ,
" self.top = top \n " ,
" \n " ,
" def push(self, data): \n " ,
2016-02-20 21:11:49 +08:00
" node = Node(data, self.top) \n " ,
2015-05-15 18:08:46 +08:00
" self.top = node \n " ,
" \n " ,
" def pop(self): \n " ,
2016-02-20 21:11:49 +08:00
" if self.top is None: \n " ,
" return None \n " ,
" data = self.top.data \n " ,
" self.top = self.top.next \n " ,
" return data \n " ,
2015-05-15 18:08:46 +08:00
" \n " ,
" def peek(self): \n " ,
2016-02-20 21:11:49 +08:00
" return self.top.data if self.top is not None else None \n " ,
2015-05-24 05:01:45 +08:00
" \n " ,
" def is_empty(self): \n " ,
" return self.peek() is None "
2015-05-15 18:08:46 +08:00
]
} ,
{
" cell_type " : " code " ,
2015-06-27 18:35:21 +08:00
" execution_count " : 2 ,
2015-05-15 18:08:46 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
" %r un stack.py "
]
} ,
2015-06-27 18:35:21 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test \n " ,
2015-07-03 11:06:52 +08:00
" \n "
2015-06-27 18:35:21 +08:00
]
} ,
2015-05-15 18:08:46 +08:00
{
" cell_type " : " code " ,
2015-06-27 18:35:21 +08:00
" execution_count " : 3 ,
2015-05-15 18:08:46 +08:00
" metadata " : {
" collapsed " : false
} ,
2015-06-27 18:35:21 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
2015-07-03 11:06:52 +08:00
" Overwriting test_stack.py \n "
2015-06-27 18:35:21 +08:00
]
}
] ,
2015-05-15 18:08:46 +08:00
" source " : [
2015-07-03 11:06:52 +08:00
" %% writefile test_stack.py \n " ,
2015-06-27 18:35:21 +08:00
" from nose.tools import assert_equal \n " ,
" \n " ,
2015-07-03 11:06:52 +08:00
" \n " ,
" class TestStack(object): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-06-27 18:35:21 +08:00
" # 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 " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-06-27 18:35:21 +08:00
" print( ' Success: test_end_to_end ' ) \n " ,
" \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-07-03 11:06:52 +08:00
" def main(): \n " ,
" test = TestStack() \n " ,
" test.test_end_to_end() \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
" \n " ,
2015-06-27 18:35:21 +08:00
" if __name__ == ' __main__ ' : \n " ,
2015-07-03 11:06:52 +08:00
" main() "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 4 ,
" metadata " : {
" collapsed " : false
} ,
" 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 " : [
" %r un -i test_stack.py "
2015-05-15 18:08:46 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Pythonic-Code "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" Source: https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-stacks \n " ,
" <pre> \n " ,
" 5.1.1. Using Lists as Stacks \n " ,
" The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example: \n " ,
" \n " ,
" >>> stack = [3, 4, 5] \n " ,
" >>> stack.append(6) \n " ,
" >>> stack.append(7) \n " ,
" >>> stack \n " ,
" [3, 4, 5, 6, 7] \n " ,
" >>> stack.pop() \n " ,
" 7 \n " ,
" >>> stack \n " ,
" [3, 4, 5, 6] \n " ,
" >>> stack.pop() \n " ,
" 6 \n " ,
" >>> stack.pop() \n " ,
" 5 \n " ,
" >>> stack \n " ,
" [3, 4] \n " ,
" </pre> "
]
}
] ,
" metadata " : {
" kernelspec " : {
2016-02-20 21:11:49 +08:00
" display_name " : " Python 3 " ,
2015-05-15 18:08:46 +08:00
" language " : " python " ,
2016-02-20 21:11:49 +08:00
" name " : " python3 "
2015-05-15 18:08:46 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2016-02-20 21:11:49 +08:00
" version " : 3
2015-05-15 18:08:46 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2016-02-20 21:11:49 +08:00
" pygments_lexer " : " ipython3 " ,
" version " : " 3.4.3 "
2015-05-15 18:08:46 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}