From 7f338b0e641dabe61ee61a36b7d69305f47a7144 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 21 Jun 2016 05:30:14 -0400 Subject: [PATCH] Polish set of stacks challenge and solution (#73) Update constraints, algorithm discussion, and code. --- .../set_of_stacks_challenge.ipynb | 12 ++++++--- .../set_of_stacks_solution.ipynb | 25 +++++++++++-------- .../set_of_stacks/test_set_of_stacks.py | 2 +- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/stacks_queues/set_of_stacks/set_of_stacks_challenge.ipynb b/stacks_queues/set_of_stacks/set_of_stacks_challenge.ipynb index 9055e43..c1ef2cf 100644 --- a/stacks_queues/set_of_stacks/set_of_stacks_challenge.ipynb +++ b/stacks_queues/set_of_stacks/set_of_stacks_challenge.ipynb @@ -36,9 +36,15 @@ "\n", "* Can we assume we already have a stack class that can be used for this problem?\n", " * Yes\n", + "* Are all stack bound by the same capacity?\n", + " * Yes\n", "* If a stack becomes full, should automatically create one?\n", " * Yes\n", "* If a stack becomes empty, should we delete it?\n", + " * Yes\n", + "* If we pop on an empty stack, should we return None?\n", + " * Yes\n", + "* Can we assume this fits memory?\n", " * Yes" ] }, @@ -111,7 +117,7 @@ "\n", "class SetOfStacks(object):\n", "\n", - " def __init__(self, capacity):\n", + " def __init__(self, indiv_stack_capacity):\n", " # TODO: Implement me\n", " pass\n", "\n", @@ -151,7 +157,7 @@ "\n", " def test_set_of_stacks(self):\n", " print('Test: Push on an empty stack')\n", - " stacks = SetOfStacks(capacity=2)\n", + " stacks = SetOfStacks(indiv_stack_capacity=2)\n", " stacks.push(3)\n", "\n", " print('Test: Push on a non-empty stack')\n", @@ -208,7 +214,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, diff --git a/stacks_queues/set_of_stacks/set_of_stacks_solution.ipynb b/stacks_queues/set_of_stacks/set_of_stacks_solution.ipynb index c71c419..49205b8 100644 --- a/stacks_queues/set_of_stacks/set_of_stacks_solution.ipynb +++ b/stacks_queues/set_of_stacks/set_of_stacks_solution.ipynb @@ -35,9 +35,15 @@ "\n", "* Can we assume we already have a stack class that can be used for this problem?\n", " * Yes\n", - "* If a stack becomes full, should we automatically create one?\n", + "* Are all stack bound by the same capacity?\n", + " * Yes\n", + "* If a stack becomes full, should automatically create one?\n", " * Yes\n", "* If a stack becomes empty, should we delete it?\n", + " * Yes\n", + "* If we pop on an empty stack, should we return None?\n", + " * Yes\n", + "* Can we assume this fits memory?\n", " * Yes" ] }, @@ -67,7 +73,7 @@ "\n", "Complexity:\n", "* Time: O(1)\n", - "* Space: O(m), where m is the size of the new stack\n", + "* Space: O(m), where m is the size of the new stack if the last stack is full\n", "\n", "### Pop\n", "\n", @@ -133,14 +139,14 @@ "\n", "class SetOfStacks(object):\n", "\n", - " def __init__(self, capacity):\n", - " self.capacity = capacity\n", + " def __init__(self, indiv_stack_capacity):\n", + " self.indiv_stack_capacity = indiv_stack_capacity\n", " self.stacks = []\n", " self.last_stack = None\n", "\n", " def push(self, data):\n", " if self.last_stack is None or self.last_stack.is_full():\n", - " self.last_stack = StackWithCapacity(None, self.capacity)\n", + " self.last_stack = StackWithCapacity(None, self.indiv_stack_capacity)\n", " self.stacks.append(self.last_stack)\n", " self.last_stack.push(data)\n", "\n", @@ -148,11 +154,10 @@ " if self.last_stack is None:\n", " return None\n", " data = self.last_stack.pop()\n", - " if self.last_stack.num_items == 0:\n", + " if not self.last_stack.num_items:\n", " self.stacks.pop()\n", - " self.last_stack = self.stacks[len(self.stacks)-1] \\\n", + " self.last_stack = self.stacks[-1] \\\n", " if len(self.stacks) else None\n", - " num_stacks = len(self.stacks)\n", " return data" ] }, @@ -188,7 +193,7 @@ "\n", " def test_set_of_stacks(self):\n", " print('Test: Push on an empty stack')\n", - " stacks = SetOfStacks(capacity=2)\n", + " stacks = SetOfStacks(indiv_stack_capacity=2)\n", " stacks.push(3)\n", "\n", " print('Test: Push on a non-empty stack')\n", @@ -261,7 +266,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, diff --git a/stacks_queues/set_of_stacks/test_set_of_stacks.py b/stacks_queues/set_of_stacks/test_set_of_stacks.py index 3313c08..e353126 100644 --- a/stacks_queues/set_of_stacks/test_set_of_stacks.py +++ b/stacks_queues/set_of_stacks/test_set_of_stacks.py @@ -5,7 +5,7 @@ class TestSetOfStacks(object): def test_set_of_stacks(self): print('Test: Push on an empty stack') - stacks = SetOfStacks(capacity=2) + stacks = SetOfStacks(indiv_stack_capacity=2) stacks.push(3) print('Test: Push on a non-empty stack')