Go•goat-latin/solution.go
package main
import "strings"
func toGoatLatin(sentence string) string {
vowels := "aeiouAEIOU"
words := strings.Fields(sentence)
for index, word := range words {
if !strings.ContainsRune(vowels, rune(word[0])) {
word = word[1:] + word[:1]
}
words[index] = word + "ma" + strings.Repeat("a", index+1)
}
return strings.Join(words, " ")
}
Python•goat-latin/solution.py
class Solution:
def toGoatLatin(self, sentence: str) -> str:
vowels = set(class="syntax-string">"aeiouAEIOU")
result: list[str] = []
for index, word in enumerate(sentence.split(), class="syntax-number">1):
base = word if word[class="syntax-number">0] in vowels else word[class="syntax-number">1:] + word[class="syntax-number">0]
result.append(base + class="syntax-string">"ma" + class="syntax-string">"a" * index)
return class="syntax-string">" ".join(result)
TypeScript•goat-latin/solution.ts
function toGoatLatin(sentence: string): string {
const vowels = new Set(class="syntax-string">"aeiouAEIOU");
return sentence.split(class="syntax-string">" ").map((word, index) => {
const base = vowels.has(word[class="syntax-number">0]) ? word : word.slice(class="syntax-number">1) + word[class="syntax-number">0];
return base + class="syntax-string">"ma" + class="syntax-string">"a".repeat(index + class="syntax-number">1);
}).join(class="syntax-string">" ");
}