<jupyter_start><jupyter_text>This notebook was prepared by [<NAME>](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).  Solution Notebook  Problem: Implement radix sort.* [Constraints](Constraints)* [Test Cases](Test-Cases)* [Algorithm](Algorithm)* [Code](Code)* [Unit Test](Unit-Test)  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]  Code<jupyter_code>class RadixSort(object):

    def sort(self, array, base=10):
        if array is None:
            raise TypeError('array cannot be None')
        if not array:
            return []
        max_element = max(array)
        max_digits = len(str(abs(max_element)))
        curr_array = array
        for digit in range(max_digits):
            buckets = [[] for _ in range(base)]
            for item in curr_array:
                buckets[(item//(base**digit))%base].append(item)
            curr_array = []
            for bucket in buckets:
                curr_array.extend(bucket)
        return curr_array<jupyter_output><empty_output><jupyter_text>Unit Test<jupyter_code>%%writefile test_radix_sort.py
from nose.tools import assert_equal, assert_raises


class TestRadixSort(object):

    def test_sort(self):
        radix_sort = RadixSort()
        assert_raises(TypeError, radix_sort.sort, None)
        assert_equal(radix_sort.sort([]), [])
        array = [128, 256, 164, 8, 2, 148, 212, 242, 244]
        expected = [2, 8, 128, 148, 164, 212, 242, 244, 256]
        assert_equal(radix_sort.sort(array), expected)
        print('Success: test_sort')


def main():
    test = TestRadixSort()
    test.test_sort()


if __name__ == '__main__':
    main()
%run -i test_radix_sort.py<jupyter_output>Success: test_sort
{'fraction_non_alphanumeric': 0.12299717247879359, 'fraction_numerical': 0.04524033930254477, 'mean_word_length': 3.074856046065259, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '432249', 'license': 'Apache-2.0', 'n_tokens_mistral': 775, 'n_tokens_neox': 667, 'n_words': 198, 'path': 'sorting_searching/radix_sort/radix_sort_solution.ipynb', 'repo_name': 'hanbf/interactive-coding-challenges', 'size': '2128'}