From c527c0828db3c51c374b402a52149dfc952a30ec Mon Sep 17 00:00:00 2001 From: SwayamSahu <91021799+SwayamSahu@users.noreply.github.com> Date: Sun, 23 Oct 2022 20:21:57 +0530 Subject: [PATCH 1/4] Update comments in check_pangram.py script --- strings/check_pangram.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/strings/check_pangram.py b/strings/check_pangram.py index 81384bfd4cc6..ccfb032e1f9d 100644 --- a/strings/check_pangram.py +++ b/strings/check_pangram.py @@ -21,7 +21,8 @@ def check_pangram( >>> check_pangram() True """ - frequency = set() + frequency = set() # Declaring frequency as Set to have unique occurances of alphabets + input_str = input_str.replace( " ", "" ) # Replacing all the Whitespaces in our sentence From d860d57946428cc93181918cbc82023cdbd894eb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Oct 2022 14:54:19 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/check_pangram.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/strings/check_pangram.py b/strings/check_pangram.py index ccfb032e1f9d..73e934d03238 100644 --- a/strings/check_pangram.py +++ b/strings/check_pangram.py @@ -21,8 +21,10 @@ def check_pangram( >>> check_pangram() True """ - frequency = set() # Declaring frequency as Set to have unique occurances of alphabets - + frequency = ( + set() + ) # Declaring frequency as Set to have unique occurances of alphabets + input_str = input_str.replace( " ", "" ) # Replacing all the Whitespaces in our sentence From ba172e4d2121bec77e0293be1832f99db1624eb3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 23 Oct 2022 17:56:25 +0200 Subject: [PATCH 3/4] Update and rename check_pangram.py to is_pangram.py --- strings/check_pangram.py | 77 ------------------------------- strings/is_pangram.py | 97 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 77 deletions(-) delete mode 100644 strings/check_pangram.py create mode 100644 strings/is_pangram.py diff --git a/strings/check_pangram.py b/strings/check_pangram.py deleted file mode 100644 index 73e934d03238..000000000000 --- a/strings/check_pangram.py +++ /dev/null @@ -1,77 +0,0 @@ -""" -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() - ) # Declaring frequency as Set to have unique occurances of alphabets - - 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_faster("Waltz, bad nymph, for quick jigs vex.") - True - >>> check_pangram_faster("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) - 97] = True - elif char.isupper(): - flag[ord(char) - 65] = 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() diff --git a/strings/is_pangram.py b/strings/is_pangram.py new file mode 100644 index 000000000000..d55ffcde7fd9 --- /dev/null +++ b/strings/is_pangram.py @@ -0,0 +1,97 @@ +""" +wiki: https://en.wikipedia.org/wiki/Pangram +""" + + +def is_pangram( + input_str: str = "The quick brown fox jumps over the lazy dog", +) -> bool: + """ + A Pangram String contains all the alphabets at least once. + >>> is_pangram("The quick brown fox jumps over the lazy dog") + True + >>> is_pangram("Waltz, bad nymph, for quick jigs vex.") + True + >>> is_pangram("Jived fox nymph grabs quick waltz.") + True + >>> is_pangram("My name is Unknown") + False + >>> is_pangram("The quick brown fox jumps over the la_y dog") + False + >>> is_pangram() + True + """ + # Declare frequency as a set to have unique occurrences of letters + frequency = (set()) + + # Replace all the whitespace in our sentence + input_str = input_str.replace(" ", "") + for alpha in input_str: + if "a" <= alpha.lower() <= "z": + frequency.add(alpha.lower()) + return len(frequency) == 26 + + +def is_pangram_faster( + input_str: str = "The quick brown fox jumps over the lazy dog", +) -> bool: + """ + >>> is_pangram_faster("The quick brown fox jumps over the lazy dog") + True + >>> is_pangram_faster("Waltz, bad nymph, for quick jigs vex.") + True + >>> is_pangram_faster("Jived fox nymph grabs quick waltz.") + True + >>> is_pangram_faster("The quick brown fox jumps over the la_y dog") + False + >>> is_pangram_faster() + True + """ + flag = [False] * 26 + for char in input_str: + if char.islower(): + flag[ord(char) - 97] = True + elif char.isupper(): + flag[ord(char) - 65] = True + return all(flag) + + +def is_pangram_fastest( + input_str: str = "The quick brown fox jumps over the lazy dog", +) -> bool: + """ + >>> is_pangram_fastest("The quick brown fox jumps over the lazy dog") + True + >>> is_pangram_fastest("Waltz, bad nymph, for quick jigs vex.") + True + >>> is_pangram_fastest("Jived fox nymph grabs quick waltz.") + True + >>> is_pangram_fastest("The quick brown fox jumps over the la_y dog") + False + >>> is_pangram_fastest() + True + """ + return len({char for char in input_str.lower() if char.isalpha()}) == 26 + + +def benchmark() -> None: + """ + Benchmark code comparing different version. + """ + from timeit import timeit + + setup = ( + "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest" + ) + print(timeit("is_pangram()", setup=setup)) + print(timeit("is_pangram_faster()", setup=setup)) + print(timeit("is_pangram_fastest()", setup=setup)) + # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 + # 5.036091582966037, 2.644472333951853, 1.8869528750656173 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + benchmark() From cc934e411c2bbb935c20b57c0e7b6843e3ce5b50 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Oct 2022 15:57:25 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/is_pangram.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/strings/is_pangram.py b/strings/is_pangram.py index d55ffcde7fd9..c8b894b7ea31 100644 --- a/strings/is_pangram.py +++ b/strings/is_pangram.py @@ -22,7 +22,7 @@ def is_pangram( True """ # Declare frequency as a set to have unique occurrences of letters - frequency = (set()) + frequency = set() # Replace all the whitespace in our sentence input_str = input_str.replace(" ", "") @@ -80,9 +80,7 @@ def benchmark() -> None: """ from timeit import timeit - setup = ( - "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest" - ) + setup = "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest" print(timeit("is_pangram()", setup=setup)) print(timeit("is_pangram_faster()", setup=setup)) print(timeit("is_pangram_fastest()", setup=setup))