山羊拉丁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 好听的元旦简短祝福语