
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find word score from list of words
Suppose we have few words in an array. These words are in lowercase letters. We have to find the total score of these set of words based on following rules −
Consider vowels are [a, e, i, o, u and y]
The score of an individual word is 2 when the word contains an even number of vowels.
Otherwise, the score of that word is 1.
The score for the whole set of words is the sum of scores of all words in the set.
So, if the input is like words = ["programming", "science", "python", "website", "sky"], then the output will be 6 because "programming" has 3 vowels score 1, "science" has three vowels, score 1, "python" has two vowels score 2, "website" has three vowels score 1, "sky" has one vowel score 1, so 1 + 1 + 2 + 1 + 1 = 6.
To solve this, we will follow these steps −
- score := 0
- for each word in words, do
- num_vowels := 0
- for each letter in word, do
- if letter is a vowel, then
- num_vowels := num_vowels + 1
- if letter is a vowel, then
- if num_vowels is even, then
- score := score + 2
- otherwise,
- score := score + 1
- return score
Example
Let us see the following implementation to get better understanding
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))
Input
["programming", "science", "python", "website", "sky"]
Output
6
- Related Articles
- Python Program to return the Length of the Longest Word from the List of Words
- Program to find the largest grouping of anagrams from a word list in Python
- Python program to find the character position of Kth word from a list of strings
- Program to find maximum score from removing stones in Python
- Python program to find average score of each students from dictionary of scores
- Program to find maximum score from performing multiplication operations in Python
- Python program to find uncommon words from two Strings
- Python Program to Replace all words except the given word
- Python Program to Remove the nth Occurrence of the Given Word in a List where Words can Repeat
- Python program to find runner-up score
- Program to find number of subsequence that are present inside word list in python
- Python Program that extract words starting with Vowel From A list
- Program to find the maximum score from all possible valid paths in Python
- Program to find minimum difference of stone games score in Python
- Program to find maximum score of brick removal game in Python
