Skip to content

Commit fb092f8

Browse files
[Sync Iteration] python/pig-latin/8
1 parent 32b8ab9 commit fb092f8

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""
2+
The task is to translate text from English to Pig Latin.
3+
The translation is defined using four rules, which look at the pattern of vowels
4+
and consonants at the beginning of a word. These rules look at each word's use
5+
of vowels and consonants:
6+
7+
vowels: the letters a, e, i, o, and u
8+
consonants: the other 21 letters of the English alphabet
9+
"""
10+
11+
import logging
12+
13+
14+
def translate(text: str) -> str:
15+
"""
16+
Translate text from English to Pig Latin.
17+
18+
:param text:
19+
:return:
20+
"""
21+
# Setup logging (console handler for visibility)
22+
logging.basicConfig(level=logging.INFO, format="%(message)s")
23+
logger = logging.getLogger(__name__)
24+
logger.info("Translating text: %s", text)
25+
words: list = text.split(" ")
26+
return " ".join(process_text(word, logger) for word in words)
27+
28+
29+
def process_text(text: str, logger: logging.Logger) -> str:
30+
"""
31+
Convert a string based on 4 rules of Pig Latin.
32+
33+
:param logger:
34+
:param text:
35+
:return:
36+
"""
37+
if not text:
38+
logger.info("Skipping empty word: '%s'", text)
39+
return ""
40+
41+
logger.info("Processing word: %s", text)
42+
# Rule 1
43+
if is_rule_1(text):
44+
# If a word begins with a vowel,
45+
# or starts with "xr" or "yt",
46+
# add an "ay" sound to the end of the word.
47+
logger.info("Applied Rule #1 to '%s'", text)
48+
return f"{text}ay"
49+
50+
# Rule 2
51+
if is_rule_2(text):
52+
# If a word begins with one or more consonants, first move those consonants
53+
# to the end of the word and then add an "ay" sound to the end of the word.
54+
logger.info("Applied Rule #2 to '%s'", text)
55+
i = get_last_consonant_indx(text)
56+
return f"{text[i + 1 :]}{text[: i + 1]}ay"
57+
58+
# Rule 3
59+
if is_rule_3(text):
60+
# If a word starts with zero or more consonants followed by "qu", first move
61+
# those consonants (if any) and the "qu" part to the end of the word, and then
62+
# add an "ay" sound to the end of the word.
63+
logger.info("Applied Rule #3 to '%s'", text)
64+
i = text.index("qu")
65+
return f"{text[i + 2 :]}{text[: i + 2]}ay"
66+
67+
# Rule 4
68+
if is_rule_4(text):
69+
# If a word starts with one or more consonants followed by "y", first move the
70+
# consonants preceding the "y" to the end of the word, and then add an "ay" sound
71+
# to the end of the word.
72+
logger.info("Applied Rule #4 to '%s'", text)
73+
i = text.index("y")
74+
return f"{text[i:]}{text[:i]}ay"
75+
76+
raise ValueError(f"Unhandled word in Pig Latin translation: '{text}'")
77+
78+
79+
def is_rule_1(text: str) -> bool:
80+
"""
81+
Check if a word begins with a vowel, or starts with "xr" or "yt".
82+
83+
:param text:
84+
:return:
85+
"""
86+
if is_vowel(text[0]):
87+
return True
88+
if text[:2] in ("xr", "yt"):
89+
return True
90+
return False
91+
92+
93+
def is_rule_2(text: str) -> bool:
94+
"""
95+
Check ff a word begins with one or more consonants.
96+
No 'qu' or 'y' in it.
97+
98+
:param text:
99+
:return:
100+
"""
101+
return is_consonant(text[0]) and not is_rule_3(text) and not is_rule_4(text)
102+
103+
104+
def is_rule_3(text: str) -> bool:
105+
"""
106+
Check if a word starts with zero or more consonants followed by "qu".
107+
108+
:param text:
109+
:return:
110+
"""
111+
if "qu" in text:
112+
if text[:2] == "qu":
113+
return True
114+
115+
for char in text[: text.index("qu")]:
116+
if is_vowel(char):
117+
return False
118+
return True
119+
return False
120+
121+
122+
def is_rule_4(text: str) -> bool:
123+
"""
124+
Check if a word starts with one or more consonants followed by "y".
125+
126+
:param text:
127+
:return:
128+
"""
129+
if "y" in text and text[0] != "y":
130+
for char in text[: text.index("y")]:
131+
if is_vowel(char):
132+
return False
133+
return True
134+
return False
135+
136+
137+
def is_vowel(char: str) -> bool:
138+
"""
139+
Test that char in vowels: the letters a, e, i, o, and u.
140+
141+
:param char:
142+
:return:
143+
"""
144+
return char in "aeiou"
145+
146+
147+
def is_consonant(char: str) -> bool:
148+
"""
149+
Check that char is consonant (the other 21 letters of the English alphabet).
150+
151+
:param char:
152+
:return:
153+
"""
154+
return char.isalpha() and not is_vowel(char)
155+
156+
157+
def get_last_consonant_indx(text: str) -> int:
158+
"""
159+
Get last index of consonant inside the string.
160+
161+
:param text:
162+
:return:
163+
"""
164+
i = 0
165+
for n, char in enumerate(text):
166+
if is_consonant(char):
167+
i = n
168+
else:
169+
break
170+
171+
return i

0 commit comments

Comments
 (0)