Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file added src/__pycache__/string_utils.cpython-312.pyc
Binary file not shown.
52 changes: 52 additions & 0 deletions src/string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
def reverse_string(input_string):
"""
Reverse a given string using manual character iteration.

Args:
input_string (str): The 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(input_string, str):
raise TypeError("Input must be a string")

# Manual string reversal
reversed_str = ""
for char in input_string:
reversed_str = char + reversed_str

return reversed_str

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 bright red)

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

# Validate color values are within 0-255 range
if not all(0 <= x <= 255 for x in (r, g, b)):
raise ValueError("RGB values must be between 0 and 255")

# Convert to hex, ensuring two-digit representation
hex_color = '#{:02X}{:02X}{:02X}'.format(r, g, b)

return hex_color
Binary file not shown.
42 changes: 42 additions & 0 deletions tests/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from src.string_utils import rgb_to_hex, reverse_string

def test_reverse_string():
assert reverse_string("hello") == "olleh"
assert reverse_string("") == ""
assert reverse_string("a") == "a"
with pytest.raises(TypeError):
reverse_string(123)

def test_rgb_to_hex_basic_conversion():
"""Test basic RGB to hex conversion"""
assert rgb_to_hex(255, 0, 0) == '#FF0000' # Bright Red
assert rgb_to_hex(0, 255, 0) == '#00FF00' # Bright Green
assert rgb_to_hex(0, 0, 255) == '#0000FF' # Bright Blue
assert rgb_to_hex(0, 0, 0) == '#000000' # Black
assert rgb_to_hex(255, 255, 255) == '#FFFFFF' # White

def test_rgb_to_hex_mixed_colors():
"""Test conversion of mixed color values"""
assert rgb_to_hex(128, 128, 128) == '#808080' # Mid Gray
assert rgb_to_hex(100, 200, 50) == '#64C832' # Mixed color

def test_rgb_to_hex_type_errors():
"""Test type validation for inputs"""
with pytest.raises(TypeError):
rgb_to_hex('255', 0, 0)
with pytest.raises(TypeError):
rgb_to_hex(255, '0', 0)
with pytest.raises(TypeError):
rgb_to_hex(255, 0, '0')
with pytest.raises(TypeError):
rgb_to_hex(255.5, 0, 0)

def test_rgb_to_hex_value_errors():
"""Test value range validation"""
with pytest.raises(ValueError):
rgb_to_hex(-1, 0, 0)
with pytest.raises(ValueError):
rgb_to_hex(0, 256, 0)
with pytest.raises(ValueError):
rgb_to_hex(0, 0, 300)