Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
0627232
Add string reversal function implementation
labrocadabro Apr 15, 2025
eec981d
Add comprehensive tests for string reversal function
labrocadabro Apr 15, 2025
71658be
Implement string reversal without using slice or reverse()
labrocadabro Apr 15, 2025
e04bd5c
Update tests for string reversal implementation
labrocadabro Apr 15, 2025
fdb1a8b
Implement array flattening function
laura-abro Apr 15, 2025
458cfef
Add comprehensive tests for array flattening function
laura-abro Apr 15, 2025
fec23c6
Implement binary search algorithm
labrocadabro Apr 15, 2025
3f5b973
Add comprehensive tests for binary search
labrocadabro Apr 15, 2025
12c83c9
Implement URL parser function with comprehensive parsing
laura-abro Apr 15, 2025
21a1605
Add comprehensive tests for URL parser function
laura-abro Apr 15, 2025
c7e1f0a
Add pytest to requirements
laura-abro Apr 15, 2025
34d6d90
Improve URL parsing with stricter validation
laura-abro Apr 15, 2025
8acca3c
Add RGB to Hex converter function
labrocadabro Apr 15, 2025
a48ebed
Add comprehensive tests for RGB to Hex converter
labrocadabro Apr 15, 2025
7fde374
Merged branch pr-232-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 15, 2025
8783713
Merged branch pr-233-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 15, 2025
02f1a81
Merged branch pr-234-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 15, 2025
c2faf8e
Merged branch pr-235-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 15, 2025
0e1907d
Merged branch pr-236-momstrosity-builder-test for PR https://github.c…
momstrosity Apr 15, 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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
29 changes: 29 additions & 0 deletions src/array_flatten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import List, Union

def flatten_array(arr: List[Union[int, List]]) -> List[int]:
"""
Recursively flatten a nested array of integers into a single-level list.

Args:
arr (List[Union[int, List]]): A potentially nested list of integers

Returns:
List[int]: A flattened list of integers

Raises:
TypeError: If the input contains non-integer and non-list elements
"""
flattened = []

for item in arr:
# If the item is a list, recursively flatten it
if isinstance(item, list):
flattened.extend(flatten_array(item))
# If the item is an integer, add it to the flattened list
elif isinstance(item, int):
flattened.append(item)
# Raise an error for non-integer and non-list elements
else:
raise TypeError(f"Invalid element type: {type(item)}. Only integers and lists are allowed.")

return flattened
46 changes: 46 additions & 0 deletions src/binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 (ascending order)
target: The element to search for

Returns:
int: Index of the target element if found, -1 otherwise

Raises:
TypeError: If input is not a list
ValueError: If the list is not sorted
"""
# Validate input
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:
# Calculate mid point to avoid potential integer overflow
mid = left + (right - left) // 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: int, g: int, b: int) -> str:
"""
Convert RGB color values to a hexadecimal color representation.

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 representation (uppercase)

Raises:
ValueError: If any color value is outside the range 0-255
"""
# Validate input values
if not all(isinstance(val, int) for val in (r, g, b)):
raise TypeError("RGB values must be integers")

if not all(0 <= val <= 255 for val in (r, g, b)):
raise ValueError("RGB values must be between 0 and 255")

# Convert each color component to a two-digit hex value
hex_color = '#{:02X}{:02X}{:02X}'.format(r, g, b)

return hex_color
30 changes: 30 additions & 0 deletions src/string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def reverse_string(s: str) -> str:
"""
Reverses the given string manually without using slicing or built-in reverse methods.

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 allow manipulation
chars = list(s)

# Manually reverse the list of characters
left, right = 0, len(chars) - 1
while left < right:
# Swap characters from both ends
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1

# Convert back to string and return
return ''.join(chars)
54 changes: 54 additions & 0 deletions src/url_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from urllib.parse import urlparse, parse_qs
from typing import Dict, Any, Optional

def parse_url(url: str) -> Dict[str, Any]:
"""
Parse a URL into its components and return a dictionary with detailed information.

Args:
url (str): The URL to parse.

Returns:
Dict[str, Any]: A dictionary containing parsed URL components:
- protocol: The URL protocol (http, https, etc.)
- domain: The domain name
- path: The path component of the URL
- query_params: A dictionary of query parameters
- port: The port number (if specified, otherwise None)
- fragment: The fragment identifier (if present, otherwise None)

Raises:
ValueError: If the input URL is invalid or empty.
"""
# Check for empty or None input
if not url or not isinstance(url, str):
raise ValueError("Invalid URL: URL must be a non-empty string")

try:
# Try to parse the URL, raising an error if it fails
parsed = urlparse(url)

# Require a scheme (protocol) to be a valid URL
if not parsed.scheme:
raise ValueError("Invalid URL: Missing protocol")

# Require a netloc (domain) to be a valid URL
if not parsed.netloc:
raise ValueError("Invalid URL: Missing domain")

# Extract query parameters
query_params = parse_qs(parsed.query)
# Convert query params from lists to single values if possible
query_params = {k: v[0] if len(v) == 1 else v for k, v in query_params.items()}

# Construct the result dictionary
return {
'protocol': parsed.scheme,
'domain': parsed.netloc.split(':')[0], # Remove port if present
'path': parsed.path,
'query_params': query_params,
'port': parsed.port,
'fragment': parsed.fragment or None
}
except Exception as e:
raise ValueError(f"Error parsing URL: {str(e)}")
31 changes: 31 additions & 0 deletions tests/test_array_flatten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from src.array_flatten import flatten_array

def test_flatten_single_level_array():
"""Test flattening a single-level array"""
assert flatten_array([1, 2, 3]) == [1, 2, 3]

def test_flatten_nested_array():
"""Test flattening a nested array"""
assert flatten_array([1, [2, 3], 4]) == [1, 2, 3, 4]

def test_flatten_deeply_nested_array():
"""Test flattening a deeply nested array"""
assert flatten_array([1, [2, [3, 4]], 5]) == [1, 2, 3, 4, 5]

def test_flatten_empty_array():
"""Test flattening an empty array"""
assert flatten_array([]) == []

def test_flatten_array_with_empty_nested_lists():
"""Test flattening an array with empty nested lists"""
assert flatten_array([1, [], [2, []], 3]) == [1, 2, 3]

def test_invalid_element_type():
"""Test raising TypeError for non-integer and non-list elements"""
with pytest.raises(TypeError, match="Invalid element type"):
flatten_array([1, 2, "3"])

def test_complex_nested_array():
"""Test flattening a complex nested array"""
assert flatten_array([1, [2, [3, [4, 5]]], [6, 7]]) == [1, 2, 3, 4, 5, 6, 7]
42 changes: 42 additions & 0 deletions tests/test_binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from src.binary_search import binary_search

def test_binary_search_normal_case():
"""Test binary search with a typical sorted list"""
arr = [1, 3, 5, 7, 9, 11, 13]
assert binary_search(arr, 7) == 3
assert binary_search(arr, 13) == 6
assert binary_search(arr, 1) == 0

def test_binary_search_not_found():
"""Test when target is not in the list"""
arr = [1, 3, 5, 7, 9, 11, 13]
assert binary_search(arr, 4) == -1
assert binary_search(arr, 0) == -1
assert binary_search(arr, 14) == -1

def test_binary_search_empty_list():
"""Test binary search on an empty list"""
assert binary_search([], 5) == -1

def test_binary_search_single_element():
"""Test binary search on a single-element list"""
arr = [5]
assert binary_search(arr, 5) == 0
assert binary_search(arr, 6) == -1

def test_binary_search_duplicate_elements():
"""Test binary search with duplicate elements"""
arr = [1, 2, 2, 3, 3, 3, 4, 4, 5]
# Note: Returns the index of one of the matching elements
assert binary_search(arr, 3) in [3, 4, 5]

def test_binary_search_invalid_input():
"""Test error handling for invalid inputs"""
# Not a list
with pytest.raises(TypeError):
binary_search("not a list", 5)

# Unsorted list
with pytest.raises(ValueError):
binary_search([5, 3, 1, 4, 2], 3)
34 changes: 34 additions & 0 deletions tests/test_rgb_to_hex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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, 255, 255) == '#FFFFFF'
assert rgb_to_hex(0, 0, 0) == '#000000'
assert rgb_to_hex(255, 0, 0) == '#FF0000'
assert rgb_to_hex(0, 255, 0) == '#00FF00'
assert rgb_to_hex(0, 0, 255) == '#0000FF'

def test_mid_range_conversion():
"""Test mid-range RGB to Hex conversion"""
assert rgb_to_hex(128, 128, 128) == '#808080'
assert rgb_to_hex(100, 150, 200) == '#6496C8'

def test_invalid_inputs():
"""Test error handling for invalid inputs"""
# Test out of range values
with pytest.raises(ValueError, match="RGB values must be between 0 and 255"):
rgb_to_hex(-1, 0, 0)
with pytest.raises(ValueError, match="RGB values must be between 0 and 255"):
rgb_to_hex(0, 256, 0)
with pytest.raises(ValueError, match="RGB values must be between 0 and 255"):
rgb_to_hex(0, 0, 300)

def test_type_errors():
"""Test error handling for incorrect input types"""
with pytest.raises(TypeError, match="RGB values must be integers"):
rgb_to_hex(1.5, 0, 0)
with pytest.raises(TypeError, match="RGB values must be integers"):
rgb_to_hex('255', 0, 0)
with pytest.raises(TypeError, match="RGB values must be integers"):
rgb_to_hex(0, [255], 0)
34 changes: 34 additions & 0 deletions tests/test_string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from src.string_reversal import reverse_string

def test_reverse_string_basic():
"""Test basic string reversal."""
assert reverse_string("hello") == "olleh"
assert reverse_string("python") == "nohtyp"

def test_reverse_string_empty():
"""Test reversing an empty string."""
assert reverse_string("") == ""

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_unicode():
"""Test reversing a string with Unicode characters."""
assert reverse_string("café") == "éfac"

def test_reverse_string_invalid_input():
"""Test that 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