diff --git a/src/__pycache__/string_utils.cpython-312.pyc b/src/__pycache__/string_utils.cpython-312.pyc new file mode 100644 index 0000000..e12e39f Binary files /dev/null and b/src/__pycache__/string_utils.cpython-312.pyc differ diff --git a/src/string_utils.py b/src/string_utils.py new file mode 100644 index 0000000..de31cb8 --- /dev/null +++ b/src/string_utils.py @@ -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 \ No newline at end of file diff --git a/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc new file mode 100644 index 0000000..472ec8a Binary files /dev/null and b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc differ diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py new file mode 100644 index 0000000..2b0519f --- /dev/null +++ b/tests/test_string_utils.py @@ -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) \ No newline at end of file