Skip to content

Commit 0facd8b

Browse files
authored
refactor: simplify character filtering in is_pangram
Refactored is_pangram function to use isalpha for letter check and removed benchmark function.
1 parent 678dedb commit 0facd8b

File tree

1 file changed

+4
-24
lines changed

1 file changed

+4
-24
lines changed

strings/is_pangram.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ def is_pangram(
2121
>>> is_pangram()
2222
True
2323
"""
24-
# Declare frequency as a set to have unique occurrences of letters
2524
frequency = set()
26-
27-
# Replace all the whitespace in our sentence
2825
input_str = input_str.replace(" ", "")
2926
for alpha in input_str:
30-
if "a" <= alpha.lower() <= "z":
27+
if alpha.isalpha():
3128
frequency.add(alpha.lower())
3229
return len(frequency) == 26
3330

@@ -73,23 +70,6 @@ def is_pangram_fastest(
7370
"""
7471
return len({char for char in input_str.lower() if char.isalpha()}) == 26
7572

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()
73+
74+
75+

0 commit comments

Comments
 (0)