Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pig Latin #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 52 additions & 1 deletion practice/pig-latin/pig_latin.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
def translate(text):
pass
"""
Translate a text to Pig Latin.

Pig Latin is a language game that children use to speak in code.
The rules to translate a word into Pig Latin are:

- If a word begins with a vowel (a, e, i, o, u), append "ay" to the end of the word.
- If a word begins with a consonant, move it to the end of the word, and then append "ay".
- If a word starts with "xr" or "yt", treat it as if it starts with a vowel.
- If "y" comes after a consonant cluster, it makes a vowel sound (e.g., "rhythm" -> "ythmrhay").
- If "u" follows a "q" that has been moved to the end of the word, it should also be moved (e.g., "quiet" -> "ietquay").

Args:
text (str): The text to be translated.

Returns:
str: The translated text.

Examples:
>>> translate("hello")
"ellohay"
>>> translate("apple")
"appleay"
"""
# Define the set of vowels and vowels including 'y'
vowels = {"a", "e", "i", "o", "u"}
vowels_y = {"a", "e", "i", "o", "u", "y"}

# Define the set of special cases where the word starts with these two letters
specials = {"xr", "yt"}

# Initialize the list to store the translated words
piggyfied = []

# Iterate over each word in the input text
for word in text.split():
# If the word starts with a vowel or a special case, append "ay" and continue to the next word
if word[0] in vowels or word[0:2] in specials:
piggyfied.append(word + "ay")
continue

# If the word starts with a consonant, find the position of the first vowel
for pos in range(1, len(word)):
if word[pos] in vowels_y:
# If the vowel is 'u' and the previous letter is 'q', increment the position
pos += 1 if word[pos] == 'u' and word[pos - 1] == "q" else 0
# Move the consonant(s) to the end of the word, append "ay", and break the loop
piggyfied.append(word[pos:] + word[:pos] + "ay")
break

# Join the translated words with spaces and return the result
return " ".join(piggyfied)
Loading