exercism/go/pig-latin/igpay.go
2016-08-23 15:47:19 -05:00

42 lines
932 B
Go

package igpay
import "strings"
// PigLatin takes some words and converts them to pig latin
// 1) Move starting consonant sounds to the end of the word
// 2) add 'ay'
func PigLatin(in string) string {
var ret string
words := strings.Split(in, " ")
for i := range words {
ret += pigLatinWord(words[i])
if i < len(words)-1 {
ret += " "
}
}
return ret
}
// pigLatinWord actually does the heavy lifting
func pigLatinWord(in string) string {
var ret string
// First move all consonant sounds to the end
for i := 0; i < len(in); i++ {
// Edge consonant sound cases
if in[0] == 'q' && in[1] == 'u' {
in = in[2:] + string(in[0]) + string(in[1])
}
vowelPos := strings.IndexAny(in, "aeiou")
// If we have a y and another vowel, then y is a consonant
// also, x
if vowelPos == 0 || (vowelPos > 1 && (in[0] == 'y' || in[0] == 'x')) {
return in + "ay"
}
in = in[1:] + string(in[0])
}
return ret
}