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",
"* 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,

View File

@ -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,

View File

@ -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')