Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mudassra-taskeen committed Jan 12, 2025
1 parent 2c4203f commit 04214fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 59 deletions.
21 changes: 6 additions & 15 deletions solutions/find_single_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
This module provides a function to find the single number in a list where all other numbers appear twice.
"""


def find_single_number(numbers: list[int]) -> int:
"""
Returns the number that appears only once in the list, where all other numbers appear twice.
Expand All @@ -19,8 +18,7 @@ def find_single_number(numbers: list[int]) -> int:
int: The single number that appears only once.
Raises:
ValueError: If the input list is empty.
TypeError: If the input is not a list or contains non-integer elements.
AssertionError: If the input list is empty or if input is not a list or contains non-integer elements.
Assumptions:
- The input is always a list of integers.
Expand All @@ -44,25 +42,18 @@ def find_single_number(numbers: list[int]) -> int:
- Ensures the input is a list of integers.
"""
# Check for valid input type
if not isinstance(numbers, list):
raise TypeError("Input must be a list")
assert isinstance(numbers, list), "Input must be a list"

# Ensure list is not empty
if len(numbers) == 0:
raise ValueError("List cannot be empty")
assert len(numbers) > 0, "List cannot be empty"

# Ensure all elements are integers
if not all(isinstance(current_number, int) for current_number in numbers):
raise TypeError("Each element must be an integer")
assert all(isinstance(current_number, int) for current_number in numbers), "Each element must be an integer"

# Logic to find the single number
single_number = 0

for current_number in numbers:
single_number ^= current_number # XOR operation, all duplicate numbers cancel out

single_number ^= (

current_number # XOR operation, all duplicate numbers cancel out
)

return single_number
return single_number
53 changes: 9 additions & 44 deletions solutions/tests/test_find_single_number.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Tests for the 'find_single_number' module.
Test Categories:
- Standard Cases: Regular lists with varying numbers of elements.
- Edge Cases: Boundary conditions like empty lists and lists with mixed types.
- Special Cases: Lists with floating-point numbers and special characters.
- Defensive Cases: Ensuring proper handling of invalid inputs.
Author: Mudassra Taskeen
Created on: 2025-01-07
"""
Expand All @@ -18,72 +10,45 @@


class TestFindSingleNumber(unittest.TestCase):
"""
Unit tests for the `find_single_number` function.
"""
"""Unit tests for the find_single_number function."""

# Standard Cases
def test_single_element(self):
"""
Test the case when there is only one element in the list.
"""
self.assertEqual(find_single_number([1]), 1)

def test_positive_and_negative_integers(self):
"""
It should correctly identify the single number with both positive and negative integers.
"""
self.assertEqual(find_single_number([-1, 2, -1, 3, 2]), 3)

def test_includes_zero(self):
"""
It should correctly identify the single number in a list that includes zero.
"""
self.assertEqual(find_single_number([0, 1, 0, -2, -2]), 1)

def test_all_negative_numbers(self):
"""
It should correctly identify the single number when all elements are negative.
"""
self.assertEqual(find_single_number([-3, -1, -3, -2, -2]), -1)

# Edge Cases
def test_empty_list(self):
"""It should raise an error or handle the case gracefully if the input list is empty."""
with self.assertRaises(ValueError):
with self.assertRaises(AssertionError):
find_single_number([])


def test_single_number_with_floats(self):
"""
It should raise an error for lists containing floating-point numbers.
"""
with self.assertRaises(TypeError):
find_single_number([1.5, 2, 2, 1.5, 3])

# Special Cases
def test_single_number_with_floats(self):
with self.assertRaises(AssertionError):
find_single_number([1.5, 2, 2, 1.5, 3])

def test_invalid_input(self):
"""
Test the case when input is not a list type.
"""
with self.assertRaises(AssertionError):
find_single_number("not a list")

def test_defensive_non_integer_elements(self):
"""
Test the case when input list contains non-integer elements.
"""
with self.assertRaises(AssertionError):
find_single_number([1, "2", 3])

# Performance/Boundary Cases
def test_boundary_large_list(self):
"""
Test boundary case for a large list.
"""
nums = [i for i in range(1, 1000)] * 2 + [1001]
self.assertEqual(find_single_number(nums), 1001)


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

unittest.main()

0 comments on commit 04214fb

Please sign in to comment.