diff --git a/solutions/find_highest_alpha_position.py b/solutions/find_highest_alpha_position.py new file mode 100644 index 000000000..c03ce0617 --- /dev/null +++ b/solutions/find_highest_alpha_position.py @@ -0,0 +1,52 @@ +"""Module for finding the highest alphabetical position in a text string. + +This module provides functionality to find the alphabetical position (1-26) +of the character that appears latest in the alphabet within a given text. + +@author: Musab Kaymak +@created: 01/09/2025 +""" + + +def find_highest_alpha_position(text: str) -> int: + """Find the alphabetical position of the latest alphabet character in text. + + Parameters: + text (str): The input text to analyze. Must contain at least one letter + and only contain English letters. Numbers and special characters are ignored. + + Returns: + int: The highest alphabetical position (1-26) found in the text. + 'a' is position 1, 'z' is position 26. + + Raises: + ValueError: If the text is empty or contains no letters. + ValueError: If the text contains non-ASCII letters. + + Examples: + >>> find_highest_alpha_position("flower") + 23 + >>> find_highest_alpha_position("apple") + 16 + >>> find_highest_alpha_position("ZEBRA") + 26 + """ + if not text: + raise ValueError("Input text cannot be empty") + + if not any(c.isalpha() for c in text): + raise ValueError("Input text must contain at least one letter") + + if not all(c.isascii() for c in text): + raise ValueError("Input text must contain only English characters") + + alphabet = "abcdefghijklmnopqrstuvwxyz" + max_position = 0 + + for char in text.lower(): + if char.isalpha(): + position = alphabet.index(char.lower()) + 1 + if position > max_position: + max_position = position + + return max_position diff --git a/solutions/tests/test_find_highest_alpha_position.py b/solutions/tests/test_find_highest_alpha_position.py new file mode 100644 index 000000000..051185ba7 --- /dev/null +++ b/solutions/tests/test_find_highest_alpha_position.py @@ -0,0 +1,60 @@ +"""Tests for the find_highest_alpha_position function. + +@author: Musab Kaymak +@created: 01/09/2025 +""" + +import unittest + +from solutions.find_highest_alpha_position import find_highest_alpha_position + + +class TestFindHighestAlphaPosition(unittest.TestCase): + """Test cases for the find_highest_alpha_position function.""" + + def test_simple_word(self): + """Test with a simple lowercase word.""" + self.assertEqual(find_highest_alpha_position("flower"), 23) + + def test_uppercase_word(self): + """Test with an uppercase word.""" + self.assertEqual(find_highest_alpha_position("ZEBRA"), 26) + + def test_mixed_case_word(self): + """Test with a mixed case word.""" + self.assertEqual(find_highest_alpha_position("PyThOn"), 25) + + def test_single_letter(self): + """Test with a single letter.""" + self.assertEqual(find_highest_alpha_position("a"), 1) + + def test_with_spaces(self): + """Test with text containing spaces.""" + self.assertEqual(find_highest_alpha_position("hello world"), 23) + + def test_with_numbers(self): + """Test with text containing numbers.""" + self.assertEqual(find_highest_alpha_position("hello123"), 15) + + def test_with_special_characters(self): + """Test with text containing special characters.""" + self.assertEqual(find_highest_alpha_position("hello!@#"), 15) + + def test_empty_string(self): + """Test that empty string raises ValueError.""" + with self.assertRaises(ValueError, msg="Input text cannot be empty"): + find_highest_alpha_position("") + + def test_no_letters(self): + """Test that string with no letters raises ValueError.""" + with self.assertRaises( + ValueError, msg="Input text must contain at least one letter." + ): + find_highest_alpha_position("123") + + def test_non_english_characters(self): + """Test that non-English characters raise ValueError.""" + with self.assertRaises( + ValueError, msg="Input text must contain only English characters." + ): + find_highest_alpha_position("héllo")