Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
003d3a7
Implement array flattening function with recursive approach
labrocadabro Apr 14, 2025
1a2b817
Add comprehensive tests for array flattening function
labrocadabro Apr 14, 2025
0ef5990
Implement URL parser function
laura-abro Apr 14, 2025
46d6437
Add comprehensive tests for URL parser function
laura-abro Apr 14, 2025
40c5e43
Modify URL parser to handle params and invalid URLs
laura-abro Apr 14, 2025
a64a4e5
Enhance URL validation with regex and stricter checks
laura-abro Apr 14, 2025
0532688
Simplify URL validation to use urlparse checks
laura-abro Apr 14, 2025
bdeb538
Add string reversal function implementation
momstrosity Apr 14, 2025
8746c37
Implement binary search function with comprehensive error handling
labrocadabro Apr 14, 2025
8c49f41
Add comprehensive tests for string reversal function
momstrosity Apr 14, 2025
5b72103
Add comprehensive tests for binary search function
labrocadabro Apr 14, 2025
17595f6
Implement string reversal using manual two-pointer approach
momstrosity Apr 14, 2025
38d4fc3
Update tests for string reversal function
momstrosity Apr 14, 2025
bf106d5
Implement RGB to Hex converter function
laura-abro Apr 14, 2025
462822c
Add comprehensive tests for RGB to Hex converter
laura-abro Apr 14, 2025
c5d87e8
Merged branch pr-209-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 14, 2025
6aa4b5d
Merged branch pr-211-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 14, 2025
3acfaff
Merged branch pr-212-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 14, 2025
a4dd634
Merged branch pr-213-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 14, 2025
0b94357
Merged branch pr-215-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/array_flattener.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions src/binary_search.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions src/rgb_to_hex.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions src/string_reversal.py
Original file line number Diff line number Diff line change
@@ -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)
47 changes: 47 additions & 0 deletions src/url_parser.py
Original file line number Diff line number Diff line change
@@ -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}")
40 changes: 40 additions & 0 deletions tests/test_array_flattener.py
Original file line number Diff line number Diff line change
@@ -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]
41 changes: 41 additions & 0 deletions tests/test_binary_search.py
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions tests/test_rgb_to_hex.py
Original file line number Diff line number Diff line change
@@ -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')
37 changes: 37 additions & 0 deletions tests/test_string_reversal.py
Original file line number Diff line number Diff line change
@@ -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"])
Loading