2020-09-26 14:58:59 +00:00
|
|
|
"""
|
|
|
|
wiki: https://en.wikipedia.org/wiki/Pangram
|
|
|
|
"""
|
2019-11-14 10:22:08 +00:00
|
|
|
|
|
|
|
|
2020-04-07 12:08:11 +00:00
|
|
|
def check_pangram(
|
2019-11-14 10:22:08 +00:00
|
|
|
input_str: str = "The quick brown fox jumps over the lazy dog",
|
|
|
|
) -> bool:
|
|
|
|
"""
|
2020-04-07 12:08:11 +00:00
|
|
|
A Pangram String contains all the alphabets at least once.
|
|
|
|
>>> check_pangram("The quick brown fox jumps over the lazy dog")
|
2019-11-14 10:22:08 +00:00
|
|
|
True
|
2020-09-26 14:58:59 +00:00
|
|
|
>>> check_pangram("Waltz, bad nymph, for quick jigs vex.")
|
|
|
|
True
|
|
|
|
>>> check_pangram("Jived fox nymph grabs quick waltz.")
|
|
|
|
True
|
2020-04-07 12:08:11 +00:00
|
|
|
>>> check_pangram("My name is Unknown")
|
2019-11-14 10:22:08 +00:00
|
|
|
False
|
2020-04-07 12:08:11 +00:00
|
|
|
>>> check_pangram("The quick brown fox jumps over the la_y dog")
|
2019-11-14 10:22:08 +00:00
|
|
|
False
|
2020-09-26 14:58:59 +00:00
|
|
|
>>> check_pangram()
|
|
|
|
True
|
2019-11-14 10:22:08 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-09-26 14:58:59 +00:00
|
|
|
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
|
2021-05-04 06:49:41 +00:00
|
|
|
>>> check_pangram_faster("Waltz, bad nymph, for quick jigs vex.")
|
2020-09-26 14:58:59 +00:00
|
|
|
True
|
2021-05-04 06:49:41 +00:00
|
|
|
>>> check_pangram_faster("Jived fox nymph grabs quick waltz.")
|
2020-09-26 14:58:59 +00:00
|
|
|
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():
|
2021-05-04 06:49:41 +00:00
|
|
|
flag[ord(char) - 97] = True
|
|
|
|
elif char.isupper():
|
|
|
|
flag[ord(char) - 65] = True
|
2020-09-26 14:58:59 +00:00
|
|
|
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()
|