Skip to content

Commit 34c421c

Browse files
authored
Merge pull request #93 from ikostan/main
Merge from master
2 parents 8ace286 + ac72d87 commit 34c421c

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

pig-latin/pig_latin.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def translate(text: str) -> str:
1616
:param text: The English text to translate
1717
:return: The text translated to Pig Latin
1818
"""
19-
words: list[str] = text.lower().split(" ")
19+
words: list[str] = text.split(" ")
2020
return " ".join(process_word(word) for word in words)
2121

2222

@@ -31,35 +31,37 @@ def process_word(text: str) -> str:
3131
if not text:
3232
return ""
3333

34+
text_lower: str = text.lower()
35+
3436
# Rule 1
35-
if is_vowel(text[0]) or text[:2] in ("xr", "yt"):
37+
if is_vowel(text_lower[0]) or text_lower[:2] in ("xr", "yt"):
3638
# If a word begins with a vowel,
3739
# or starts with "xr" or "yt",
3840
# add an "ay" sound to the end of the word.
3941
return f"{text}ay"
4042

4143
# Rule 3
42-
if is_rule_3(text):
44+
if is_rule_3(text_lower):
4345
# If a word starts with zero or more consonants followed by "qu",
4446
# first move those consonants (if any) and the "qu" part to the
4547
# end of the word, and then add an "ay" sound to the end of the word.
46-
i = text.index("qu")
48+
i = text_lower.index("qu")
4749
return f"{text[i + 2 :]}{text[: i + 2]}ay"
4850

4951
# Rule 4
50-
if is_rule_4(text):
52+
if is_rule_4(text_lower):
5153
# If a word starts with one or more consonants followed by "y",
5254
# first move the consonants preceding the "y" to the end of the
5355
# word, and then add an "ay" sound to the end of the word.
54-
i = text.index("y")
56+
i = text_lower.index("y")
5557
return f"{text[i:]}{text[:i]}ay"
5658

5759
# Rule 2
58-
if not is_vowel(text[0]):
60+
if not is_vowel(text_lower[0]):
5961
# If a word begins with one or more consonants, first move those
6062
# consonants to the end of the word and then add an "ay" sound
6163
# to the end of the word.
62-
i = get_consonant_cluster_length(text)
64+
i = get_consonant_cluster_index(text_lower)
6365
return f"{text[i + 1 :]}{text[: i + 1]}ay"
6466

6567
raise ValueError(f"Unhandled word in Pig Latin translation: '{text}'")
@@ -69,32 +71,25 @@ def is_rule_3(text: str) -> bool:
6971
"""
7072
Check if a word starts with zero or more consonants followed by "qu".
7173
72-
:param text:
73-
:return:
74+
:param text: The word to check
75+
:return: True if the word matches the pattern, False otherwise
7476
"""
7577
if "qu" in text:
76-
if text[:2] == "qu":
77-
return True
78-
79-
for char in text[: text.index("qu")]:
80-
if is_vowel(char):
81-
return False
82-
return True
78+
qu_indx: int = text.index("qu")
79+
return all(not is_vowel(char) for char in text[:qu_indx])
8380
return False
8481

8582

8683
def is_rule_4(text: str) -> bool:
8784
"""
8885
Check if a word starts with one or more consonants followed by "y".
8986
90-
:param text:
91-
:return:
87+
:param text: The word to check
88+
:return: True if the word matches the pattern, False otherwise
9289
"""
9390
if "y" in text and text[0] != "y":
94-
for char in text[: text.index("y")]:
95-
if is_vowel(char):
96-
return False
97-
return True
91+
y_indx: int = text.index("y")
92+
return all(not is_vowel(char) for char in text[:y_indx])
9893
return False
9994

10095

@@ -108,18 +103,18 @@ def is_vowel(char: str) -> bool:
108103
return char in "aeiou"
109104

110105

111-
def get_consonant_cluster_length(text: str) -> int:
106+
def get_consonant_cluster_index(text: str) -> int:
112107
"""
113-
Find the length of the consonant cluster at the beginning of a word.
108+
Find the index of the last consonant in the initial consonant cluster.
114109
115110
:param text: The word to analyze
116111
:return: The index of the last consonant in the initial consonant cluster
117112
"""
118-
i = 0
119-
for n, char in enumerate(text):
113+
index: int = 0
114+
for i, char in enumerate(text):
120115
if not is_vowel(char):
121-
i = n
116+
index = i
122117
else:
123118
break
124119

125-
return i
120+
return index

pig-latin/pig_latin_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313

1414
class PigLatinTest(unittest.TestCase):
15+
16+
def test_word_with_capital_letter(self):
17+
self.assertEqual(translate("Queen"), "eenQuay")
18+
1519
def test_word_beginning_with_a(self):
1620
self.assertEqual(translate("apple"), "appleay")
1721

0 commit comments

Comments
 (0)