This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).
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.
In [ ]:
%run ../stack/stack.py
%load ../stack/stack.py
In [ ]:
class MyStack(Stack):
def sort(self):
# TODO: Implement me
pass
In [ ]:
# %load test_sort_stack.py
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 range(num_items)]
sorted_stack = self.get_sorted_stack(numbers)
sorted_numbers = []
for _ in range(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()
Review the Solution Notebook for a discussion on algorithms and code solutions.