元音计数顺序 [英] Sequence of vowels count
问题描述
这不是家庭作业问题,是考试准备问题.
This is not a homework question, it is an exam preparation question.
我应该定义一个函数syllables(word)
来计算其中的音节数量
用以下方式一个词:
I should define a function syllables(word)
that counts the number of syllables in
A word in the following way:
•最大的元音序列是一个音节;
• a maximal sequence of vowels is a syllable;
•单词中的最后一个 e 不是一个音节(或者它是元音序列的一部分) 的.
• a final e in a word is not a syllable (or the vowel sequence it is a part Of).
我不必处理任何特殊情况,例如 一个音节的单词(例如"be"或"bee").
I do not have to deal with any special cases, such as a final e in a One-syllable word (e.g., ’be’ or ’bee’).
>>> syllables(’honour’)
2
>>> syllables(’decode’)
2
>>> syllables(’oiseau’)
2
我应该在这里使用正则表达式还是只是列表理解?
Should I use regular expression here or just list comprehension ?
推荐答案
我发现此问题很自然的正则表达式. (我认为,非正则表达式的答案将需要更多的编码.我使用两种字符串方法("lower"和"endswith"来使答案更清晰).
I find regular expressions natural for this question. (I think a non-regex answer would take more coding. I use two string methods, 'lower' and 'endswith' to make the answer more clear.)
import re
def syllables(word):
word = word.lower()
if word.endswith('e'):
word = word[:-1]
count = len(re.findall('[aeiou]+', word))
return count
for word in ('honour', 'decode', 'decodes', 'oiseau', 'pie'):
print word, syllables(word)
哪些印刷品:
honour 2
decode 2
decodes 3
oiseau 2
pie 1
请注意,解码"的音节比解码"多(这很奇怪,但符合您的定义).
Note that 'decodes' has one more syllable than 'decode' (which is strange, but fits your definition).
问题.这对您有什么帮助?学习问题不是您自己亲自研究的重点吗?通过在问题中进行失败的尝试,将来可能会获得更多好处,因此您可以确切地了解自己所缺少的地方.
Question. How does this help you? Isn't the point of the study question that you work through it yourself? You may get more benefit in the future by posting a failed attempt in your question, so you can learn exactly where you are lacking.
这篇关于元音计数顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!