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
HashSet lookup
HashSet lookup
object PigLatin {
private val vowels = hashSetOf('a', 'e', 'i', 'o', 'u')
private val specials = hashSetOf("xr", "yt")
private val vowels_y = hashSetOf('a', 'e', 'i', 'o', 'u', 'y')
fun translate(phrase: String): String {
return phrase.split(" ").joinToString(separator = " ") { piggyfy(it) }
}
private fun piggyfy(word: String): String {
if (vowels.contains(word[0]) || specials.contains(word.substring(0, 2))) return word + "ay"
for (pos in 1..word.length) {
val letter = word[pos]
if (vowels_y.contains(letter)) {
val posCalc = if (letter == 'u' && word[pos - 1] == 'q') pos + 1 else pos
return word.substring(posCalc) + word.substring(0, posCalc) + "ay"
}
}
return word
}
}
An object declaration is used to define PigLatin as essentially a singleton object instantiation of the class.
This is sufficient, since there is no object state that needs to change with each call of the translate method.
Three private vals are defined for the HashSets, using the hashSetOf method.
The translate function is implemented by splitting the input String by a space, and then using the joinToString method
with a space as the separator.
The lambda of joinToString uses the it keyword to refer to the single String parameter for the lambda, and passes that to the
piggyfy function.
The translate function returns the rejoined input String as transformed by each word being processed by the piggyfy function.
The piggyfy function takes the input String and checks to see if its first character is in the HashSet of vowels (not including y) or
if the first two characters are in the HashSet of “specials” (e.g. "xr" or "yt").
If the word starts with a vowel or a special two-letter combination, then the function returns the word concatenated with ay at the end.
Otherwise, a range is used to iterate an index for the characters of the word in a for loop,
starting with the second character and up to but not including the length of the word.
A variable is set from the character in the word at the current index of the range.
If the character is in the HashSet of vowels (including y), then the index is adjusted if the current character is a u
and the previous character is a q.
A substring is then taken from the character at the index until the end of the word.
Concatenated to the end of that is a substring from the beginning of the word up to but not including the index.
Finally, concatenated to the end of that is ay.
The concatenated String is returned from the function.
In the event that the word has no vowels and the for loop finishes without returning, the function returns the word as is.
Source: Exercism kotlin/pig-latin