forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_pangram.py
72 lines (61 loc) · 1.87 KB
/
check_pangram.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
wiki: https://en.wikipedia.org/wiki/Pangram
"""
def check_pangram(
input_str: str = "The quick brown fox jumps over the lazy dog",
) -> bool:
"""
A Pangram String contains all the alphabets at least once.
>>> check_pangram("The quick brown fox jumps over the lazy dog")
True
>>> check_pangram("Waltz, bad nymph, for quick jigs vex.")
True
>>> check_pangram("Jived fox nymph grabs quick waltz.")
True
>>> check_pangram("My name is Unknown")
False
>>> check_pangram("The quick brown fox jumps over the la_y dog")
False
>>> check_pangram()
True
"""
frequency = set()
input_str = input_str.replace(
" ", ""
) # Replacing all the Whitespaces in our sentence
for alpha in input_str:
if "a" <= alpha.lower() <= "z":
frequency.add(alpha.lower())
return True if len(frequency) == 26 else False
def check_pangram_faster(
input_str: str = "The quick brown fox jumps over the lazy dog",
) -> bool:
"""
>>> check_pangram_faster("The quick brown fox jumps over the lazy dog")
True
>>> check_pangram("Waltz, bad nymph, for quick jigs vex.")
True
>>> check_pangram("Jived fox nymph grabs quick waltz.")
True
>>> check_pangram_faster("The quick brown fox jumps over the la_y dog")
False
>>> check_pangram_faster()
True
"""
flag = [False] * 26
for char in input_str:
if char.islower():
flag[ord(char) - ord("a")] = True
return all(flag)
def benchmark() -> None:
"""
Benchmark code comparing different version.
"""
from timeit import timeit
setup = "from __main__ import check_pangram, check_pangram_faster"
print(timeit("check_pangram()", setup=setup))
print(timeit("check_pangram_faster()", setup=setup))
if __name__ == "__main__":
import doctest
doctest.testmod()
benchmark()