Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions solutions/python/pig-latin/1/pig_latin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
"""
The task is to translate text from English to Pig Latin.
The translation is defined using four rules, which look at the pattern of vowels
and consonants at the beginning of a word. These rules look at each word's use
of vowels and consonants:

vowels: the letters a, e, i, o, and u
consonants: the other 21 letters of the English alphabet
"""


def translate(text: str) -> str:
"""
Translate text from English to Pig Latin.

:param text:
:return:
"""
words: list = text.split(' ')
return " ".join(process_text(word) for word in words)


def process_text(text: str) -> str:
# Rule 1
# If a word begins with a vowel,
# or starts with "xr" or "yt",
# add an "ay" sound to the end of the word.
if is_rule_1(text):
print(f"Rule #1: {text}")
return text + 'ay'

# Rule 2
# 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.
if is_rule_2(text):
print(f"Rule #2: {text}")
i = get_last_consonant_indx(text)
return text[i + 1:] + text[: i + 1] + 'ay'

# Rule 3
# 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.
if is_rule_3(text):
print(f"\nRule #3: {text}")
if text[:2] != 'qu':
i = text.index('qu')
return text[i + 2:] + text[:i + 2] + 'ay'
else:
return text[2:] + 'qu' + 'ay'

# Rule 4
# 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.
#
# Some examples:
# "my" -> "ym" -> "ymay" (starts with single consonant followed by "y")
# "rhythm" -> "ythmrh" -> "ythmrhay" (starts with multiple consonants followed by "y")
if is_rule_4(text):
print(f"\nRule #4: {text}")
i = text.index('y')
b = get_last_consonant_indx(text[:i])
return text[b + 1:] + text[:b + 1] + 'ay'


def is_rule_1(text: str) -> bool:
"""
Check if a word begins with a vowel, or starts with "xr" or "yt".

:param text:
:return:
"""
if is_vowel(text[0]):
return True
if text[:2] in ("xr", "yt"):
return True
return False


def is_rule_2(text: str) -> bool:
"""
Check ff a word begins with one or more consonants.
No 'qu' or 'y' in it.

:param text:
:return:
"""
return is_consonant(text[0]) and not is_rule_3(text) and not is_rule_4(text)


def is_rule_3(text: str) -> bool:
"""
Check if a word starts with zero or more consonants followed by "qu".

:param text:
:return:
"""
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
return False


def is_rule_4(text: str) -> bool:
"""
Check if a word starts with one or more consonants followed by "y".

:param text:
:return:
"""
if 'y' in text and text[0] != 'y':
for char in text[:text.index('y')]:
if is_vowel(char):
return False
return True
return False


def is_vowel(char: str) -> bool:
"""

:param char:
:return:
"""
return char in "aeiou"


def is_consonant(char: str) -> bool:
"""

:param char:
:return:
"""
return char.isalpha() and not is_vowel(char)


def get_last_consonant_indx(text: str) -> int:
"""

:param text:
:return:
"""
if len(text) == 1 and is_consonant(text):
return 0

for i, char in enumerate(text):
if not is_consonant(char):
return i - 1

return len(text) - 1