TheAlgorithms-Python

Форк
0
95 строк · 2.7 Кб
1
"""
2
wiki: https://en.wikipedia.org/wiki/Pangram
3
"""
4

5

6
def is_pangram(
7
    input_str: str = "The quick brown fox jumps over the lazy dog",
8
) -> bool:
9
    """
10
    A Pangram String contains all the alphabets at least once.
11
    >>> is_pangram("The quick brown fox jumps over the lazy dog")
12
    True
13
    >>> is_pangram("Waltz, bad nymph, for quick jigs vex.")
14
    True
15
    >>> is_pangram("Jived fox nymph grabs quick waltz.")
16
    True
17
    >>> is_pangram("My name is Unknown")
18
    False
19
    >>> is_pangram("The quick brown fox jumps over the la_y dog")
20
    False
21
    >>> is_pangram()
22
    True
23
    """
24
    # Declare frequency as a set to have unique occurrences of letters
25
    frequency = set()
26

27
    # Replace all the whitespace in our sentence
28
    input_str = input_str.replace(" ", "")
29
    for alpha in input_str:
30
        if "a" <= alpha.lower() <= "z":
31
            frequency.add(alpha.lower())
32
    return len(frequency) == 26
33

34

35
def is_pangram_faster(
36
    input_str: str = "The quick brown fox jumps over the lazy dog",
37
) -> bool:
38
    """
39
    >>> is_pangram_faster("The quick brown fox jumps over the lazy dog")
40
    True
41
    >>> is_pangram_faster("Waltz, bad nymph, for quick jigs vex.")
42
    True
43
    >>> is_pangram_faster("Jived fox nymph grabs quick waltz.")
44
    True
45
    >>> is_pangram_faster("The quick brown fox jumps over the la_y dog")
46
    False
47
    >>> is_pangram_faster()
48
    True
49
    """
50
    flag = [False] * 26
51
    for char in input_str:
52
        if char.islower():
53
            flag[ord(char) - 97] = True
54
        elif char.isupper():
55
            flag[ord(char) - 65] = True
56
    return all(flag)
57

58

59
def is_pangram_fastest(
60
    input_str: str = "The quick brown fox jumps over the lazy dog",
61
) -> bool:
62
    """
63
    >>> is_pangram_fastest("The quick brown fox jumps over the lazy dog")
64
    True
65
    >>> is_pangram_fastest("Waltz, bad nymph, for quick jigs vex.")
66
    True
67
    >>> is_pangram_fastest("Jived fox nymph grabs quick waltz.")
68
    True
69
    >>> is_pangram_fastest("The quick brown fox jumps over the la_y dog")
70
    False
71
    >>> is_pangram_fastest()
72
    True
73
    """
74
    return len({char for char in input_str.lower() if char.isalpha()}) == 26
75

76

77
def benchmark() -> None:
78
    """
79
    Benchmark code comparing different version.
80
    """
81
    from timeit import timeit
82

83
    setup = "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest"
84
    print(timeit("is_pangram()", setup=setup))
85
    print(timeit("is_pangram_faster()", setup=setup))
86
    print(timeit("is_pangram_fastest()", setup=setup))
87
    # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
88
    # 5.036091582966037, 2.644472333951853,  1.8869528750656173
89

90

91
if __name__ == "__main__":
92
    import doctest
93

94
    doctest.testmod()
95
    benchmark()
96

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.