mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 17:38:02 +00:00
Added stack sorting challenge.
This commit is contained in:
0
stacks_queues/sort_stack/__init__.py
Normal file
0
stacks_queues/sort_stack/__init__.py
Normal file
177
stacks_queues/sort_stack/sort_stack_challenge.ipynb
Normal file
177
stacks_queues/sort_stack/sort_stack_challenge.ipynb
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Problem: Sort a stack. You can use another stack as a buffer.\n",
|
||||||
|
"\n",
|
||||||
|
"* [Constraints](#Constraints)\n",
|
||||||
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
|
"* [Algorithm](#Algorithm)\n",
|
||||||
|
"* [Code](#Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Constraints\n",
|
||||||
|
"\n",
|
||||||
|
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||||
|
"\n",
|
||||||
|
"* When sorted, should the largest element be at the top or bottom?\n",
|
||||||
|
" * Top\n",
|
||||||
|
"* Can you have duplicate values like 5, 5?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||||
|
" * Yes"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Test Cases\n",
|
||||||
|
"\n",
|
||||||
|
"* Empty stack -> None\n",
|
||||||
|
"* One element stack\n",
|
||||||
|
"* Two or more element stack (general case)\n",
|
||||||
|
"* Already sorted stack"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Algorithm\n",
|
||||||
|
"\n",
|
||||||
|
"Refer to the solution notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Code"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run ../stack/stack.py\n",
|
||||||
|
"%load ../stack/stack.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class MyStack(Stack):\n",
|
||||||
|
" \n",
|
||||||
|
" def sort(self):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test\n",
|
||||||
|
"\n",
|
||||||
|
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*\n",
|
||||||
|
"\n",
|
||||||
|
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# %load test_sort_stack.py\n",
|
||||||
|
"from random import randint\n",
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class TestSortStack(object):\n",
|
||||||
|
" \n",
|
||||||
|
" def get_sorted_stack(self, numbers):\n",
|
||||||
|
" stack = MyStack()\n",
|
||||||
|
" for x in numbers:\n",
|
||||||
|
" stack.push(x)\n",
|
||||||
|
" sorted_stack = stack.sort()\n",
|
||||||
|
" return sorted_stack\n",
|
||||||
|
"\n",
|
||||||
|
" def test_sort_stack(self):\n",
|
||||||
|
" print('Test: Empty stack')\n",
|
||||||
|
" sorted_stack = self.get_sorted_stack([])\n",
|
||||||
|
" assert_equal(sorted_stack.pop(), None)\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: One element stack')\n",
|
||||||
|
" sorted_stack = self.get_sorted_stack([1])\n",
|
||||||
|
" assert_equal(sorted_stack.pop(), 1)\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: Two or more element stack (general case)')\n",
|
||||||
|
" num_items = 10\n",
|
||||||
|
" numbers = [randint(0, 10) for x in xrange(num_items)]\n",
|
||||||
|
" sorted_stack = self.get_sorted_stack(numbers)\n",
|
||||||
|
" sorted_numbers = []\n",
|
||||||
|
" for _ in xrange(num_items):\n",
|
||||||
|
" sorted_numbers.append(sorted_stack.pop())\n",
|
||||||
|
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
|
||||||
|
" \n",
|
||||||
|
" print('Success: test_sort_stack')\n",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" test = TestSortStack()\n",
|
||||||
|
" test.test_sort_stack()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" main()"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 2",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python2"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Empty stack\n",
|
"* Empty stack -> None\n",
|
||||||
"* One element stack\n",
|
"* One element stack\n",
|
||||||
"* Two or more element stack (general case)\n",
|
"* Two or more element stack (general case)\n",
|
||||||
"* Already sorted stack"
|
"* Already sorted stack"
|
||||||
@@ -82,7 +82,8 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run stack.py"
|
"%run ../stack/stack.py\n",
|
||||||
|
"%load ../stack/stack.py"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -94,6 +95,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class MyStack(Stack):\n",
|
"class MyStack(Stack):\n",
|
||||||
|
" \n",
|
||||||
" def sort(self):\n",
|
" def sort(self):\n",
|
||||||
" buff = MyStack()\n",
|
" buff = MyStack()\n",
|
||||||
" while not self.is_empty():\n",
|
" while not self.is_empty():\n",
|
||||||
@@ -124,18 +126,18 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Test: Empty stack\n",
|
"Overwriting test_sort_stack.py\n"
|
||||||
"Test: One element stack\n",
|
|
||||||
"Test: Two or more element stack (general case)\n",
|
|
||||||
"Success: test_sort_stack\n"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
|
"%%writefile test_sort_stack.py\n",
|
||||||
"from random import randint\n",
|
"from random import randint\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Test(object):\n",
|
"\n",
|
||||||
|
"class TestSortStack(object):\n",
|
||||||
|
" \n",
|
||||||
" def get_sorted_stack(self, numbers):\n",
|
" def get_sorted_stack(self, numbers):\n",
|
||||||
" stack = MyStack()\n",
|
" stack = MyStack()\n",
|
||||||
" for x in numbers:\n",
|
" for x in numbers:\n",
|
||||||
@@ -163,9 +165,34 @@
|
|||||||
" \n",
|
" \n",
|
||||||
" print('Success: test_sort_stack')\n",
|
" print('Success: test_sort_stack')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" test = TestSortStack()\n",
|
||||||
|
" test.test_sort_stack()\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" test = Test()\n",
|
" main()"
|
||||||
" test.test_sort_stack()"
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Test: Empty stack\n",
|
||||||
|
"Test: One element stack\n",
|
||||||
|
"Test: Two or more element stack (general case)\n",
|
||||||
|
"Success: test_sort_stack\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%run -i test_sort_stack.py"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
39
stacks_queues/sort_stack/test_sort_stack.py
Normal file
39
stacks_queues/sort_stack/test_sort_stack.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
from random import randint
|
||||||
|
from nose.tools import assert_equal
|
||||||
|
|
||||||
|
|
||||||
|
class TestSortStack(object):
|
||||||
|
|
||||||
|
def get_sorted_stack(self, numbers):
|
||||||
|
stack = MyStack()
|
||||||
|
for x in numbers:
|
||||||
|
stack.push(x)
|
||||||
|
sorted_stack = stack.sort()
|
||||||
|
return sorted_stack
|
||||||
|
|
||||||
|
def test_sort_stack(self):
|
||||||
|
print('Test: Empty stack')
|
||||||
|
sorted_stack = self.get_sorted_stack([])
|
||||||
|
assert_equal(sorted_stack.pop(), None)
|
||||||
|
|
||||||
|
print('Test: One element stack')
|
||||||
|
sorted_stack = self.get_sorted_stack([1])
|
||||||
|
assert_equal(sorted_stack.pop(), 1)
|
||||||
|
|
||||||
|
print('Test: Two or more element stack (general case)')
|
||||||
|
num_items = 10
|
||||||
|
numbers = [randint(0, 10) for x in xrange(num_items)]
|
||||||
|
sorted_stack = self.get_sorted_stack(numbers)
|
||||||
|
sorted_numbers = []
|
||||||
|
for _ in xrange(num_items):
|
||||||
|
sorted_numbers.append(sorted_stack.pop())
|
||||||
|
assert_equal(sorted_numbers, sorted(numbers, reverse=True))
|
||||||
|
|
||||||
|
print('Success: test_sort_stack')
|
||||||
|
|
||||||
|
def main():
|
||||||
|
test = TestSortStack()
|
||||||
|
test.test_sort_stack()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user