#!/usr/bin/env python # coding: utf-8 # This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). # # Challenge Notebook # ## Problem: Implement selection sort. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * Is a naive solution sufficient (ie not stable, not based on a heap)? # * Yes # * Are duplicates allowed? # * Yes # * Can we assume the input is valid? # * No # * Can we assume this fits memory? # * Yes # ## Test Cases # # * None -> Exception # * Empty input -> [] # * One element -> [element] # * Two or more elements # ## Algorithm # # Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/sorting_searching/selection_sort/selection_sort_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start. # ## Code # In[ ]: class SelectionSort(object): def sort(self, data): # TODO: Implement me (recursive) pass # ## Unit Test # # # # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_selection_sort.py import unittest class TestSelectionSort(unittest.TestCase): def test_selection_sort(self, func): print('None input') self.assertRaises(TypeError, func, None) print('Empty input') self.assertEqual(func([]), []) print('One element') self.assertEqual(func([5]), [5]) print('Two or more elements') data = [5, 1, 7, 2, 6, -3, 5, 7, -10] self.assertEqual(func(data), sorted(data)) print('Success: test_selection_sort\n') def main(): test = TestSelectionSort() selection_sort = SelectionSort() test.test_selection_sort(selection_sort.sort) try: test.test_selection_sort(selection_sort.sort_recursive) test.test_selection_sort(selection_sort.sor_iterative_alt) except NameError: # Alternate solutions are only defined # in the solutions file pass if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/sorting_searching/selection_sort/selection_sort_solution.ipynb) for a discussion on algorithms and code solutions.