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