-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path824. Goat Latin.py
More file actions
61 lines (43 loc) · 1.85 KB
/
824. Goat Latin.py
File metadata and controls
61 lines (43 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。
我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。
山羊拉丁文的规则如下:
如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
例如,单词"apple"变为"applema"。
如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
例如,单词"goat"变为"oatgma"。
根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。
例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。
返回将 S 转换为山羊拉丁文后的句子。
示例 1:
输入: "I speak Goat Latin"
输出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
示例 2:
输入: "The quick brown fox jumped over the lazy dog"
输出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
说明:
S 中仅包含大小写字母和空格。单词间有且仅有一个空格。
1 <= S.length <= 150。
"""
class Solution(object):
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
s_list = S.split(" ")
result = [self.make_goat_latin(s_list[i], i + 1) for i in range(len(s_list))]
return " ".join(result)
def make_goat_latin(self, word, i):
print i
vowel = ["a", "e", "i", "o", "u"]
if list(word)[0].lower() in vowel:
ret_info = word + "ma"
else:
ret_info = word[1:] + word[0] + "ma"
return ret_info + "a" * i
if __name__ == '__main__':
test = "I speak Goat Latin"
print Solution().toGoatLatin(test)