#!/usr/bin/env python # coding: utf-8 # This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). # # Challenge Notebook # ## Problem: Implement radix sort. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * Is the input a list? # * Yes # * Can we assume the inputs are valid? # * Check for None in place of an array # * Assume array elements are ints # * Do we know the max digits to handle? # * No # * Are the digits base 10? # * Yes # * Can we assume this fits memory? # * Yes # ## Test Cases # # * None -> Exception # * [] -> [] # * [128, 256, 164, 8, 2, 148, 212, 242, 244] -> [2, 8, 128, 148, 164, 212, 242, 244, 256] # ## Algorithm # # Refer to the [Solution Notebook](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/sorting_searching/radix_sort/radix_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 RadixSort(object): def sort(self, array, base=10): # TODO: Implement me pass # ## Unit Test # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_radix_sort.py import unittest class TestRadixSort(unittest.TestCase): def test_sort(self): radix_sort = RadixSort() self.assertRaises(TypeError, radix_sort.sort, None) self.assertEqual(radix_sort.sort([]), []) array = [128, 256, 164, 8, 2, 148, 212, 242, 244] expected = [2, 8, 128, 148, 164, 212, 242, 244, 256] self.assertEqual(radix_sort.sort(array), expected) print('Success: test_sort') def main(): test = TestRadixSort() test.test_sort() if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook]() for a discussion on algorithms and code solutions.