forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
search insert position problem function and test files
- Loading branch information
Omnia Nasr
committed
Dec 31, 2024
1 parent
579f75e
commit 69c9070
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This module provides the `search_insert_position` function. | ||
Functionality: | ||
- Determines the index of a target value in a sorted list of integers. | ||
- Returns the position where the target would be inserted if not found. | ||
Created on 2024-12-31 | ||
Author: Omnia | ||
""" | ||
|
||
|
||
def search_insert_position(numbers: list[int], target: int) -> int: | ||
""" | ||
Returns the index of the target in a sorted list, or the position where it should be inserted. | ||
Parameters: | ||
numbers (list[int]): A list of distinct integers sorted in ascending order. | ||
target (int): The integer to search for. | ||
Returns: | ||
int: The index or insertion position. | ||
Raises: | ||
AssertionError: If inputs are of incorrect type or format. | ||
Examples: | ||
>>> search_insert_position([1, 3, 5, 6], 5) | ||
2 | ||
>>> search_insert_position([1, 3, 5, 6], 2) | ||
1 | ||
>>> search_insert_position([1, 3, 5, 6], 7) | ||
4 | ||
>>> search_insert_position([], 5) | ||
0 | ||
""" | ||
# Assertions to validate input types | ||
assert isinstance(numbers, list), "First input must be a list" | ||
assert isinstance(target, int), "Second input must be an integer" | ||
assert all( | ||
isinstance(x, int) for x in numbers | ||
), "Numbers list must only contain integers" | ||
assert numbers == sorted( | ||
numbers | ||
), "The input list must be sorted in ascending order" | ||
|
||
for index in range(len(numbers)): | ||
# Check if the current number is greater than or equal to the target | ||
# If so, return the current index as the insertion point | ||
if numbers[index] >= target: | ||
return index | ||
|
||
# If no number in the list is greater than or equal to the target, | ||
# the target should be inserted at the end of the list. | ||
return len(numbers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for the search_insert_position function | ||
Created on 2024-12-31 | ||
Author: Omnia | ||
""" | ||
|
||
import unittest | ||
from ..search_insert_position import search_insert_position | ||
|
||
|
||
class TestIsIn(unittest.TestCase): | ||
"""Test Suite for the search_insert_position function""" | ||
|
||
def test_minimal_input(self): | ||
"""Test when numbers is an empty list""" | ||
self.assertEqual(search_insert_position([], 0), 0) | ||
|
||
def test_target_found(self): | ||
"""Test when target is found in the numbers list""" | ||
self.assertEqual(search_insert_position([1, 3, 5, 6], 1), 0) | ||
|
||
def test_target_not_found(self): | ||
"""Test when target is not found in the numbers list""" | ||
self.assertEqual(search_insert_position([1, 3, 5, 6], 7), 4) | ||
|
||
def test_one_element(self): | ||
"""Test when the numbers list contain one element""" | ||
self.assertEqual(search_insert_position([-1], 1), 1) | ||
|
||
def test_negative_numbers(self): | ||
"""Test with a list containing negative numbers""" | ||
self.assertEqual(search_insert_position([-10, -5, 0, 5], -6), 1) | ||
|
||
def test_large_list(self): | ||
"""Test with a large list""" | ||
self.assertEqual(search_insert_position(list(range(1, 10001)), 10001), 10000) | ||
|
||
# defensive tests | ||
def test_invalid_numbers_input(self): | ||
"""should raise assertion error because first parameter is not a list of only integers""" | ||
with self.assertRaises(AssertionError): | ||
search_insert_position(["3", 4, 6], 5) | ||
|
||
def test_invalid_target_input(self): | ||
"""should raise assertion error because second parameter is not an integer""" | ||
with self.assertRaises(AssertionError): | ||
search_insert_position([1, 2, 3], "x") | ||
|
||
def test_unsorted_list(self): | ||
"""Test defensive assertion for an unsorted list.""" | ||
with self.assertRaises(AssertionError): | ||
search_insert_position([3, 1, 4, 2], 2) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |