Skip to content

Commit c76d955

Browse files
its-100rabhpre-commit-ci[bot]
authored andcommitted
Create count_vowels.py (TheAlgorithms#11474)
* Create count_vowels.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent bb16071 commit c76d955

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

strings/count_vowels.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def count_vowels(s: str) -> int:
2+
"""
3+
Count the number of vowels in a given string.
4+
5+
:param s: Input string to count vowels in.
6+
:return: Number of vowels in the input string.
7+
8+
Examples:
9+
>>> count_vowels("hello world")
10+
3
11+
>>> count_vowels("HELLO WORLD")
12+
3
13+
>>> count_vowels("123 hello world")
14+
3
15+
>>> count_vowels("")
16+
0
17+
>>> count_vowels("a quick brown fox")
18+
5
19+
>>> count_vowels("the quick BROWN fox")
20+
5
21+
>>> count_vowels("PYTHON")
22+
1
23+
"""
24+
if not isinstance(s, str):
25+
raise ValueError("Input must be a string")
26+
27+
vowels = "aeiouAEIOU"
28+
return sum(1 for char in s if char in vowels)
29+
30+
31+
if __name__ == "__main__":
32+
from doctest import testmod
33+
34+
testmod()

0 commit comments

Comments
 (0)