From fcc64604d389233b78713493d8f913128a4b38e8 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Thu, 6 Oct 2022 19:22:06 +0200 Subject: [PATCH 01/13] Update README.md Added Google Cirq references --- quantum/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/quantum/README.md b/quantum/README.md index 423d34fa3364..3ce364574486 100644 --- a/quantum/README.md +++ b/quantum/README.md @@ -6,6 +6,7 @@ Started at https://github.com/TheAlgorithms/Python/issues/1831 * Google: https://research.google/teams/applied-science/quantum * IBM: https://qiskit.org and https://github.com/Qiskit * Rigetti: https://rigetti.com and https://github.com/rigetti +* Zapata: https://www.zapatacomputing.com and https://github.com/zapatacomputing ## IBM Qiskit - Start using by installing `pip install qiskit`, refer the [docs](https://qiskit.org/documentation/install.html) for more info. @@ -13,3 +14,10 @@ Started at https://github.com/TheAlgorithms/Python/issues/1831 - https://github.com/Qiskit/qiskit-tutorials - https://quantum-computing.ibm.com/docs/iql/first-circuit - https://medium.com/qiskit/how-to-program-a-quantum-computer-982a9329ed02 + +## Google Cirq +- Start using by installing `python -m pip install cirq`, refer the [docs](https://quantumai.google/cirq/start/install) for more info. +- Tutorials & references + - https://github.com/quantumlib/cirq + - https://quantumai.google/cirq/experiments + - https://tanishabassan.medium.com/quantum-programming-with-google-cirq-3209805279bc From fe80790786580c860380ef71eaffa78c82175393 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Fri, 7 Oct 2022 13:11:53 +0200 Subject: [PATCH 02/13] Create barcode_validator.py Barcode/EAN validator --- strings/barcode_validator.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 strings/barcode_validator.py diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py new file mode 100644 index 000000000000..fcf40446aef5 --- /dev/null +++ b/strings/barcode_validator.py @@ -0,0 +1,26 @@ +def evalKey(code: int): + code //= 10 # exclude the last digit + checker = False + s = 0 + + # extract and check each digit + while code != 0: + mult = 1 if checker else 3 + s += mult * (code % 10) + code //= 10 + checker = not checker + + return (10 - (s % 10)) % 10 + + +def isValid(code: int): + return len(str(code)) == 13 and evalKey(code) == code % 10 + + +if __name__ == '__main__': + barcode = input('Enter a barcode: ') + number_barcode = int(barcode) + if isValid(number_barcode): + print(f'|{n}| is a valid Barcode') + else: + print(f'|{n}| is NOT is valid Barcode.') From 775e9f77db13f7cdc5c187591020d7d1ca8fd75a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Oct 2022 11:12:57 +0000 Subject: [PATCH 03/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/barcode_validator.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index fcf40446aef5..928fe631699e 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -2,7 +2,7 @@ def evalKey(code: int): code //= 10 # exclude the last digit checker = False s = 0 - + # extract and check each digit while code != 0: mult = 1 if checker else 3 @@ -17,10 +17,10 @@ def isValid(code: int): return len(str(code)) == 13 and evalKey(code) == code % 10 -if __name__ == '__main__': - barcode = input('Enter a barcode: ') +if __name__ == "__main__": + barcode = input("Enter a barcode: ") number_barcode = int(barcode) if isValid(number_barcode): - print(f'|{n}| is a valid Barcode') + print(f"|{n}| is a valid Barcode") else: - print(f'|{n}| is NOT is valid Barcode.') + print(f"|{n}| is NOT is valid Barcode.") From bf6657963542bfe864ae4cfc9d4d032b6138d84d Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Fri, 7 Oct 2022 13:31:30 +0200 Subject: [PATCH 04/13] Update barcode_validator.py Included docstring and updated variables to snake_case --- strings/barcode_validator.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index 928fe631699e..214e03e6cffd 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -1,4 +1,9 @@ -def evalKey(code: int): +def eval_key(code: int): + ''' + Returns the last digit of barcode by excluding the last digit first + and then computing to reach the actual last digit from the remaining + 12 digits. + ''' code //= 10 # exclude the last digit checker = False s = 0 @@ -13,14 +18,22 @@ def evalKey(code: int): return (10 - (s % 10)) % 10 -def isValid(code: int): - return len(str(code)) == 13 and evalKey(code) == code % 10 +def is_valid(code: int): + ''' + Checks for length of barcode and last-digit + Returns boolean value of validity of barcode + ''' + return len(str(code)) == 13 and eval_key(code) == code % 10 if __name__ == "__main__": + ''' + Enter a barcode. + Displays whether the entered barcode is valid or invalid. + ''' barcode = input("Enter a barcode: ") number_barcode = int(barcode) - if isValid(number_barcode): + if is_valid(number_barcode): print(f"|{n}| is a valid Barcode") else: print(f"|{n}| is NOT is valid Barcode.") From 9bdb492104c18c101eaf246de2905075507e71c4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Oct 2022 11:32:23 +0000 Subject: [PATCH 05/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/barcode_validator.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index 214e03e6cffd..1c572d34b93d 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -1,9 +1,9 @@ def eval_key(code: int): - ''' + """ Returns the last digit of barcode by excluding the last digit first and then computing to reach the actual last digit from the remaining 12 digits. - ''' + """ code //= 10 # exclude the last digit checker = False s = 0 @@ -19,18 +19,18 @@ def eval_key(code: int): def is_valid(code: int): - ''' + """ Checks for length of barcode and last-digit Returns boolean value of validity of barcode - ''' + """ return len(str(code)) == 13 and eval_key(code) == code % 10 if __name__ == "__main__": - ''' + """ Enter a barcode. Displays whether the entered barcode is valid or invalid. - ''' + """ barcode = input("Enter a barcode: ") number_barcode = int(barcode) if is_valid(number_barcode): From 57247b683d1fd54e2d1d4ef2691273e23e08c1c7 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Fri, 7 Oct 2022 14:36:19 +0200 Subject: [PATCH 06/13] Update barcode_validator.py Included docset and updated bugs --- strings/barcode_validator.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index 1c572d34b93d..5ed9d93edaf1 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -1,9 +1,13 @@ -def eval_key(code: int): +def eval_key(code: int) -> int: """ Returns the last digit of barcode by excluding the last digit first and then computing to reach the actual last digit from the remaining 12 digits. + + >>> eval_key(8718452538119) + 9 """ + code //= 10 # exclude the last digit checker = False s = 0 @@ -18,15 +22,25 @@ def eval_key(code: int): return (10 - (s % 10)) % 10 -def is_valid(code: int): +def is_valid(code: int) -> bool: """ Checks for length of barcode and last-digit Returns boolean value of validity of barcode + + >>> is_valid(8718452538119) + True + + >>> is_valid(87184525) + False + """ + return len(str(code)) == 13 and eval_key(code) == code % 10 if __name__ == "__main__": + import doctest + doctest.testmod() """ Enter a barcode. Displays whether the entered barcode is valid or invalid. @@ -34,6 +48,6 @@ def is_valid(code: int): barcode = input("Enter a barcode: ") number_barcode = int(barcode) if is_valid(number_barcode): - print(f"|{n}| is a valid Barcode") + print(f"|{number_barcode}| is a valid Barcode") else: - print(f"|{n}| is NOT is valid Barcode.") + print(f"|{number_barcode}| is NOT is valid Barcode.") From a4019a5bf843af309113b7b91e00966205e5a94d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Oct 2022 12:37:13 +0000 Subject: [PATCH 07/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/barcode_validator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index 5ed9d93edaf1..d9fd8bdebda0 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -40,6 +40,7 @@ def is_valid(code: int) -> bool: if __name__ == "__main__": import doctest + doctest.testmod() """ Enter a barcode. From 947fa4abddb62a2f2584d97a4dbdc0385e10c12b Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Wed, 12 Oct 2022 22:26:45 +0200 Subject: [PATCH 08/13] Update barcode_validator.py Implemented the changes asked in review. --- strings/barcode_validator.py | 62 +++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index d9fd8bdebda0..79b4c9e5f309 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -1,28 +1,29 @@ -def eval_key(code: int) -> int: +def get_check_digit(barcode: int) -> int: """ Returns the last digit of barcode by excluding the last digit first and then computing to reach the actual last digit from the remaining 12 digits. - >>> eval_key(8718452538119) + >>> get_check_digit(8718452538119) 9 + """ - code //= 10 # exclude the last digit + barcode //= 10 # exclude the last digit checker = False s = 0 # extract and check each digit - while code != 0: + while barcode != 0: mult = 1 if checker else 3 - s += mult * (code % 10) - code //= 10 + s += mult * (barcode % 10) + barcode //= 10 checker = not checker return (10 - (s % 10)) % 10 -def is_valid(code: int) -> bool: +def is_valid(barcode: int) -> bool: """ Checks for length of barcode and last-digit Returns boolean value of validity of barcode @@ -33,10 +34,41 @@ def is_valid(code: int) -> bool: >>> is_valid(87184525) False + >>> is_valid(87193425381089) + False + + >>> is_valid(0) + False + + >>> is_valid(dwefgiweuf) + Traceback (most recent call last): + ... + NameError: name 'dwefgiweuf' is not defined """ - return len(str(code)) == 13 and eval_key(code) == code % 10 + return len(str(barcode)) == 13 and get_check_digit(barcode) == barcode % 10 + + +def get_barcode(barcode: str) -> int: + """ + Returns the barcode as an integer + >>> get_barcode("8718452538119") + 8718452538119 + + >>> get_barcode("dwefgiweuf") + Traceback (most recent call last): + ... + ValueError: Barcode 'dwefgiweuf' has alphabets in it + """ + if str(barcode).isalpha(): + raise ValueError("Barcode '{}' has alphabets in it".format(barcode)) + + if int(barcode)<0: + raise ValueError("The entered barcode has negative values. Try again.") + + else: + return int(barcode) if __name__ == "__main__": import doctest @@ -44,11 +76,11 @@ def is_valid(code: int) -> bool: doctest.testmod() """ Enter a barcode. - Displays whether the entered barcode is valid or invalid. - """ - barcode = input("Enter a barcode: ") - number_barcode = int(barcode) - if is_valid(number_barcode): - print(f"|{number_barcode}| is a valid Barcode") + + """ + barcode = get_barcode((input("Barcode: ").strip())) + + if is_valid(barcode): + print(f"|{barcode}| is a valid Barcode") else: - print(f"|{number_barcode}| is NOT is valid Barcode.") + print(f"|{barcode}| is NOT is valid Barcode.") From e11313a3faa84d03a4a97e191b552b4f67b5b841 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Oct 2022 20:28:17 +0000 Subject: [PATCH 09/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/barcode_validator.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index 79b4c9e5f309..2c123b41e144 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -6,7 +6,7 @@ def get_check_digit(barcode: int) -> int: >>> get_check_digit(8718452538119) 9 - + """ barcode //= 10 # exclude the last digit @@ -62,23 +62,24 @@ def get_barcode(barcode: str) -> int: ValueError: Barcode 'dwefgiweuf' has alphabets in it """ if str(barcode).isalpha(): - raise ValueError("Barcode '{}' has alphabets in it".format(barcode)) + raise ValueError(f"Barcode '{barcode}' has alphabets in it") - if int(barcode)<0: + if int(barcode) < 0: raise ValueError("The entered barcode has negative values. Try again.") - + else: return int(barcode) + if __name__ == "__main__": import doctest doctest.testmod() """ Enter a barcode. - - """ - barcode = get_barcode((input("Barcode: ").strip())) + + """ + barcode = get_barcode(input("Barcode: ").strip()) if is_valid(barcode): print(f"|{barcode}| is a valid Barcode") From 70084a0f97ed78f8c5b75a629355d80efbd22f5b Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Wed, 12 Oct 2022 23:03:59 +0200 Subject: [PATCH 10/13] Update barcode_validator.py Updated with f-string format --- strings/barcode_validator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index 79b4c9e5f309..90d2ccdcf772 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -62,7 +62,7 @@ def get_barcode(barcode: str) -> int: ValueError: Barcode 'dwefgiweuf' has alphabets in it """ if str(barcode).isalpha(): - raise ValueError("Barcode '{}' has alphabets in it".format(barcode)) + raise ValueError(f"Barcode '{barcode}' has alphabets in it") if int(barcode)<0: raise ValueError("The entered barcode has negative values. Try again.") From b973148a8f0abf50373c2c8953fdf75834e60cb6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 13 Oct 2022 00:45:35 +0200 Subject: [PATCH 11/13] Update barcode_validator.py --- strings/barcode_validator.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/strings/barcode_validator.py b/strings/barcode_validator.py index 2c123b41e144..05670007665c 100644 --- a/strings/barcode_validator.py +++ b/strings/barcode_validator.py @@ -1,3 +1,8 @@ +""" +https://en.wikipedia.org/wiki/Check_digit#Algorithms +""" + + def get_check_digit(barcode: int) -> int: """ Returns the last digit of barcode by excluding the last digit first @@ -6,9 +11,13 @@ def get_check_digit(barcode: int) -> int: >>> get_check_digit(8718452538119) 9 - + >>> get_check_digit(87184523) + 5 + >>> get_check_digit(87193425381086) + 9 + >>> [get_check_digit(x) for x in range(0, 100, 10)] + [0, 7, 4, 1, 8, 5, 2, 9, 6, 3] """ - barcode //= 10 # exclude the last digit checker = False s = 0 @@ -30,22 +39,17 @@ def is_valid(barcode: int) -> bool: >>> is_valid(8718452538119) True - >>> is_valid(87184525) False - >>> is_valid(87193425381089) False - >>> is_valid(0) False - >>> is_valid(dwefgiweuf) Traceback (most recent call last): ... NameError: name 'dwefgiweuf' is not defined """ - return len(str(barcode)) == 13 and get_check_digit(barcode) == barcode % 10 @@ -55,18 +59,15 @@ def get_barcode(barcode: str) -> int: >>> get_barcode("8718452538119") 8718452538119 - >>> get_barcode("dwefgiweuf") Traceback (most recent call last): ... - ValueError: Barcode 'dwefgiweuf' has alphabets in it + ValueError: Barcode 'dwefgiweuf' has alphabetic characters. """ if str(barcode).isalpha(): - raise ValueError(f"Barcode '{barcode}' has alphabets in it") - - if int(barcode) < 0: - raise ValueError("The entered barcode has negative values. Try again.") - + raise ValueError(f"Barcode '{barcode}' has alphabetic characters.") + elif int(barcode) < 0: + raise ValueError("The entered barcode has a negative value. Try again.") else: return int(barcode) @@ -82,6 +83,6 @@ def get_barcode(barcode: str) -> int: barcode = get_barcode(input("Barcode: ").strip()) if is_valid(barcode): - print(f"|{barcode}| is a valid Barcode") + print(f"'{barcode}' is a valid Barcode") else: - print(f"|{barcode}| is NOT is valid Barcode.") + print(f"'{barcode}' is NOT is valid Barcode.") From 633361a334ac3804424fe780c0da59095d59b563 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Thu, 13 Oct 2022 14:49:41 +0200 Subject: [PATCH 12/13] Update volume_conversions.py Simpler doctest output --- conversions/volume_conversions.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/conversions/volume_conversions.py b/conversions/volume_conversions.py index de2290196fc2..34799705b119 100644 --- a/conversions/volume_conversions.py +++ b/conversions/volume_conversions.py @@ -52,11 +52,7 @@ def volume_conversion(value: float, from_type: str, to_type: str) -> float: 0.000236588 >>> volume_conversion(4, "wrongUnit", "litre") Traceback (most recent call last): - File "/usr/lib/python3.8/doctest.py", line 1336, in __run - exec(compile(example.source, filename, "single", - File "", line 1, in - volume_conversion(4, "wrongUnit", "litre") - File "", line 62, in volume_conversion + ... ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: cubicmeter, litre, kilolitre, gallon, cubicyard, cubicfoot, cup """ From b3e52bc7f1b0898a358d8adaa1caec55cd2803aa Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Fri, 14 Oct 2022 11:59:27 +0200 Subject: [PATCH 13/13] Update volume_conversions.py Fixed indentation --- conversions/volume_conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/volume_conversions.py b/conversions/volume_conversions.py index 34799705b119..44d29009120c 100644 --- a/conversions/volume_conversions.py +++ b/conversions/volume_conversions.py @@ -52,7 +52,7 @@ def volume_conversion(value: float, from_type: str, to_type: str) -> float: 0.000236588 >>> volume_conversion(4, "wrongUnit", "litre") Traceback (most recent call last): - ... + ... ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: cubicmeter, litre, kilolitre, gallon, cubicyard, cubicfoot, cup """