From 8904af98a1e07a1f5b177c2d9f8f042c265e60b1 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Sat, 26 Sep 2020 22:58:59 +0800 Subject: [PATCH] Optimization for pangram string (#2473) * optimization for pangram string * fixup! Format Python code with psf/black push * Update strings/check_pangram.py Co-authored-by: Christian Clauss * updating DIRECTORY.md * Update strings/check_pangram.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss --- strings/check_pangram.py | 52 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/strings/check_pangram.py b/strings/check_pangram.py index 410afd8cc..e3695b918 100644 --- a/strings/check_pangram.py +++ b/strings/check_pangram.py @@ -1,4 +1,6 @@ -# Created by sarathkaul on 12/11/19 +""" +wiki: https://en.wikipedia.org/wiki/Pangram +""" def check_pangram( @@ -8,10 +10,16 @@ def check_pangram( 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( @@ -24,7 +32,41 @@ def check_pangram( return True if len(frequency) == 26 else False -if __name__ == "main": - check_str = "INPUT STRING" - status = check_pangram(check_str) - print(f"{check_str} is {'not ' if status else ''}a pangram string") +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()