From 3ca5429300d3217e2361a361f69808536e94565f Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Mon, 1 Sep 2025 12:42:51 -0700 Subject: [PATCH 1/3] Pig latin --- pig-latin/pig_latin.py | 38 +++++++++++++++++++------------------ pig-latin/pig_latin_test.py | 4 ++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/pig-latin/pig_latin.py b/pig-latin/pig_latin.py index feb60a0..814b51e 100644 --- a/pig-latin/pig_latin.py +++ b/pig-latin/pig_latin.py @@ -16,7 +16,7 @@ def translate(text: str) -> str: :param text: The English text to translate :return: The text translated to Pig Latin """ - words: list[str] = text.lower().split(" ") + words: list[str] = text.split(" ") return " ".join(process_word(word) for word in words) @@ -31,35 +31,37 @@ def process_word(text: str) -> str: if not text: return "" + text_lower: str = text.lower() + # Rule 1 - if is_vowel(text[0]) or text[:2] in ("xr", "yt"): + if is_vowel(text_lower[0]) or text_lower[:2] in ("xr", "yt"): # If a word begins with a vowel, # or starts with "xr" or "yt", # add an "ay" sound to the end of the word. return f"{text}ay" # Rule 3 - if is_rule_3(text): + if is_rule_3(text_lower): # If a word starts with zero or more consonants followed by "qu", # first move those consonants (if any) and the "qu" part to the # end of the word, and then add an "ay" sound to the end of the word. - i = text.index("qu") + i = text_lower.index("qu") return f"{text[i + 2 :]}{text[: i + 2]}ay" # Rule 4 - if is_rule_4(text): + if is_rule_4(text_lower): # If a word starts with one or more consonants followed by "y", # first move the consonants preceding the "y" to the end of the # word, and then add an "ay" sound to the end of the word. - i = text.index("y") + i = text_lower.index("y") return f"{text[i:]}{text[:i]}ay" # Rule 2 - if not is_vowel(text[0]): + if not is_vowel(text_lower[0]): # If a word begins with one or more consonants, first move those # consonants to the end of the word and then add an "ay" sound # to the end of the word. - i = get_consonant_cluster_length(text) + i = get_consonant_cluster_index(text_lower) return f"{text[i + 1 :]}{text[: i + 1]}ay" raise ValueError(f"Unhandled word in Pig Latin translation: '{text}'") @@ -69,8 +71,8 @@ def is_rule_3(text: str) -> bool: """ Check if a word starts with zero or more consonants followed by "qu". - :param text: - :return: + :param text: The word to check + :return: True if the word matches the pattern, False otherwise """ if "qu" in text: if text[:2] == "qu": @@ -87,8 +89,8 @@ def is_rule_4(text: str) -> bool: """ Check if a word starts with one or more consonants followed by "y". - :param text: - :return: + :param text: The word to check + :return: True if the word matches the pattern, False otherwise """ if "y" in text and text[0] != "y": for char in text[: text.index("y")]: @@ -108,18 +110,18 @@ def is_vowel(char: str) -> bool: return char in "aeiou" -def get_consonant_cluster_length(text: str) -> int: +def get_consonant_cluster_index(text: str) -> int: """ - Find the length of the consonant cluster at the beginning of a word. + Find the index of the last consonant in the initial consonant cluster. :param text: The word to analyze :return: The index of the last consonant in the initial consonant cluster """ - i = 0 - for n, char in enumerate(text): + index: int = 0 + for i, char in enumerate(text): if not is_vowel(char): - i = n + index = i else: break - return i + return index diff --git a/pig-latin/pig_latin_test.py b/pig-latin/pig_latin_test.py index 309f49c..b220b38 100644 --- a/pig-latin/pig_latin_test.py +++ b/pig-latin/pig_latin_test.py @@ -12,6 +12,10 @@ class PigLatinTest(unittest.TestCase): + + def test_word_with_capital_letter(self): + self.assertEqual(translate("Queen"), "eenQuay") + def test_word_beginning_with_a(self): self.assertEqual(translate("apple"), "appleay") From 37607daa95d6626cb0c6eeda2f2b347519f74829 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Wed, 3 Sep 2025 12:11:29 -0700 Subject: [PATCH 2/3] Update pig_latin.py --- pig-latin/pig_latin.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/pig-latin/pig_latin.py b/pig-latin/pig_latin.py index 814b51e..76d9de6 100644 --- a/pig-latin/pig_latin.py +++ b/pig-latin/pig_latin.py @@ -75,13 +75,8 @@ def is_rule_3(text: str) -> bool: :return: True if the word matches the pattern, False otherwise """ if "qu" in text: - if text[:2] == "qu": - return True - - for char in text[: text.index("qu")]: - if is_vowel(char): - return False - return True + qu_indx: int = text.index("qu") + return all(not is_vowel(char) for char in text[: qu_indx]) return False @@ -93,10 +88,8 @@ def is_rule_4(text: str) -> bool: :return: True if the word matches the pattern, False otherwise """ if "y" in text and text[0] != "y": - for char in text[: text.index("y")]: - if is_vowel(char): - return False - return True + y_indx: int = text.index("y") + return all(not is_vowel(char) for char in text[: y_indx]) return False From ac72d8751eb20dfb7b3affc68c2488712a0d91ce Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Wed, 3 Sep 2025 12:14:22 -0700 Subject: [PATCH 3/3] Update pig_latin.py --- pig-latin/pig_latin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pig-latin/pig_latin.py b/pig-latin/pig_latin.py index 76d9de6..928c7ed 100644 --- a/pig-latin/pig_latin.py +++ b/pig-latin/pig_latin.py @@ -76,7 +76,7 @@ def is_rule_3(text: str) -> bool: """ if "qu" in text: qu_indx: int = text.index("qu") - return all(not is_vowel(char) for char in text[: qu_indx]) + return all(not is_vowel(char) for char in text[:qu_indx]) return False @@ -89,7 +89,7 @@ def is_rule_4(text: str) -> bool: """ if "y" in text and text[0] != "y": y_indx: int = text.index("y") - return all(not is_vowel(char) for char in text[: y_indx]) + return all(not is_vowel(char) for char in text[:y_indx]) return False