山羊拉丁Python
假设我们有一组字符串(句子),在该字符串集中只有很少的单词。每个单词都由小写字母和大写字母组成。我们的任务是将句子转换为Goat-Latin形式。山羊拉丁类似于猪拉丁。有一些条件。
如果单词以元音开头,则在单词后附加“ma”
该词以辅音开头,然后从头开始将其删除,并在末尾附加,然后在末尾添加“ma”。
在句子中按单词索引在每个单词的末尾添加一个字母'a',从1开始
因此,如果示例如“亚当想上大学”,则转换后的字符串将为“Adammaaantswmaaaotmaaaaogmaaaaaotmaaaaaahetmaaaaaaauniversitymaaaaaaaa”
为了解决这个问题,任务很简单,将句子分解成单词,然后针对每个单词,检查给定条件并执行上述操作。
示例
让我们看下面的实现以更好地理解-
class Solution:
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
temp = S.split(" ")
counter = 1
result = []
vowel = {"a","e","i","o","u"}
for i in temp:
if i[0].lower() in vowel:
x = i + "ma" + ("a"*counter)
else:
x=i[1:]+i[0] + "ma" +("a"*counter)
counter+=1
result.append(x)
return " ".join(c for c in result)
ob1 = Solution()print(ob1.toGoatLatin("Adam wants to go to the university"))输入值
"Adam wants to go to the university"
输出结果
"Adammaa antswmaaa otmaaaa ogmaaaaa otmaaaaaa hetmaaaaaaa universitymaaaaaaaa"
热门推荐
10 冬季最美的祝福语简短
11 结婚婚宴圆满祝福语简短
12 过满月祝福语女生简短
13 跟女生道别祝福语简短
14 撩妹生日祝福语简短
15 朋友妹妹高考祝福语简短
16 新年元旦祝福语大全简短
17 搬家祝福语卡片文案简短
18 重逢时的祝福语简短