mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-03 07:58:02 +00:00
Polish set of stacks solution
This commit is contained in:
@@ -35,7 +35,7 @@
|
||||
"\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, we should automatically create one?\n",
|
||||
"* If a stack becomes full, should we automatically create one?\n",
|
||||
" * Yes\n",
|
||||
"* If a stack becomes empty, should we delete it?\n",
|
||||
" * Yes"
|
||||
@@ -50,7 +50,7 @@
|
||||
"* Push and pop on an empty stack\n",
|
||||
"* Push and pop on a non-empty stack\n",
|
||||
"* Push on a capacity stack to create a new one\n",
|
||||
"* Pop on a one element stack to destroy it"
|
||||
"* Pop on a stack to destroy it"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -67,7 +67,7 @@
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)\n",
|
||||
"* Space: O(m), where m is the size of the new stack\n",
|
||||
"\n",
|
||||
"### Pop\n",
|
||||
"\n",
|
||||
@@ -113,21 +113,19 @@
|
||||
"class StackWithCapacity(Stack):\n",
|
||||
"\n",
|
||||
" def __init__(self, top=None, capacity=10):\n",
|
||||
" super(StackWithCapacity, self).__init__(top)\n",
|
||||
" self.capacity = capacity\n",
|
||||
" self.num_items = 0\n",
|
||||
" super(StackWithCapacity, self).__init__(top)\n",
|
||||
"\n",
|
||||
" def push(self, data):\n",
|
||||
" if self.num_items < self.capacity:\n",
|
||||
" super(StackWithCapacity, self).push(data)\n",
|
||||
" self.num_items += 1\n",
|
||||
" else:\n",
|
||||
" if self.is_full():\n",
|
||||
" raise Exception('Stack full')\n",
|
||||
" super(StackWithCapacity, self).push(data)\n",
|
||||
" self.num_items += 1\n",
|
||||
"\n",
|
||||
" def pop(self):\n",
|
||||
" data = super(StackWithCapacity, self).pop()\n",
|
||||
" self.num_items -= 1\n",
|
||||
" return data\n",
|
||||
" return super(StackWithCapacity, self).pop()\n",
|
||||
"\n",
|
||||
" def is_full(self):\n",
|
||||
" return self.num_items == self.capacity\n",
|
||||
@@ -148,17 +146,13 @@
|
||||
"\n",
|
||||
" def pop(self):\n",
|
||||
" if self.last_stack is None:\n",
|
||||
" return\n",
|
||||
" elif self.last_stack.top.next is None:\n",
|
||||
" data = self.last_stack.pop()\n",
|
||||
" return None\n",
|
||||
" data = self.last_stack.pop()\n",
|
||||
" if self.last_stack.num_items == 0:\n",
|
||||
" self.stacks.pop()\n",
|
||||
" self.last_stack = self.stacks[len(self.stacks)-1] \\\n",
|
||||
" if len(self.stacks) else None\n",
|
||||
" num_stacks = len(self.stacks)\n",
|
||||
" if num_stacks == 0:\n",
|
||||
" self.last_stack = None\n",
|
||||
" else:\n",
|
||||
" self.last_stack = self.stacks[num_stacks-1]\n",
|
||||
" else:\n",
|
||||
" data = self.last_stack.pop()\n",
|
||||
" return data"
|
||||
]
|
||||
},
|
||||
@@ -194,8 +188,7 @@
|
||||
"\n",
|
||||
" def test_set_of_stacks(self):\n",
|
||||
" print('Test: Push on an empty stack')\n",
|
||||
" capacity = 2\n",
|
||||
" stacks = SetOfStacks(capacity)\n",
|
||||
" stacks = SetOfStacks(capacity=2)\n",
|
||||
" stacks.push(3)\n",
|
||||
"\n",
|
||||
" print('Test: Push on a non-empty stack')\n",
|
||||
@@ -204,7 +197,7 @@
|
||||
" print('Test: Push on a capacity stack to create a new one')\n",
|
||||
" stacks.push('a')\n",
|
||||
"\n",
|
||||
" print('Test: Pop on a one element stack to destroy it')\n",
|
||||
" print('Test: Pop on a stack to destroy it')\n",
|
||||
" assert_equal(stacks.pop(), 'a')\n",
|
||||
"\n",
|
||||
" print('Test: Pop general case')\n",
|
||||
@@ -240,7 +233,7 @@
|
||||
"Test: Push on an empty stack\n",
|
||||
"Test: Push on a non-empty stack\n",
|
||||
"Test: Push on a capacity stack to create a new one\n",
|
||||
"Test: Pop on a one element stack to destroy it\n",
|
||||
"Test: Pop on a stack to destroy it\n",
|
||||
"Test: Pop general case\n",
|
||||
"Test: Pop on no elements\n",
|
||||
"Success: test_set_of_stacks\n"
|
||||
@@ -254,21 +247,21 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -5,8 +5,7 @@ class TestSetOfStacks(object):
|
||||
|
||||
def test_set_of_stacks(self):
|
||||
print('Test: Push on an empty stack')
|
||||
capacity = 2
|
||||
stacks = SetOfStacks(capacity)
|
||||
stacks = SetOfStacks(capacity=2)
|
||||
stacks.push(3)
|
||||
|
||||
print('Test: Push on a non-empty stack')
|
||||
@@ -15,7 +14,7 @@ class TestSetOfStacks(object):
|
||||
print('Test: Push on a capacity stack to create a new one')
|
||||
stacks.push('a')
|
||||
|
||||
print('Test: Pop on a one element stack to destroy it')
|
||||
print('Test: Pop on a stack to destroy it')
|
||||
assert_equal(stacks.pop(), 'a')
|
||||
|
||||
print('Test: Pop general case')
|
||||
|
||||
Reference in New Issue
Block a user