From fcc64604d389233b78713493d8f913128a4b38e8 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Thu, 6 Oct 2022 19:22:06 +0200 Subject: [PATCH 01/18] 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/18] 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/18] [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/18] 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/18] [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/18] 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/18] [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/18] 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/18] [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/18] 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/18] 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/18] 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 820be69c6605a11de54a544d54b76c09b8887a34 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Thu, 13 Oct 2022 17:21:22 +0200 Subject: [PATCH 13/18] Create speed_conversions.py Conversion of speed units --- conversions/speed_conversions.py | 170 +++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 conversions/speed_conversions.py diff --git a/conversions/speed_conversions.py b/conversions/speed_conversions.py new file mode 100644 index 000000000000..3ba7644ef09c --- /dev/null +++ b/conversions/speed_conversions.py @@ -0,0 +1,170 @@ +""" +Convert speed units + +https://en.wikipedia.org/wiki/Kilometres_per_hour +https://en.wikipedia.org/wiki/Miles_per_hour +https://en.wikipedia.org/wiki/Knot_(unit) +https://en.wikipedia.org/wiki/Metre_per_second + +""" + +def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: + """ + Return speed units based on input + + >>> convert_speed(10, 'm/s', 'km/h') + 36.0 + + >>> convert_speed(10, 'km/h', 'm/s') + 2.78 + + >>> convert_speed(10, 'm/s', 'mph') + 22.37 + + >>> convert_speed(10, 'mph', 'm/s') + 4.47 + + >>> convert_speed(10, 'km/h', 'mph') + 6.21 + + >>> convert_speed(10, 'mph', 'km/h') + 16.09 + + >>> convert_speed(10, 'm/s', 'knots') + 19.44 + + >>> convert_speed(10, 'knots', 'm/s') + 5.14 + + >>> convert_speed(10, 'km/h', 'knots') + 5.4 + + >>> convert_speed(10, 'knots', 'km/h') + 18.52 + + >>> convert_speed(10, 'mph', 'knots') + 8.69 + + >>> convert_speed(10, 'knots', 'mph') + 11.51 + + >>> convert_speed(abc, 'm/s', 'km/h') + Traceback (most recent call last): + ... + NameError: name 'abc' is not defined + + >>> convert_speed(10, 'abc', 'km/h') + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: abc + """ + if unit_from == 'm/s': + if unit_to == 'km/h': + return round(speed * 3.6, 2) + elif unit_to == 'mph': + return round(speed * 2.23694, 2) + elif unit_to == 'knots': + return round(speed * 1.94384, 2) + else: + raise ValueError('invalid literal for int() with base 10: ' + unit_to) + + elif unit_from == 'km/h': + if unit_to == 'm/s': + return round(speed / 3.6, 2) + elif unit_to == 'mph': + return round(speed * 0.621371, 2) + elif unit_to == 'knots': + return round(speed * 0.539957, 2) + else: + raise ValueError('invalid literal for int() with base 10: ' + unit_to) + + elif unit_from == 'mph': + if unit_to == 'm/s': + return round(speed / 2.23694, 2) + elif unit_to == 'km/h': + return round(speed / 0.621371, 2) + elif unit_to == 'knots': + return round(speed * 0.868976, 2) + else: + raise ValueError('invalid literal for int() with base 10: ' + unit_to) + + elif unit_from == 'knots': + if unit_to == 'm/s': + return round(speed / 1.94384, 2) + elif unit_to == 'km/h': + return round(speed / 0.539957, 2) + elif unit_to == 'mph': + return round(speed / 0.868976, 2) + else: + raise ValueError('invalid literal for int() with base 10: ' + unit_to) + else: + raise ValueError('invalid literal for int() with base 10: ' + unit_from) + + +if __name__ == '__main__': + """ + Run doctests + + >>> round(convert_speed(10, 'm/s', 'km/h'), 2) + 36.0 + + >>> round(convert_speed(10, 'km/h', 'm/s'), 2) + 2.78 + + >>> round(convert_speed(10, 'm/s', 'mph'), 2) + 22.37 + + >>> round(convert_speed(10, 'mph', 'm/s'), 2) + 4.47 + + >>> round(convert_speed(10, 'km/h', 'mph'), 2) + 6.21 + + >>> round(convert_speed(10, 'mph', 'km/h'), 2) + 16.09 + + >>> round(convert_speed(10, 'm/s', 'knots'), 2) + 19.44 + + >>> round(convert_speed(10, 'knots', 'm/s'), 2) + 5.14 + + >>> round(convert_speed(10, 'km/h', 'knots'), 2) + 5.4 + + >>> round(convert_speed(10, 'knots', 'km/h'), 2) + 18.52 + + >>> round(convert_speed(10, 'mph', 'knots'), 2) + 8.69 + + >>> round(convert_speed(10, 'knots', 'mph'), 2) + 11.55 + + >>> round(convert_speed(10, 'abc', 'km/h'), 2) + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: 'abc' + + >>> round(convert_speed(abc, 'm/s', 'km/h'), 2) + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'abc' + + """ + import doctest + doctest.testmod() + + print("----Codes for speed units----") + print("m/s: meters per second") + print("km/h: kilometers per hour") + print("mph: miles per hour") + print("knots: nautical miles per hour") + + from_unit = input('From unit code: ') + to_unit = input('To unit code: ') + speed = input('Speed: ') + + speed_converted = round(convert_speed(float(speed), from_unit, to_unit), 2) + + print(f'{speed} {from_unit} is {speed_converted} {to_unit}') \ No newline at end of file From e70f471acaba4797e1e58073f1467f77f5e033e3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 15:26:14 +0000 Subject: [PATCH 14/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/speed_conversions.py | 66 ++++++++++++++++---------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/conversions/speed_conversions.py b/conversions/speed_conversions.py index 3ba7644ef09c..858fe9ad4783 100644 --- a/conversions/speed_conversions.py +++ b/conversions/speed_conversions.py @@ -8,16 +8,17 @@ """ + def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: """ Return speed units based on input - + >>> convert_speed(10, 'm/s', 'km/h') 36.0 >>> convert_speed(10, 'km/h', 'm/s') 2.78 - + >>> convert_speed(10, 'm/s', 'mph') 22.37 @@ -47,7 +48,7 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: >>> convert_speed(10, 'knots', 'mph') 11.51 - + >>> convert_speed(abc, 'm/s', 'km/h') Traceback (most recent call last): ... @@ -58,53 +59,53 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: ... ValueError: invalid literal for int() with base 10: abc """ - if unit_from == 'm/s': - if unit_to == 'km/h': + if unit_from == "m/s": + if unit_to == "km/h": return round(speed * 3.6, 2) - elif unit_to == 'mph': + elif unit_to == "mph": return round(speed * 2.23694, 2) - elif unit_to == 'knots': + elif unit_to == "knots": return round(speed * 1.94384, 2) else: - raise ValueError('invalid literal for int() with base 10: ' + unit_to) + raise ValueError("invalid literal for int() with base 10: " + unit_to) - elif unit_from == 'km/h': - if unit_to == 'm/s': + elif unit_from == "km/h": + if unit_to == "m/s": return round(speed / 3.6, 2) - elif unit_to == 'mph': + elif unit_to == "mph": return round(speed * 0.621371, 2) - elif unit_to == 'knots': + elif unit_to == "knots": return round(speed * 0.539957, 2) else: - raise ValueError('invalid literal for int() with base 10: ' + unit_to) + raise ValueError("invalid literal for int() with base 10: " + unit_to) - elif unit_from == 'mph': - if unit_to == 'm/s': + elif unit_from == "mph": + if unit_to == "m/s": return round(speed / 2.23694, 2) - elif unit_to == 'km/h': + elif unit_to == "km/h": return round(speed / 0.621371, 2) - elif unit_to == 'knots': + elif unit_to == "knots": return round(speed * 0.868976, 2) else: - raise ValueError('invalid literal for int() with base 10: ' + unit_to) + raise ValueError("invalid literal for int() with base 10: " + unit_to) - elif unit_from == 'knots': - if unit_to == 'm/s': + elif unit_from == "knots": + if unit_to == "m/s": return round(speed / 1.94384, 2) - elif unit_to == 'km/h': + elif unit_to == "km/h": return round(speed / 0.539957, 2) - elif unit_to == 'mph': + elif unit_to == "mph": return round(speed / 0.868976, 2) else: - raise ValueError('invalid literal for int() with base 10: ' + unit_to) + raise ValueError("invalid literal for int() with base 10: " + unit_to) else: - raise ValueError('invalid literal for int() with base 10: ' + unit_from) + raise ValueError("invalid literal for int() with base 10: " + unit_from) -if __name__ == '__main__': +if __name__ == "__main__": """ Run doctests - + >>> round(convert_speed(10, 'm/s', 'km/h'), 2) 36.0 @@ -149,22 +150,23 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: >>> round(convert_speed(abc, 'm/s', 'km/h'), 2) Traceback (most recent call last): ... - ValueError: could not convert string to float: 'abc' + ValueError: could not convert string to float: 'abc' """ import doctest + doctest.testmod() print("----Codes for speed units----") print("m/s: meters per second") print("km/h: kilometers per hour") print("mph: miles per hour") - print("knots: nautical miles per hour") + print("knots: nautical miles per hour") - from_unit = input('From unit code: ') - to_unit = input('To unit code: ') - speed = input('Speed: ') + from_unit = input("From unit code: ") + to_unit = input("To unit code: ") + speed = input("Speed: ") speed_converted = round(convert_speed(float(speed), from_unit, to_unit), 2) - print(f'{speed} {from_unit} is {speed_converted} {to_unit}') \ No newline at end of file + print(f"{speed} {from_unit} is {speed_converted} {to_unit}") From f1c08373b7d064bf6c2ea70f9ab2929a2d5b436d Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Fri, 14 Oct 2022 01:15:53 +0200 Subject: [PATCH 15/18] Update speed_conversions.py Doctests updated, dictionary implemented. --- conversions/speed_conversions.py | 194 ++++++++----------------------- 1 file changed, 51 insertions(+), 143 deletions(-) diff --git a/conversions/speed_conversions.py b/conversions/speed_conversions.py index 858fe9ad4783..6bb52fa5ab13 100644 --- a/conversions/speed_conversions.py +++ b/conversions/speed_conversions.py @@ -8,165 +8,73 @@ """ +speed_chart: dict[str, float] = { + "km/h": 1.0, + "m/s": 3.6, + "mph": 1.609344, + "knot": 1.852, +} + +speed_chart_inverse: dict[str, float] = { + "km/h": 1.0, + "m/s": 0.277777778, + "mph": 0.621371192, + "knot": 0.539956803, +} def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: """ - Return speed units based on input + Convert speed from one unit to another unit using the speed_chart - >>> convert_speed(10, 'm/s', 'km/h') - 36.0 + "km/h": 1.0, + "m/s": 3.6, + "mph": 1.609344, + "knot": 1.852, - >>> convert_speed(10, 'km/h', 'm/s') - 2.78 + >>> convert_speed(100, "km/h", "m/s") + 27.778 - >>> convert_speed(10, 'm/s', 'mph') - 22.37 + >>> convert_speed(100, "km/h", "mph") + 62.137 - >>> convert_speed(10, 'mph', 'm/s') - 4.47 + >>> convert_speed(100, "km/h", "knot") + 53.996 - >>> convert_speed(10, 'km/h', 'mph') - 6.21 + >>> convert_speed(100, "m/s", "km/h") + 360.0 - >>> convert_speed(10, 'mph', 'km/h') - 16.09 + >>> convert_speed(100, "m/s", "mph") + 223.694 - >>> convert_speed(10, 'm/s', 'knots') - 19.44 + >>> convert_speed(100, "m/s", "knot") + 194.384 - >>> convert_speed(10, 'knots', 'm/s') - 5.14 + >>> convert_speed(100, "mph", "km/h") + 160.934 - >>> convert_speed(10, 'km/h', 'knots') - 5.4 + >>> convert_speed(100, "mph", "m/s") + 44.704 - >>> convert_speed(10, 'knots', 'km/h') - 18.52 + >>> convert_speed(100, "mph", "knot") + 86.898 - >>> convert_speed(10, 'mph', 'knots') - 8.69 + >>> convert_speed(100, "knot", "km/h") + 185.2 - >>> convert_speed(10, 'knots', 'mph') - 11.51 - - >>> convert_speed(abc, 'm/s', 'km/h') - Traceback (most recent call last): - ... - NameError: name 'abc' is not defined - - >>> convert_speed(10, 'abc', 'km/h') - Traceback (most recent call last): - ... - ValueError: invalid literal for int() with base 10: abc + >>> convert_speed(100, "knot", "m/s") + 51.444 + + >>> convert_speed(100, "knot", "mph") + 115.078 """ - if unit_from == "m/s": - if unit_to == "km/h": - return round(speed * 3.6, 2) - elif unit_to == "mph": - return round(speed * 2.23694, 2) - elif unit_to == "knots": - return round(speed * 1.94384, 2) - else: - raise ValueError("invalid literal for int() with base 10: " + unit_to) - - elif unit_from == "km/h": - if unit_to == "m/s": - return round(speed / 3.6, 2) - elif unit_to == "mph": - return round(speed * 0.621371, 2) - elif unit_to == "knots": - return round(speed * 0.539957, 2) - else: - raise ValueError("invalid literal for int() with base 10: " + unit_to) - - elif unit_from == "mph": - if unit_to == "m/s": - return round(speed / 2.23694, 2) - elif unit_to == "km/h": - return round(speed / 0.621371, 2) - elif unit_to == "knots": - return round(speed * 0.868976, 2) - else: - raise ValueError("invalid literal for int() with base 10: " + unit_to) - - elif unit_from == "knots": - if unit_to == "m/s": - return round(speed / 1.94384, 2) - elif unit_to == "km/h": - return round(speed / 0.539957, 2) - elif unit_to == "mph": - return round(speed / 0.868976, 2) - else: - raise ValueError("invalid literal for int() with base 10: " + unit_to) - else: - raise ValueError("invalid literal for int() with base 10: " + unit_from) - + if unit_to not in speed_chart or unit_from not in speed_chart_inverse: + raise ValueError( + f"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n" + f"Valid values are: {', '.join(speed_chart_inverse)}" + ) + + return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to], 3) if __name__ == "__main__": - """ - Run doctests - - >>> round(convert_speed(10, 'm/s', 'km/h'), 2) - 36.0 - - >>> round(convert_speed(10, 'km/h', 'm/s'), 2) - 2.78 - - >>> round(convert_speed(10, 'm/s', 'mph'), 2) - 22.37 - - >>> round(convert_speed(10, 'mph', 'm/s'), 2) - 4.47 - - >>> round(convert_speed(10, 'km/h', 'mph'), 2) - 6.21 - - >>> round(convert_speed(10, 'mph', 'km/h'), 2) - 16.09 - - >>> round(convert_speed(10, 'm/s', 'knots'), 2) - 19.44 - - >>> round(convert_speed(10, 'knots', 'm/s'), 2) - 5.14 - - >>> round(convert_speed(10, 'km/h', 'knots'), 2) - 5.4 - - >>> round(convert_speed(10, 'knots', 'km/h'), 2) - 18.52 - - >>> round(convert_speed(10, 'mph', 'knots'), 2) - 8.69 - - >>> round(convert_speed(10, 'knots', 'mph'), 2) - 11.55 - - >>> round(convert_speed(10, 'abc', 'km/h'), 2) - Traceback (most recent call last): - ... - ValueError: invalid literal for int() with base 10: 'abc' - - >>> round(convert_speed(abc, 'm/s', 'km/h'), 2) - Traceback (most recent call last): - ... - ValueError: could not convert string to float: 'abc' - - """ import doctest - - doctest.testmod() - - print("----Codes for speed units----") - print("m/s: meters per second") - print("km/h: kilometers per hour") - print("mph: miles per hour") - print("knots: nautical miles per hour") - - from_unit = input("From unit code: ") - to_unit = input("To unit code: ") - speed = input("Speed: ") - - speed_converted = round(convert_speed(float(speed), from_unit, to_unit), 2) - - print(f"{speed} {from_unit} is {speed_converted} {to_unit}") + doctest.testmod() \ No newline at end of file From ed8c7f3d31a09cd4b7c70821ddeb9d6e9a1eb8f6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 23:17:39 +0000 Subject: [PATCH 16/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/speed_conversions.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/conversions/speed_conversions.py b/conversions/speed_conversions.py index 6bb52fa5ab13..42db191a6999 100644 --- a/conversions/speed_conversions.py +++ b/conversions/speed_conversions.py @@ -22,6 +22,7 @@ "knot": 0.539956803, } + def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: """ Convert speed from one unit to another unit using the speed_chart @@ -63,7 +64,7 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: >>> convert_speed(100, "knot", "m/s") 51.444 - + >>> convert_speed(100, "knot", "mph") 115.078 """ @@ -71,10 +72,12 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: raise ValueError( f"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n" f"Valid values are: {', '.join(speed_chart_inverse)}" - ) - + ) + return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to], 3) + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 5f84d0b872eaf4e22992266103810bbe05d97656 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Fri, 14 Oct 2022 11:54:51 +0200 Subject: [PATCH 17/18] Update speed_conversions.py Reduced LOC --- conversions/speed_conversions.py | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/conversions/speed_conversions.py b/conversions/speed_conversions.py index 42db191a6999..62da9e137bc7 100644 --- a/conversions/speed_conversions.py +++ b/conversions/speed_conversions.py @@ -5,7 +5,6 @@ https://en.wikipedia.org/wiki/Miles_per_hour https://en.wikipedia.org/wiki/Knot_(unit) https://en.wikipedia.org/wiki/Metre_per_second - """ speed_chart: dict[str, float] = { @@ -25,7 +24,7 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: """ - Convert speed from one unit to another unit using the speed_chart + Convert speed from one unit to another using the speed_chart above. "km/h": 1.0, "m/s": 3.6, @@ -34,37 +33,26 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: >>> convert_speed(100, "km/h", "m/s") 27.778 - >>> convert_speed(100, "km/h", "mph") 62.137 - >>> convert_speed(100, "km/h", "knot") 53.996 - >>> convert_speed(100, "m/s", "km/h") 360.0 - >>> convert_speed(100, "m/s", "mph") 223.694 - >>> convert_speed(100, "m/s", "knot") 194.384 - >>> convert_speed(100, "mph", "km/h") 160.934 - >>> convert_speed(100, "mph", "m/s") 44.704 - >>> convert_speed(100, "mph", "knot") 86.898 - >>> convert_speed(100, "knot", "km/h") 185.2 - >>> convert_speed(100, "knot", "m/s") 51.444 - >>> convert_speed(100, "knot", "mph") 115.078 """ @@ -73,7 +61,6 @@ def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: f"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n" f"Valid values are: {', '.join(speed_chart_inverse)}" ) - return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to], 3) From c5153ff903c3f3c1c3c0110e1de436893d3f5ca5 Mon Sep 17 00:00:00 2001 From: Abinash Satapathy Date: Fri, 14 Oct 2022 19:42:36 +0200 Subject: [PATCH 18/18] Update volume_conversions.py --- 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 """