Add intermediate sort stack solution and test

This commit is contained in:
Donne Martin 2016-02-23 06:12:45 -05:00
parent d9b2d7144e
commit ba14cb0417
3 changed files with 67 additions and 33 deletions

View File

@ -123,26 +123,25 @@
"\n", "\n",
"class TestSortStack(object):\n", "class TestSortStack(object):\n",
"\n", "\n",
" def get_sorted_stack(self, numbers):\n", " def get_sorted_stack(self, stack, numbers):\n",
" stack = MyStack()\n",
" for x in numbers:\n", " for x in numbers:\n",
" stack.push(x)\n", " stack.push(x)\n",
" sorted_stack = stack.sort()\n", " sorted_stack = stack.sort()\n",
" return sorted_stack\n", " return sorted_stack\n",
"\n", "\n",
" def test_sort_stack(self):\n", " def test_sort_stack(self, stack):\n",
" print('Test: Empty stack')\n", " print('Test: Empty stack')\n",
" sorted_stack = self.get_sorted_stack([])\n", " sorted_stack = self.get_sorted_stack(stack, [])\n",
" assert_equal(sorted_stack.pop(), None)\n", " assert_equal(sorted_stack.pop(), None)\n",
"\n", "\n",
" print('Test: One element stack')\n", " print('Test: One element stack')\n",
" sorted_stack = self.get_sorted_stack([1])\n", " sorted_stack = self.get_sorted_stack(stack, [1])\n",
" assert_equal(sorted_stack.pop(), 1)\n", " assert_equal(sorted_stack.pop(), 1)\n",
"\n", "\n",
" print('Test: Two or more element stack (general case)')\n", " print('Test: Two or more element stack (general case)')\n",
" num_items = 10\n", " num_items = 10\n",
" numbers = [randint(0, 10) for x in range(num_items)]\n", " numbers = [randint(0, 10) for x in range(num_items)]\n",
" sorted_stack = self.get_sorted_stack(numbers)\n", " sorted_stack = self.get_sorted_stack(stack, numbers)\n",
" sorted_numbers = []\n", " sorted_numbers = []\n",
" for _ in range(num_items):\n", " for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\n", " sorted_numbers.append(sorted_stack.pop())\n",
@ -153,7 +152,8 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestSortStack()\n", " test = TestSortStack()\n",
" test.test_sort_stack()\n", " test.test_sort_stack(MyStack())\n",
" test.test_sort_stack(MyStackSimplified())\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@ -172,21 +172,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,

View File

@ -105,6 +105,36 @@
" buff = MyStack()\n", " buff = MyStack()\n",
" while not self.is_empty():\n", " while not self.is_empty():\n",
" temp = self.pop()\n", " temp = self.pop()\n",
" if buff.is_empty() or temp >= buff.peek():\n",
" buff.push(temp)\n",
" else:\n",
" while not buff.is_empty() and buff.peek() > temp:\n",
" self.push(buff.pop())\n",
" buff.push(temp)\n",
" return buff"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The solution can be further simplified:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class MyStackSimplified(Stack):\n",
"\n",
" def sort(self):\n",
" buff = MyStack()\n",
" while not self.is_empty():\n",
" temp = self.pop()\n",
" while not buff.is_empty() and buff.peek() > temp:\n", " while not buff.is_empty() and buff.peek() > temp:\n",
" self.push(buff.pop())\n", " self.push(buff.pop())\n",
" buff.push(temp)\n", " buff.push(temp)\n",
@ -121,7 +151,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -142,26 +172,25 @@
"\n", "\n",
"class TestSortStack(object):\n", "class TestSortStack(object):\n",
"\n", "\n",
" def get_sorted_stack(self, numbers):\n", " def get_sorted_stack(self, stack, numbers):\n",
" stack = MyStack()\n",
" for x in numbers:\n", " for x in numbers:\n",
" stack.push(x)\n", " stack.push(x)\n",
" sorted_stack = stack.sort()\n", " sorted_stack = stack.sort()\n",
" return sorted_stack\n", " return sorted_stack\n",
"\n", "\n",
" def test_sort_stack(self):\n", " def test_sort_stack(self, stack):\n",
" print('Test: Empty stack')\n", " print('Test: Empty stack')\n",
" sorted_stack = self.get_sorted_stack([])\n", " sorted_stack = self.get_sorted_stack(stack, [])\n",
" assert_equal(sorted_stack.pop(), None)\n", " assert_equal(sorted_stack.pop(), None)\n",
"\n", "\n",
" print('Test: One element stack')\n", " print('Test: One element stack')\n",
" sorted_stack = self.get_sorted_stack([1])\n", " sorted_stack = self.get_sorted_stack(stack, [1])\n",
" assert_equal(sorted_stack.pop(), 1)\n", " assert_equal(sorted_stack.pop(), 1)\n",
"\n", "\n",
" print('Test: Two or more element stack (general case)')\n", " print('Test: Two or more element stack (general case)')\n",
" num_items = 10\n", " num_items = 10\n",
" numbers = [randint(0, 10) for x in range(num_items)]\n", " numbers = [randint(0, 10) for x in range(num_items)]\n",
" sorted_stack = self.get_sorted_stack(numbers)\n", " sorted_stack = self.get_sorted_stack(stack, numbers)\n",
" sorted_numbers = []\n", " sorted_numbers = []\n",
" for _ in range(num_items):\n", " for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\n", " sorted_numbers.append(sorted_stack.pop())\n",
@ -172,7 +201,8 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestSortStack()\n", " test = TestSortStack()\n",
" test.test_sort_stack()\n", " test.test_sort_stack(MyStack())\n",
" test.test_sort_stack(MyStackSimplified())\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@ -181,7 +211,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -190,6 +220,10 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Test: Empty stack\n",
"Test: One element stack\n",
"Test: Two or more element stack (general case)\n",
"Success: test_sort_stack\n",
"Test: Empty stack\n", "Test: Empty stack\n",
"Test: One element stack\n", "Test: One element stack\n",
"Test: Two or more element stack (general case)\n", "Test: Two or more element stack (general case)\n",
@ -204,21 +238,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,

View File

@ -4,26 +4,25 @@ from nose.tools import assert_equal
class TestSortStack(object): class TestSortStack(object):
def get_sorted_stack(self, numbers): def get_sorted_stack(self, stack, numbers):
stack = MyStack()
for x in numbers: for x in numbers:
stack.push(x) stack.push(x)
sorted_stack = stack.sort() sorted_stack = stack.sort()
return sorted_stack return sorted_stack
def test_sort_stack(self): def test_sort_stack(self, stack):
print('Test: Empty stack') print('Test: Empty stack')
sorted_stack = self.get_sorted_stack([]) sorted_stack = self.get_sorted_stack(stack, [])
assert_equal(sorted_stack.pop(), None) assert_equal(sorted_stack.pop(), None)
print('Test: One element stack') print('Test: One element stack')
sorted_stack = self.get_sorted_stack([1]) sorted_stack = self.get_sorted_stack(stack, [1])
assert_equal(sorted_stack.pop(), 1) assert_equal(sorted_stack.pop(), 1)
print('Test: Two or more element stack (general case)') print('Test: Two or more element stack (general case)')
num_items = 10 num_items = 10
numbers = [randint(0, 10) for x in range(num_items)] numbers = [randint(0, 10) for x in range(num_items)]
sorted_stack = self.get_sorted_stack(numbers) sorted_stack = self.get_sorted_stack(stack, numbers)
sorted_numbers = [] sorted_numbers = []
for _ in range(num_items): for _ in range(num_items):
sorted_numbers.append(sorted_stack.pop()) sorted_numbers.append(sorted_stack.pop())
@ -34,7 +33,8 @@ class TestSortStack(object):
def main(): def main():
test = TestSortStack() test = TestSortStack()
test.test_sort_stack() test.test_sort_stack(MyStack())
test.test_sort_stack(MyStackSimplified())
if __name__ == '__main__': if __name__ == '__main__':