<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: Move all zeroes in a list to the end.* [Constraints](Constraints)* [Test Cases](Test-Cases)* [Algorithm](Algorithm)* [Code](Code)* [Unit Test](Unit-Test)  Constraints* Is the input an array of ints?    * Yes* Is the output a new array of ints?    * No, do this in-place* Do we need to maintain ordering of non-zero values?    * Yes* Can we assume the inputs are valid?    * No* Can we assume this fits memory?    * Yes  Test Cases* None -> TypeError* [0, 1, 0, 3, 12] -> [1, 3, 12, 0, 0]* [1, 0] -> [1, 0]* [0, 1] -> [1, 0]* [0] -> [0]* [1] -> [1]* [1, 1] -> [1, 1]  Algorithm* pos = 0* Loop through each item in the input    * If the item != 0, set input[pos] = item        * pos++* Fill input[pos:] with zeroes |[0, 1, 0, 3, 12] ^    |[0, 1, 0, 3, 12] ^    |[1, 1, 0, 3, 12] ^       |[1, 1, 0, 3, 12]    ^          |[1, 1, 0, 3, 12]    ^          |[1, 3, 0, 3, 12]    ^              |[1, 3, 0, 3, 12]       ^              |[1, 3, 12, 3, 12]       ^Fill right with zeroes:[1, 3, 12, 3, 12]           ^[1, 3, 12, 0, 12]           ^[1, 3, 12, 0, 0]              ^Complexity:* Time: O(n)* Space: O(1)  Code<jupyter_code>class Solution(object):

    def move_zeroes(self, nums):
        if nums is None:
            raise TypeError('nums cannot be None')
        pos = 0
        for num in nums:
            if num != 0:
                nums[pos] = num
                pos += 1
        if pos < len(nums):
            nums[pos:] = [0] * (len(nums) - pos)<jupyter_output><empty_output><jupyter_text>Unit Test<jupyter_code>%%writefile test_move_zeroes.py
import unittest


class TestMoveZeroes(unittest.TestCase):

    def test_move_zeroes(self):
        solution = Solution()
        self.assertRaises(TypeError, solution.move_zeroes, None)
        array = [0, 1, 0, 3, 12]
        solution.move_zeroes(array)
        self.assertEqual(array, [1, 3, 12, 0, 0])
        array = [1, 0]
        solution.move_zeroes(array)
        self.assertEqual(array, [1, 0])
        array = [0, 1]
        solution.move_zeroes(array)
        self.assertEqual(array, [1, 0])
        array = [0]
        solution.move_zeroes(array)
        self.assertEqual(array, [0])
        array = [1]
        solution.move_zeroes(array)
        self.assertEqual(array, [1])
        array = [1, 1]
        solution.move_zeroes(array)
        self.assertEqual(array, [1, 1])
        print('Success: test_move_zeroes')


def main():
    test = TestMoveZeroes()
    test.test_move_zeroes()


if __name__ == '__main__':
    main()
%run -i test_move_zeroes.py<jupyter_output>Success: test_move_zeroes
{'fraction_non_alphanumeric': 0.15293696275071633, 'fraction_numerical': 0.04727793696275072, 'mean_word_length': 2.397810218978102, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, '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': '107013', 'license': 'Apache-2.0', 'n_tokens_mistral': 1093, 'n_tokens_neox': 974, 'n_words': 300, 'path': 'online_judges/move_zeroes/move_zeroes_solution.ipynb', 'repo_name': 'benkeesey/interactive-coding-challenges', 'size': '2798'}