山羊拉丁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 生日开业蛋糕祝福语简短