gpt4 book ai didi

python - 包含至少三个连续元音的序列

转载 作者:行者123 更新时间:2023-11-28 17:03:23 26 4
gpt4 key购买 nike

我正在尝试创建一个函数来评估是否至少连续包含三个元音。

到目前为止我已经尝试过:(我不知道如何评估它们是否连续)有什么想法吗?

def isConsecutive(word):
# initialize vowel count
vCounter = 0
for letter in word:
if letter == isVowel(word):
vCounter += 1
else:
vCounter = 0

if vCounter < 3:
return False
return True

辅助函数

def isVowel(char):
return len(char) == 1 and char.lower() in 'aeiou'

最佳答案

检查您是否按顺序到达了第三个vovel,应该在vCounter += 1 之后。如果有三个vovels:返回true。

此外,isVowel 检查应该应用于 letter,而不是整个 word

def isVowel(char):
return char.lower() in 'aeiou'

def isConsecutive(word):
# initialize vowel count
vCounter = 0
for letter in word:
if isVowel(letter): # <= check if the letter is a vowel
vCounter += 1
if vCounter >= 3: # <= do the check right here
return True
else:
vCounter = 0

return False # <= if we did not find three vovels in the loop, then there is none

print "hello: " + str(isConsecutive("hello"))
print "heeello: " + str(isConsecutive("heeello"))
print "hellooo: " + str(isConsecutive("hellooo"))

在线试一试: DEMO

关于python - 包含至少三个连续元音的序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52809729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com