diff --git a/src/array_flattener.py b/src/array_flattener.py new file mode 100644 index 00000000..679031d6 --- /dev/null +++ b/src/array_flattener.py @@ -0,0 +1,37 @@ +def flatten_array(arr): + """ + Flatten a nested array of arbitrary depth into a single-level array. + + Args: + arr (list): A nested list that may contain sublists. + + Returns: + list: A flattened version of the input list. + + Raises: + TypeError: If the input is not a list or contains non-list/non-numeric elements. + """ + # Validate input is a list + if not isinstance(arr, list): + raise TypeError("Input must be a list") + + # Result to store flattened array + flattened = [] + + # Recursive helper function to flatten the array + def _recursive_flatten(element): + # If element is a list, recursively flatten it + if isinstance(element, list): + for sub_element in element: + _recursive_flatten(sub_element) + # If element is a number (int or float), append to flattened list + elif isinstance(element, (int, float)): + flattened.append(element) + # Raise error for non-numeric, non-list elements + else: + raise TypeError(f"Invalid element type: {type(element)}") + + # Start the recursive flattening + _recursive_flatten(arr) + + return flattened \ No newline at end of file diff --git a/src/binary_search.py b/src/binary_search.py new file mode 100644 index 00000000..436347a0 --- /dev/null +++ b/src/binary_search.py @@ -0,0 +1,45 @@ +def binary_search(arr, target): + """ + Perform binary search on a sorted array to find the target element. + + Args: + arr (list): A sorted list of comparable elements + target: The element to search for + + Returns: + int: Index of the target element if found, otherwise -1 + + Raises: + TypeError: If input is not a list + ValueError: If the list is not sorted + """ + # Check input type + if not isinstance(arr, list): + raise TypeError("Input must be a list") + + # Check if list is sorted + if arr != sorted(arr): + raise ValueError("Input list must be sorted in ascending order") + + # Handle empty list + if not arr: + return -1 + + # Perform binary search + left, right = 0, len(arr) - 1 + + while left <= right: + mid = (left + right) // 2 + + # Check if target is found + if arr[mid] == target: + return mid + + # Adjust search boundaries + if arr[mid] < target: + left = mid + 1 + else: + right = mid - 1 + + # Target not found + return -1 \ No newline at end of file diff --git a/src/rgb_to_hex.py b/src/rgb_to_hex.py new file mode 100644 index 00000000..658486f7 --- /dev/null +++ b/src/rgb_to_hex.py @@ -0,0 +1,26 @@ +def rgb_to_hex(r, g, b): + """ + Convert RGB color values to a hexadecimal color code. + + Args: + r (int): Red color value (0-255) + g (int): Green color value (0-255) + b (int): Blue color value (0-255) + + Returns: + str: Hexadecimal color code (e.g., '#FF0000' for red) + + Raises: + ValueError: If any color value is not in the range 0-255 + """ + # Validate input ranges + for color, name in [(r, 'Red'), (g, 'Green'), (b, 'Blue')]: + if not isinstance(color, int): + raise TypeError(f"{name} value must be an integer") + if color < 0 or color > 255: + raise ValueError(f"{name} value must be between 0 and 255") + + # Convert to hex, removing '0x' prefix and zero-padding to 2 digits + hex_color = '#{:02X}{:02X}{:02X}'.format(r, g, b) + + return hex_color \ No newline at end of file diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 00000000..10cb6f00 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,31 @@ +def reverse_string(s): + """ + Reverse the given string using a manual approach. + + Args: + s (str): The input string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If the input is not a string. + """ + # Check if input is a string + if not isinstance(s, str): + raise TypeError("Input must be a string") + + # Convert string to list of characters to enable manual manipulation + chars = list(s) + + # Manual reversal using two-pointer technique + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + # Move pointers + left += 1 + right -= 1 + + # Convert back to string and return + return ''.join(chars) \ No newline at end of file diff --git a/src/url_parser.py b/src/url_parser.py new file mode 100644 index 00000000..fc8235f7 --- /dev/null +++ b/src/url_parser.py @@ -0,0 +1,47 @@ +from urllib.parse import urlparse +from typing import Dict, Any, Optional +import re + +def parse_url(url: str) -> Dict[str, Optional[str]]: + """ + Parse a given URL into its components. + + Args: + url (str): The URL to parse. + + Returns: + Dict[str, Optional[str]]: A dictionary containing URL components. + + Raises: + ValueError: If the input is not a valid URL. + """ + # Validate input + if not isinstance(url, str): + raise ValueError("Input must be a string") + + if not url.strip(): + raise ValueError("URL cannot be empty") + + try: + # Use urlparse to break down the URL + parsed_url = urlparse(url) + + # Basic validation + if not parsed_url.scheme or not parsed_url.netloc: + raise ValueError(f"Invalid URL format: {url}") + + # Return a comprehensive dictionary of URL components + return { + 'scheme': parsed_url.scheme or None, + 'netloc': parsed_url.netloc or None, + 'path': parsed_url.path or None, + 'params': parsed_url.params or '', # Change to empty string instead of None + 'query': parsed_url.query or None, + 'fragment': parsed_url.fragment or None, + 'username': parsed_url.username or None, + 'password': parsed_url.password or None, + 'hostname': parsed_url.hostname or None, + 'port': parsed_url.port or None + } + except Exception: + raise ValueError(f"Invalid URL format: {url}") \ No newline at end of file diff --git a/tests/test_array_flattener.py b/tests/test_array_flattener.py new file mode 100644 index 00000000..a530d4c1 --- /dev/null +++ b/tests/test_array_flattener.py @@ -0,0 +1,40 @@ +import pytest +from src.array_flattener import flatten_array + +def test_flatten_simple_list(): + """Test flattening a simple list""" + assert flatten_array([1, 2, 3]) == [1, 2, 3] + +def test_flatten_nested_list(): + """Test flattening a nested list""" + assert flatten_array([1, [2, 3], 4]) == [1, 2, 3, 4] + +def test_flatten_deeply_nested_list(): + """Test flattening a deeply nested list""" + assert flatten_array([1, [2, [3, 4]], 5]) == [1, 2, 3, 4, 5] + +def test_flatten_multiple_nested_levels(): + """Test flattening list with multiple nesting levels""" + assert flatten_array([1, [2, [3, [4]]], 5]) == [1, 2, 3, 4, 5] + +def test_flatten_mixed_nesting(): + """Test flattening list with mixed nesting patterns""" + assert flatten_array([1, [2, 3], [4, [5, 6]]]) == [1, 2, 3, 4, 5, 6] + +def test_flatten_empty_list(): + """Test flattening an empty list""" + assert flatten_array([]) == [] + +def test_invalid_input_type(): + """Test that non-list inputs raise a TypeError""" + with pytest.raises(TypeError): + flatten_array("not a list") + +def test_invalid_element_type(): + """Test that lists with non-numeric, non-list elements raise a TypeError""" + with pytest.raises(TypeError): + flatten_array([1, 2, "string"]) + +def test_float_support(): + """Test that float values are supported""" + assert flatten_array([1.5, [2.7, 3], 4.2]) == [1.5, 2.7, 3, 4.2] \ No newline at end of file diff --git a/tests/test_binary_search.py b/tests/test_binary_search.py new file mode 100644 index 00000000..50187e39 --- /dev/null +++ b/tests/test_binary_search.py @@ -0,0 +1,41 @@ +import pytest +from src.binary_search import binary_search + +def test_binary_search_found(): + """Test finding an existing element in the list""" + assert binary_search([1, 2, 3, 4, 5], 3) == 2 + assert binary_search([1, 2, 3, 4, 5], 1) == 0 + assert binary_search([1, 2, 3, 4, 5], 5) == 4 + +def test_binary_search_not_found(): + """Test searching for a non-existent element""" + assert binary_search([1, 2, 3, 4, 5], 6) == -1 + assert binary_search([1, 2, 3, 4, 5], 0) == -1 + +def test_binary_search_empty_list(): + """Test searching in an empty list""" + assert binary_search([], 5) == -1 + +def test_binary_search_single_element(): + """Test searching in a single-element list""" + assert binary_search([1], 1) == 0 + assert binary_search([1], 2) == -1 + +def test_binary_search_unsorted_list(): + """Test that an unsorted list raises a ValueError""" + with pytest.raises(ValueError): + binary_search([5, 2, 1, 4, 3], 3) + +def test_binary_search_invalid_input(): + """Test that non-list inputs raise a TypeError""" + with pytest.raises(TypeError): + binary_search("not a list", 3) + with pytest.raises(TypeError): + binary_search(None, 3) + +def test_binary_search_large_list(): + """Test binary search on a larger sorted list""" + large_list = list(range(1000)) + assert binary_search(large_list, 500) == 500 + assert binary_search(large_list, 999) == 999 + assert binary_search(large_list, 1000) == -1 \ No newline at end of file diff --git a/tests/test_rgb_to_hex.py b/tests/test_rgb_to_hex.py new file mode 100644 index 00000000..f9457c4a --- /dev/null +++ b/tests/test_rgb_to_hex.py @@ -0,0 +1,37 @@ +import pytest +from src.rgb_to_hex import rgb_to_hex + +def test_basic_conversion(): + """Test basic RGB to Hex conversion""" + assert rgb_to_hex(255, 0, 0) == '#FF0000' # Red + assert rgb_to_hex(0, 255, 0) == '#00FF00' # Green + assert rgb_to_hex(0, 0, 255) == '#0000FF' # Blue + assert rgb_to_hex(128, 128, 128) == '#808080' # Gray + +def test_edge_cases(): + """Test edge case values""" + assert rgb_to_hex(0, 0, 0) == '#000000' # Black + assert rgb_to_hex(255, 255, 255) == '#FFFFFF' # White + +def test_invalid_inputs(): + """Test error handling for invalid inputs""" + # Out of range values + with pytest.raises(ValueError, match="Red value must be between 0 and 255"): + rgb_to_hex(-1, 0, 0) + + with pytest.raises(ValueError, match="Green value must be between 0 and 255"): + rgb_to_hex(0, 256, 0) + + with pytest.raises(ValueError, match="Blue value must be between 0 and 255"): + rgb_to_hex(0, 0, 300) + +def test_type_errors(): + """Test type checking for inputs""" + with pytest.raises(TypeError, match="Red value must be an integer"): + rgb_to_hex('255', 0, 0) + + with pytest.raises(TypeError, match="Green value must be an integer"): + rgb_to_hex(0, '255', 0) + + with pytest.raises(TypeError, match="Blue value must be an integer"): + rgb_to_hex(0, 0, '255') \ No newline at end of file diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 00000000..9187901d --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,37 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_string_normal(): + """Test reversing a normal string.""" + assert reverse_string("hello") == "olleh" + +def test_reverse_string_empty(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_string_single_char(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +def test_reverse_string_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_with_mixed_chars(): + """Test reversing a string with mixed character types.""" + assert reverse_string("Hello, World! 123") == "321 !dlroW ,olleH" + +def test_reverse_string_invalid_input(): + """Test that a TypeError is raised for non-string inputs.""" + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(123) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(None) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(["hello"]) \ No newline at end of file diff --git a/tests/test_url_parser.py b/tests/test_url_parser.py new file mode 100644 index 00000000..76e9f4eb --- /dev/null +++ b/tests/test_url_parser.py @@ -0,0 +1,70 @@ +import pytest +from src.url_parser import parse_url + +def test_full_url(): + """Test parsing a complete URL with all components.""" + url = "https://username:password@example.com:8080/path/to/page?query=value#fragment" + result = parse_url(url) + + assert result == { + 'scheme': 'https', + 'netloc': 'username:password@example.com:8080', + 'path': '/path/to/page', + 'params': '', + 'query': 'query=value', + 'fragment': 'fragment', + 'username': 'username', + 'password': 'password', + 'hostname': 'example.com', + 'port': 8080 + } + +def test_minimal_url(): + """Test parsing a minimal URL.""" + url = "https://example.com" + result = parse_url(url) + + assert result['scheme'] == 'https' + assert result['netloc'] == 'example.com' + assert result['path'] is None + assert result['query'] is None + assert result['fragment'] is None + +def test_url_with_path(): + """Test URL with path.""" + url = "http://example.com/path/to/resource" + result = parse_url(url) + + assert result['scheme'] == 'http' + assert result['path'] == '/path/to/resource' + +def test_url_with_query(): + """Test URL with query parameters.""" + url = "https://example.com/search?q=python&category=programming" + result = parse_url(url) + + assert result['query'] == 'q=python&category=programming' + +def test_url_with_fragment(): + """Test URL with fragment.""" + url = "https://example.com/page#section" + result = parse_url(url) + + assert result['fragment'] == 'section' + +def test_invalid_input_type(): + """Test that non-string input raises a ValueError.""" + with pytest.raises(ValueError, match="Input must be a string"): + parse_url(123) + +def test_empty_url(): + """Test that empty URL raises a ValueError.""" + with pytest.raises(ValueError, match="URL cannot be empty"): + parse_url("") + with pytest.raises(ValueError, match="URL cannot be empty"): + parse_url(" ") + +def test_invalid_url(): + """Test that malformed URL raises a ValueError.""" + with pytest.raises(ValueError, match="Invalid URL format"): + parse_url("not a valid url") \ No newline at end of file