-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathik4_count_alphabets.py
54 lines (42 loc) · 1.01 KB
/
ik4_count_alphabets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Count Alphabets
Count the number of alphabets in a given string.
Example One
{
"s": "#aBdj12C"
}
Output:
5
Example Two
{
"s": "123 !@#$"
}
Output:
0
Notes
Constraints:
0 <= length of string <= 104
String contains lower case and upper case English alphabets,
digits, and some special characters('!', '@', '#', '$', '*', ' ') only.
"""
import unittest
def count_alphabets(sentense: str) -> int:
"""
Count Alphabets
"""
alpha_chars = list(filter(lambda ch: ch.isalpha(), sentense))
return len(alpha_chars)
class TestCountAlphabets(unittest.TestCase):
"""
Test count alphabets algorithm
"""
def test_count_alphabets(self) -> None:
"""
Default implementation using isalpha
"""
self.assertEqual(count_alphabets("#aBdj12C"), 5)
self.assertEqual(count_alphabets("123 !@#$"), 0)
self.assertEqual(count_alphabets(" "), 0)
self.assertEqual(count_alphabets("Ran 3 tests in 0.000s"), 11)
if __name__ == "__main__":
unittest.main()