Introduction
Your parents have challenged you and your sibling to a game of two-on-two basketball.
Confident they’ll win, they let you score the first couple of points, but then start taking over the game.
Needing a little boost, you start speaking in Pig Latin, which is a made-up children’s language that’s difficult for non-children to understand.
This will give you the edge to prevail over your parents!
Instructions
Instructions
Your 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
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.
For example:
"apple" -> "appleay" (starts with vowel)
"xray" -> "xrayay" (starts with "xr")
"yttria" -> "yttriaay" (starts with "yt")
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.
For example:
"pig" -> "igp" -> "igpay" (starts with single consonant)
"chair" -> "airch" -> "airchay" (starts with multiple consonants)
"thrush" -> "ushthr" -> "ushthray" (starts with multiple consonants)
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.
For example:
"quick" -> "ickqu" -> "ickquay" (starts with "qu", no preceding consonants)
"square" -> "aresqu" -> "aresquay" (starts with one consonant followed by "qu”)
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")
Dig Deeper
Sets and slices
Sets and slices
VOWELS = {"a", "e", "i", "o", "u"}
VOWELS_Y = {"a", "e", "i", "o", "u", "y"}
SPECIALS = {"xr", "yt"}
def translate(text):
piggyfied = []
for word in text.split():
if word[0] in VOWELS or word[0:2] in SPECIALS:
piggyfied.append(word + "ay")
continue
for pos in range(1, len(word)):
if word[pos] in VOWELS_Y:
pos += 1 if word[pos] == 'u' and word[pos - 1] == "q" else 0
piggyfied.append(word[pos:] + word[:pos] + "ay")
break
return " ".join(piggyfied)
This approach begins by defining sets for looking up matching characters from the input.
Python doesn’t enforce having real constant values,
but the sets are defined with all uppercase letters, which is the naming convention for a Python constant.
It indicates that the value is not intended to be changed.
The translate() function begins by defining the list which will hold the parsed value(s).
The input is split() into a list of its words, which is then iterated.
String indexing is used to check if the first letter of the word is in the set of vowels.
If so, the logical or operator “short-circuits” and the word plus “ay” is appended to the list.
If the first letter is not a vowel, then a slice of the first two letters is used to check if they match any of the special beginning characters.
If the letters match, the word plus “ay” will be appended to the list.
If the beginning of the word matches either condition, the loop continues to the next word.
If the beginning of the word did not match either condition,
that leaves ranging its characters from position 1 until the len() of the word.
When a [range](https://docs.python.org/3/library/stdtypes.html?#range) is provided two arguments,
it generates values from the `start` argument up to _but not including_ the `stop` argument.
This behavior can be referred to as start inclusive, stop exclusive.
The inner loop iterating characters is nested within the outer loop that iterates the words.
Each character is iterated until finding a vowel (at this point, the letter y is now considered a vowel.)
If the vowel is u, the previous consonant is checked to be q.
If so, the position is advanced to be one position after the found u.
A slice is then taken from the position until the end of the word,
plus a slice taken from the beginning of the word and stopped just before the position, plus ay.
That concatenated string is appended to the list, break is called to exit the inner loop,
and execution returns to the outer loop.
Once all of the words in the input have been iterated,
the join method is called on a space character to connect all of the words in the list back into a single string.
Since the space is only used as a separator between elements of the list, if the list has only one element,
the space will not be added to the beginning or end of the output string.
Source: Exercism python/pig-latin