Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization for pangram string #2473

Merged
merged 5 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
* [Sum Of Subsets](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sum_of_subsets.py)

## Bit Manipulation
* [Binary And Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_and_operator.py)
* [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py)
* [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py)

## Blockchain
* [Chinese Remainder Theorem](https://github.com/TheAlgorithms/Python/blob/master/blockchain/chinese_remainder_theorem.py)
Expand Down
1 change: 1 addition & 0 deletions bit_manipulation/binary_and_operator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm


def binary_and(a: int, b: int):
"""
Take in 2 integers, convert them to binary,
Expand Down
1 change: 1 addition & 0 deletions bit_manipulation/binary_xor_operator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm


def binary_xor(a: int, b: int):
"""
Take in 2 integers, convert them to binary,
Expand Down
52 changes: 47 additions & 5 deletions strings/check_pangram.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Created by sarathkaul on 12/11/19
"""
wiki: https://en.wikipedia.org/wiki/Pangram
"""


def check_pangram(
Expand All @@ -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(
Expand All @@ -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()