本题注意点:字符串处理技巧,另见 https://blog.youkuaiyun.com/zuocuomiao/article/details/82146577
'a'*3可以得到'aaa'; join()可以实现字符串数组拼接成一个字符串。
import numpy as np
def toGoatLatin(S):
"""
:type S: str
:rtype: str
"""
ans = []
vowel = set('aeiouAEIOU')
S = S.split(' ')
for i, x in enumerate(S):
if x[0] not in vowel:
x = x[1:]+x[0]
x += 'ma'
x += 'a' * (i+1)
ans.append(x)
return ' '.join(word for word in ans).strip()
# samples:
S = "I speak Goat Latin"
print(toGoatLatin(S))
S = "The quick brown fox jumped over the lazy dog"
print(toGoatLatin(S))