山羊拉丁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 生日广州男生祝福语简短