Skip to content

Commit d25dcad

Browse files
committed
Update pig_latin.py
1 parent b08d54f commit d25dcad

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

pig-latin/pig_latin.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
consonants: the other 21 letters of the English alphabet
99
"""
1010

11+
import logging
12+
1113

1214
def translate(text: str) -> str:
1315
"""
@@ -16,46 +18,56 @@ def translate(text: str) -> str:
1618
:param text:
1719
:return:
1820
"""
21+
# Setup logging (console handler for visibility)
22+
logging.basicConfig(level=logging.INFO, format='%(message)s')
23+
logger = logging.getLogger(__name__)
24+
logger.info(f"Translating text: {text}")
1925
words: list = text.split(" ")
20-
return " ".join(process_text(word) for word in words)
26+
return " ".join(process_text(word, logger) for word in words)
2127

2228

23-
def process_text(text: str) -> str:
29+
def process_text(text: str, logger: logging.Logger) -> str:
2430
"""
2531
Convert a string based on 4 rules of Pig Latin.
2632
33+
:param logger:
2734
:param text:
2835
:return:
2936
"""
37+
logger.info(f"Processing word: {text}")
3038
# Rule 1
3139
if is_rule_1(text):
3240
# If a word begins with a vowel,
3341
# or starts with "xr" or "yt",
3442
# add an "ay" sound to the end of the word.
35-
return text + "ay"
43+
logger.info(f"Applied Rule #1 to '{text}'")
44+
return f"{text}ay"
3645

3746
# Rule 2
3847
if is_rule_2(text):
3948
# If a word begins with one or more consonants, first move those consonants
4049
# to the end of the word and then add an "ay" sound to the end of the word.
50+
logger.info(f"Applied Rule #2 to '{text}'")
4151
i = get_last_consonant_indx(text)
42-
return text[i + 1 :] + text[: i + 1] + "ay"
52+
return f"{text[i + 1:]}{text[: i + 1]}ay"
4353

4454
# Rule 3
4555
if is_rule_3(text):
4656
# If a word starts with zero or more consonants followed by "qu", first move
4757
# those consonants (if any) and the "qu" part to the end of the word, and then
4858
# add an "ay" sound to the end of the word.
59+
logger.info(f"Applied Rule #3 to '{text}'")
4960
i = text.index("qu")
50-
return text[i + 2 :] + text[: i + 2] + "ay"
61+
return f"{text[i + 2:]}{text[: i + 2]}ay"
5162

5263
# Rule 4
5364
if is_rule_4(text):
5465
# If a word starts with one or more consonants followed by "y", first move the
5566
# consonants preceding the "y" to the end of the word, and then add an "ay" sound
5667
# to the end of the word.
68+
logger.info(f"Applied Rule #4 to '{text}'")
5769
i = text.index("y")
58-
return text[i:] + text[:i] + "ay"
70+
return f"{text[i:]}{text[:i]}ay"
5971

6072
raise ValueError(f"Unhandled word in Pig Latin translation: '{text}'")
6173

0 commit comments

Comments
 (0)