primeiro commit com exercicios de Exercism realizados até esta data
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
VOWELS: list = [
|
||||
"a",
|
||||
"A",
|
||||
"e",
|
||||
"E",
|
||||
"i",
|
||||
"I",
|
||||
"o",
|
||||
"O",
|
||||
"u",
|
||||
"U",
|
||||
]
|
||||
# commenting out this list since its not being used
|
||||
# CONSONANTS: list = [
|
||||
# letter for letter in range(ord("A"), ord("Z") + 1) if letter not in VOWELS
|
||||
# ]
|
||||
|
||||
XR_YT: tuple = (
|
||||
"xr",
|
||||
"Xr",
|
||||
"XR",
|
||||
"xR",
|
||||
"yt",
|
||||
"Yt",
|
||||
"YT",
|
||||
"yT",
|
||||
)
|
||||
|
||||
# NOTE: this could be easier if used with match pattern (re module) but for
|
||||
# now lets leave this way. can be used later for refactoring
|
||||
|
||||
|
||||
def translate(text):
|
||||
# list with words translated
|
||||
pig_latin_text: list = []
|
||||
for word in text.split():
|
||||
# rule one - starts with vowel or xt/yt
|
||||
if word.startswith(tuple(VOWELS)) or word.startswith(XR_YT):
|
||||
pig_latin_text.append(word + "ay")
|
||||
else:
|
||||
# rules 2, 3 and 4 starts with consonant, so lets treat them here
|
||||
consonants_at_start: str = "" # string with grabbed consonants
|
||||
for index, letter in enumerate(word):
|
||||
# letter is not a vowel, lets find if rules 3 or 4 applies
|
||||
if letter not in VOWELS:
|
||||
# check rule 4
|
||||
if letter == "y" and index != 0:
|
||||
pig_latin_text.append(word[index:] + word[:index] + "ay")
|
||||
break
|
||||
# special case of rule 3, when word starts with 'qu'
|
||||
if word.startswith("qu"):
|
||||
pig_latin_text.append(word[2:] + word[:2] + "ay")
|
||||
break
|
||||
# general case of rule 3, where 'qu' its precessed with only consonants
|
||||
if word[index + 1 : index + 3] == "qu":
|
||||
pig_latin_text.append(
|
||||
word[index + 3 :] + word[: index + 3] + "ay"
|
||||
)
|
||||
break
|
||||
consonants_at_start += letter
|
||||
else:
|
||||
# letter is a vowel, so lets apply rule 2
|
||||
pig_latin_text.append(word[index:] + consonants_at_start + "ay")
|
||||
break
|
||||
|
||||
return " ".join(pig_latin_text)
|
||||
Reference in New Issue
Block a user