Skip to content

Commit 634828d

Browse files
committed
Add string_statistics algorithms.
1 parent 678dedb commit 634828d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

strings/string_statistics.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def string_statistics(text: str) -> dict[str, int]:
2+
"""
3+
Return statistics about a string.
4+
5+
>>> string_statistics("Hello")
6+
{'length': 5, 'vowels': 2, 'consonants': 3}
7+
8+
>>> string_statistics("")
9+
{'length': 0, 'vowels': 0, 'consonants': 0}
10+
"""
11+
vowels = "aeiouAEIOU"
12+
13+
length = len(text)
14+
vowel_count = sum(1 for char in text if char in vowels)
15+
consonant_count = sum(1 for char in text if char.isalpha() and char not in vowels)
16+
17+
return {
18+
"length": length,
19+
"vowels": vowel_count,
20+
"consonants": consonant_count,
21+
}
22+
23+
24+
if __name__ == "__main__":
25+
from doctest import testmod
26+
testmod()

0 commit comments

Comments
 (0)