Polish set of stacks challenge and solution (#73)

Update constraints, algorithm discussion, and code.
This commit is contained in:
Donne Martin 2016-06-21 05:30:14 -04:00 committed by GitHub
parent e71b280082
commit 7f338b0e64
3 changed files with 25 additions and 14 deletions

View File

@ -36,9 +36,15 @@
"\n", "\n",
"* Can we assume we already have a stack class that can be used for this problem?\n", "* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes\n", " * Yes\n",
"* Are all stack bound by the same capacity?\n",
" * Yes\n",
"* If a stack becomes full, should automatically create one?\n", "* If a stack becomes full, should automatically create one?\n",
" * Yes\n", " * Yes\n",
"* If a stack becomes empty, should we delete it?\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" " * Yes"
] ]
}, },
@ -111,7 +117,7 @@
"\n", "\n",
"class SetOfStacks(object):\n", "class SetOfStacks(object):\n",
"\n", "\n",
" def __init__(self, capacity):\n", " def __init__(self, indiv_stack_capacity):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass\n", " pass\n",
"\n", "\n",
@ -151,7 +157,7 @@
"\n", "\n",
" def test_set_of_stacks(self):\n", " def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\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", " stacks.push(3)\n",
"\n", "\n",
" print('Test: Push on a non-empty stack')\n", " print('Test: Push on a non-empty stack')\n",
@ -208,7 +214,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -35,9 +35,15 @@
"\n", "\n",
"* Can we assume we already have a stack class that can be used for this problem?\n", "* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes\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", " * Yes\n",
"* If a stack becomes empty, should we delete it?\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" " * Yes"
] ]
}, },
@ -67,7 +73,7 @@
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(1)\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", "\n",
"### Pop\n", "### Pop\n",
"\n", "\n",
@ -133,14 +139,14 @@
"\n", "\n",
"class SetOfStacks(object):\n", "class SetOfStacks(object):\n",
"\n", "\n",
" def __init__(self, capacity):\n", " def __init__(self, indiv_stack_capacity):\n",
" self.capacity = capacity\n", " self.indiv_stack_capacity = indiv_stack_capacity\n",
" self.stacks = []\n", " self.stacks = []\n",
" self.last_stack = None\n", " self.last_stack = None\n",
"\n", "\n",
" def push(self, data):\n", " def push(self, data):\n",
" if self.last_stack is None or self.last_stack.is_full():\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.stacks.append(self.last_stack)\n",
" self.last_stack.push(data)\n", " self.last_stack.push(data)\n",
"\n", "\n",
@ -148,11 +154,10 @@
" if self.last_stack is None:\n", " if self.last_stack is None:\n",
" return None\n", " return None\n",
" data = self.last_stack.pop()\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.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", " if len(self.stacks) else None\n",
" num_stacks = len(self.stacks)\n",
" return data" " return data"
] ]
}, },
@ -188,7 +193,7 @@
"\n", "\n",
" def test_set_of_stacks(self):\n", " def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\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", " stacks.push(3)\n",
"\n", "\n",
" print('Test: Push on a non-empty stack')\n", " print('Test: Push on a non-empty stack')\n",
@ -261,7 +266,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -5,7 +5,7 @@ class TestSetOfStacks(object):
def test_set_of_stacks(self): def test_set_of_stacks(self):
print('Test: Push on an empty stack') print('Test: Push on an empty stack')
stacks = SetOfStacks(capacity=2) stacks = SetOfStacks(indiv_stack_capacity=2)
stacks.push(3) stacks.push(3)
print('Test: Push on a non-empty stack') print('Test: Push on a non-empty stack')