Skip to content

Commit

Permalink
Merge pull request #96 from MIT-Emerging-Talent/move_zeroes
Browse files Browse the repository at this point in the history
Move zeroes (Resolves #68)
  • Loading branch information
jola-ds authored Jan 13, 2025
2 parents 768b1cf + d9ee992 commit 4e2a68a
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
59 changes: 59 additions & 0 deletions solutions/move_zeroes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
A module provides a function to move all zeroes in an array to the end
while maintaining the relative order of the non-zero elements.
Module contents:
move_zeroes(nums): Rearranges the given list in-place as per the described behavior.
Author: Mudassra Taskeen
Date: 2025-01-08
"""

from typing import List


def move_zeroes(nums: List[int]) -> None:
"""
Move all 0's to the end of the input list in-place while maintaining
the relative sequence of non-zero elements.
Arguments:
nums (List[int]): The input list of integers.
Returns:
None: The function modifies the input list in-place.
Raises:
AssertionError: If nums is not a list of integers.
Examples:
>>> nums = [0, 1, 0, 3, 12]
>>> move_zeroes(nums)
>>> nums
[1, 3, 12, 0, 0]
>>> nums = [0]
>>> move_zeroes(nums)
>>> nums
[0]from typing import list
>>> nums = [4, 0, 5, 0, 6]
>>> move_zeroes(nums)
>>> print(nums)
[4, 5, 6, 0, 0]
"""

# Defensive assertions
assert isinstance(nums, list), "Input must be of type List"
assert all(isinstance(x, int) for x in nums), (
"All elements in the list must be integers"
)
# Implementation: Two-pointer approach

non_zero_index = 0

for i, num in enumerate(nums):
if num != 0:
nums[non_zero_index], nums[i] = nums[i], nums[non_zero_index]

non_zero_index += 1
96 changes: 96 additions & 0 deletions solutions/tests/test_move_zeroes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
Unit tests for the moveZeroes function.
Test Categories:
- Standard Cases: Various input arrays with different combinations of zeros and non-zeros
- Edge Cases: Arrays with only zeros, only non-zeros, or an empty array
- Defensive Cases: Invalid input handling
The tests verify that the function:
- Correctly moves zeros to the end
- Maintains the relative order of non-zero elements
- Handles edge cases like empty arrays and arrays with only zeros
- Validates input types and conditions
Date: 2025-01-07
Author: Mudassra Taskeen
"""

import unittest

from ..move_zeroes import move_zeroes


class TestMoveZeroes(unittest.TestCase):
"""Test cases for move_zeroes function."""

def test_standard_case(self):
"""It should move all zeros to the end while maintaining order."""
nums = [0, 1, 0, 3, 12]
move_zeroes(nums)
self.assertEqual(nums, [1, 3, 12, 0, 0])

def test_single_zero(self):
"""
It should handle a single zero correctly.
"""
nums = [0]
move_zeroes(nums)
self.assertEqual(nums, [0])

def test_single_non_zero(self):
"""
It should handle a single non-zero element correctly.
"""
nums = [5]
move_zeroes(nums)
self.assertEqual(nums, [5])

def test_all_zeros(self):
"""
It should handle arrays with all zeros.
"""
nums = [0, 0, 0, 0]
move_zeroes(nums)
self.assertEqual(nums, [0, 0, 0, 0])

def test_all_non_zeros(self):
"""
It should leave arrays with no zeros unchanged.
"""
nums = [1, 2, 3, 4]
move_zeroes(nums)
self.assertEqual(nums, [1, 2, 3, 4])

def test_empty_array(self):
"""
It should handle an empty array.
"""
nums = []
move_zeroes(nums)
self.assertEqual(nums, [])

def test_edge_case_with_large_numbers(self):
"""
It should handle large numbers correctly.
"""
nums = [1000000, 0, 999999, 0, 1]
move_zeroes(nums)
self.assertEqual(nums, [1000000, 999999, 1, 0, 0])

def test_non_list_input(self):
"""
It should raise AssertionError for non-list input.
"""
with self.assertRaises(AssertionError):
move_zeroes(123)

def test_non_integer_elements(self):
"""
It should raise AssertionError for non-integer elements in the list.
"""
with self.assertRaises(AssertionError):
move_zeroes([1, "a", 0])


if __name__ == "__main__":
unittest.main()

0 comments on commit 4e2a68a

Please sign in to comment.