Python: Find all words in a given string with n consonants
Python Programming Puzzles: Exercise-43 with Solution
From vocabulary.com:
A consonant is a speech sound that is not a vowel. It also refers to letters of the alphabet that represent those sounds: Z, B, T, G, and H are all consonants. Consonants are all the non-vowel sounds, or their corresponding letters: A, E, I, O, U and sometimes Y are not consonants.
Write a Python program to find all words in a given string with n consonants.
Input: this is our time Output: Number of consonants: 3 Words in the said string with 3 consonants: ['this'] Number of consonants: 2 Words in the said string with 2 consonants: ['time'] Number of consonants: 1 Words in the said string with 1 consonants: ['is', 'our']
Pictorial Presentation:

Sample Solution-1:
Python Code:
#License: https://bit.ly/3oLErEI
def test(strs, n):
return [w for w in strs.split() if sum([c not in "aeiou" for c in w.lower()]) == n]
strs = "this is our time"
print("Original string:",strs)
n = 3
print("Number of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 2
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 1
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
Sample Output:
Original string: this is our time Number of consonants: 3 Words in the said string with 3 consonants: ['this'] Number of consonants: 2 Words in the said string with 2 consonants: ['time'] Number of consonants: 1 Words in the said string with 1 consonants: ['is', 'our']
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Sample Solution-2:
Python Code:
def test(strs, n):
return [w for w in strs.split() if sum(c.lower() not in "aeiou" for c in w) == n]
strs = "this is our time"
print("Original string:",strs)
n = 3
print("Number of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 2
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
n = 1
print("\nNumber of consonants:",n)
print("Words in the said string with",n,"consonants:")
print(test(strs, n))
Sample Output:
Original string: this is our time Number of consonants: 3 Words in the said string with 3 consonants: ['this'] Number of consonants: 2 Words in the said string with 2 consonants: ['time'] Number of consonants: 1 Words in the said string with 1 consonants: ['is', 'our']
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the set of distinct characters in a string, ignoring case.
Next: Determine which characters of a hexadecimal number correspond to prime numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value:
Example:
def tips_map_dictionary(itr, fn): ret = {} for a in itr: ret[a] = fn(a) return ret print(tips_map_dictionary([2,4,6], lambda a: a * a))
Output:
{2: 4, 4: 16, 6: 36}
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook