Skip to content

Commit 752431f

Browse files
bhatiakashvi16pre-commit-ci[bot]cclauss
authored
Add algorithm to count vowels and consonants in a string (#14038)
* Refine Wheatstone Bridge explanation Updated docstring to provide a clearer explanation of the Wheatstone Bridge, its balance condition, and applications. * Update wheatstone_bridge.py * Add algorithm to count vowels and consonants in a string * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove test print statement from main section Removed test print statement for count_vowels_and_consonants function. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Enhance docstring with usage examples Added examples to the docstring for count_vowels_and_consonants function. * Apply suggestion from @cclauss --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 7691204 commit 752431f

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎strings/count_vowels_consonants.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def count_vowels_and_consonants(text: str) -> tuple[int, int]:
2+
"""
3+
Count the number of vowels and consonants in a given string.
4+
5+
This function ignores non-alphabetic characters.
6+
7+
Args:
8+
text (str): Input string.
9+
10+
Returns:
11+
tuple[int, int]: A tuple containing (vowel_count,
12+
consonant_count).
13+
Examples:
14+
>>> count_vowels_and_consonants("Hello World")
15+
(3, 7)
16+
>>> count_vowels_and_consonants("AEIOU")
17+
(5, 0)
18+
>>> count_vowels_and_consonants("bcdf")
19+
(0, 4)
20+
>>> count_vowels_and_consonants("")
21+
(0, 0)
22+
"""
23+
vowels = set("aeiouAEIOU")
24+
vowel_count = 0
25+
consonant_count = 0
26+
27+
for char in text:
28+
if char.isalpha():
29+
if char in vowels:
30+
vowel_count += 1
31+
else:
32+
consonant_count += 1
33+
34+
return vowel_count, consonant_count

0 commit comments

Comments
 (0)