山羊拉丁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 写给男生简短的祝福语