访问量: 10 次浏览
假设我们有一个包含几个单词的数组。这些单词都是小写字母。根据以下规则,我们必须找到这些单词集的总分数:
因此,如果输入为 words = [“programming”, “science”, “python”, “website”, “sky”],则输出将为6,因为”programming”有3个元音字母得分为1,”science”有三个元音字母得分为1,”Python”有两个元音字母得分为2,”website”有三个元音字母得分为1,”sky”有一个元音字母得分为1,所以1 + 1 + 2 + 1 + 1 = 6。
为了解决这个问题,我们将遵循以下步骤−
让我们看一下以下实现,以获得更好的理解。
def solve(words):
score = 0
for word in words:
num_vowels = 0
for letter in word:
if letter in ['a', 'e', 'i', 'o', 'u', 'y']:
num_vowels += 1
if num_vowels % 2 == 0:
score += 2
else:
score +=1
return score
words = ["programming", "science", "python", "website", "sky"]
print(solve(words))
["programming", "science", "python", "website", "sky"]
6