可以使用 Python 处理这个转换,以下是代码实现:
def toGoatLatin(sentence: str) -> str:
vowels = set("aeiouAEIOU")
words = sentence.split()
result = []
for i, word in enumerate(words, start=1):
if word[0] in vowels:
new_word = word + "ma"
else:
new_word = word[1:] + word[0] + "ma"
new_word += "a" * i
result.append(new_word)
return " ".join(result)
# 示例
sentence = "I speak Goat Latin"
print(toGoatLatin(sentence))
这个函数 toGoatLatin
按照规则逐步转换每个单词,并在末尾拼接相应数量的 'a'。你可以尝试输入不同的句子来测试它的效果!