From 1f6f4c66f782bb5f82d715c403ecf80f5392a3dc Mon Sep 17 00:00:00 2001 From: Tanmay Srivastava Date: Mon, 17 May 2021 13:54:55 +0530 Subject: [PATCH 01/28] Added a file that converts hexa to binary --- conversions/hex-bin.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 conversions/hex-bin.py diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py new file mode 100644 index 000000000000..9b3dd4478666 --- /dev/null +++ b/conversions/hex-bin.py @@ -0,0 +1,28 @@ +#code to convert hexadecimal into binary +import math + +hex_str = input("Enter you value: ").strip() +n = int(hex_str, 16) +bin_str = "" + + +""" +Here, we have used " >> " bitwise operator +Bitwise right shift: +Shifts the bits of the number to the right and fills 0 on voids left as a result. +Similar effect as of dividing the number with some power of two. +Example: +a = 10 +a >> 1 = 5 + +""" +def convert(n, bin_str): + while n>0: + #print(n) + m = str(n%2) + bin_str = m + bin_str + n=n>>1 + + return str(bin_str) + +print("Your value in BINARY is " + convert(n, bin_str)) \ No newline at end of file From 9149d373b78569ab3b9b8c6ae43e27f9e3d029c0 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Mon, 17 May 2021 14:14:49 +0530 Subject: [PATCH 02/28] Added file to convert hexadecimal to binary --- conversions/hex-bin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 9b3dd4478666..730d49ebfdf7 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -16,6 +16,7 @@ a >> 1 = 5 """ + def convert(n, bin_str): while n>0: #print(n) @@ -25,4 +26,4 @@ def convert(n, bin_str): return str(bin_str) -print("Your value in BINARY is " + convert(n, bin_str)) \ No newline at end of file +print("Your value in BINARY is " + convert(n, bin_str)) From 71cbcbcddecb2491ec6ac777dd93f023732b8919 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Mon, 17 May 2021 15:29:06 +0530 Subject: [PATCH 03/28] Update hex-bin.py --- conversions/hex-bin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 730d49ebfdf7..38e2705c7c25 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -27,3 +27,5 @@ def convert(n, bin_str): return str(bin_str) print("Your value in BINARY is " + convert(n, bin_str)) + + From 9d4e6cb3176ee1d5b10c7856e38b6d1a3c256634 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Mon, 17 May 2021 16:11:50 +0530 Subject: [PATCH 04/28] added type hint in the code --- conversions/hex-bin.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 38e2705c7c25..4a17f9b76a21 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -2,8 +2,8 @@ import math hex_str = input("Enter you value: ").strip() -n = int(hex_str, 16) -bin_str = "" +num : int = int(hex_str, 16) +bin_str: str = "" """ @@ -17,15 +17,18 @@ """ -def convert(n, bin_str): +def convert(num: int, bin_str: str) -> str: while n>0: #print(n) - m = str(n%2) - bin_str = m + bin_str - n=n>>1 + mnum = str(n%2) + bin_str = mnum + bin_str + num=num>>1 return str(bin_str) -print("Your value in BINARY is " + convert(n, bin_str)) +print("Your value in BINARY is " + convert(num, bin_str)) +if __name__ == "__main__": + import doctest + doctest.testmod() From e82db59070e70495371ff5ad5b6106ba5d9c85a9 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Mon, 17 May 2021 16:32:21 +0530 Subject: [PATCH 05/28] Added doctest --- conversions/hex-bin.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 4a17f9b76a21..5c01e0fbd1c4 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,7 +1,9 @@ #code to convert hexadecimal into binary +from doctest import testmod import math -hex_str = input("Enter you value: ").strip() + +hex_str = input("Enter your value: ").strip() num : int = int(hex_str, 16) bin_str: str = "" @@ -18,6 +20,11 @@ """ def convert(num: int, bin_str: str) -> str: + """ + >>> Enter your value: AC + >>> Your value in BINARY is 10101100 + + """ while n>0: #print(n) mnum = str(n%2) @@ -29,6 +36,5 @@ def convert(num: int, bin_str: str) -> str: print("Your value in BINARY is " + convert(num, bin_str)) if __name__ == "__main__": - import doctest - doctest.testmod() + testmod(name = 'convert', verbose = True) From 6549855c3c2a1100749e57cbae514adc46e44c39 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Mon, 17 May 2021 16:56:40 +0530 Subject: [PATCH 06/28] Added code to handle exception --- conversions/hex-bin.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 5c01e0fbd1c4..c55afcff4ef8 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,10 +1,15 @@ -#code to convert hexadecimal into binary +#convert hexadecimal into binary from doctest import testmod import math -hex_str = input("Enter your value: ").strip() -num : int = int(hex_str, 16) +hex_str = input("").strip() + +try: + num : int = int(hex_str, 16) +except: + print("An exception occured") + bin_str: str = "" @@ -21,19 +26,18 @@ def convert(num: int, bin_str: str) -> str: """ - >>> Enter your value: AC - >>> Your value in BINARY is 10101100 + >>> AC + >>> 10101100 """ - while n>0: - #print(n) - mnum = str(n%2) + while num > 0: + mnum = str(num % 2) bin_str = mnum + bin_str num=num>>1 return str(bin_str) -print("Your value in BINARY is " + convert(num, bin_str)) +print(convert(num, bin_str)) if __name__ == "__main__": testmod(name = 'convert', verbose = True) From 65d275b90ae625e8bc2c6d19aeb3811ab406ff96 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Mon, 17 May 2021 21:49:27 +0530 Subject: [PATCH 07/28] Resolved doctest issue --- conversions/hex-bin.py | 51 +++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index c55afcff4ef8..7cdab0dc7947 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -4,13 +4,14 @@ hex_str = input("").strip() - -try: - num : int = int(hex_str, 16) -except: - print("An exception occured") +if not hex_str: + raise ValueError("Empty string was passed to the function") +is_negative = hex_str[0] == "-" +if is_negative: + hex_string = hex_str[1:] -bin_str: str = "" +num: int = int(hex_str, 16) +bin: str = "" """ @@ -24,20 +25,40 @@ """ -def convert(num: int, bin_str: str) -> str: +def convert(num, bin_str, is_negative): """ - >>> AC - >>> 10101100 + + Convert a hexadecimal value to its decimal equivalent + #https://stackoverflow.com/questions/1425493/convert-hex-to-binary + >>> convert(AC, bin_str) + 10101100 + >>> convert(9A4, bin_str) + 100110100100 + >>> convert(" 12f ", bin_str) + 100101111 + >>> convert(FfFf, bin_str) + 1111111111111111 + >>> convert(F-f, bin_str) + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 16: + >>> convert(,bin_str) + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function: """ - while num > 0: - mnum = str(num % 2) + + while num>0: + mnum: str = str(n%2) bin_str = mnum + bin_str num=num>>1 - - return str(bin_str) - -print(convert(num, bin_str)) + mnum = bin_str + if is_negative: + sign = "-" + return sign+bin_str + else: + return bin_str if __name__ == "__main__": testmod(name = 'convert', verbose = True) From fc993400c6f79411b074842e3992bd9c405859e8 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Mon, 17 May 2021 22:07:06 +0530 Subject: [PATCH 08/28] Update hex-bin.py --- conversions/hex-bin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 7cdab0dc7947..cc6395cc2113 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -6,7 +6,7 @@ hex_str = input("").strip() if not hex_str: raise ValueError("Empty string was passed to the function") -is_negative = hex_str[0] == "-" +is_negative:bool = hex_str[0] == "-" if is_negative: hex_string = hex_str[1:] @@ -25,7 +25,7 @@ """ -def convert(num, bin_str, is_negative): +def convert(num: int, bin_str: str, is_negative: bool) ->str : """ Convert a hexadecimal value to its decimal equivalent From cbce9e19af43d5ca87d7b9d6d98374ab6b4cc601 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Tue, 18 May 2021 12:41:20 +0530 Subject: [PATCH 09/28] Modified convert function --- conversions/hex-bin.py | 76 ++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index cc6395cc2113..8dfdd02875f7 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -2,63 +2,65 @@ from doctest import testmod import math - -hex_str = input("").strip() -if not hex_str: - raise ValueError("Empty string was passed to the function") -is_negative:bool = hex_str[0] == "-" -if is_negative: - hex_string = hex_str[1:] +def convert(num: str) -> str: -num: int = int(hex_str, 16) -bin: str = "" - - -""" -Here, we have used " >> " bitwise operator -Bitwise right shift: -Shifts the bits of the number to the right and fills 0 on voids left as a result. -Similar effect as of dividing the number with some power of two. -Example: -a = 10 -a >> 1 = 5 - -""" - -def convert(num: int, bin_str: str, is_negative: bool) ->str : """ - Convert a hexadecimal value to its decimal equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary - >>> convert(AC, bin_str) + >>> convert("AC") 10101100 - >>> convert(9A4, bin_str) + >>> convert("9A4") 100110100100 - >>> convert(" 12f ", bin_str) + >>> convert(" 12f ") 100101111 - >>> convert(FfFf, bin_str) + >>> convert("FfFf") 1111111111111111 - >>> convert(F-f, bin_str) + >>> convert("F-f") Traceback (most recent call last): ... ValueError: invalid literal for int() with base 16: - >>> convert(,bin_str) + >>> convert() Traceback (most recent call last): ... ValueError: Empty string was passed to the function: """ - while num>0: - mnum: str = str(n%2) - bin_str = mnum + bin_str - num=num>>1 - mnum = bin_str + bin_str : str="" + flag :bool = 0 + hex_str :str = num.strip() + + if not hex_str: + raise ValueError("Empty string was passed to the function") + is_negative : bool= hex_str[0] == "-" + if is_negative: - sign = "-" - return sign+bin_str + flag = 1 + hex_string = hex_str[1:] + + num2 :int = int(hex_str, 16) + + """ + Here, we have used " >> " bitwise operator + Bitwise right shift: + Shifts the bits of the number to the right and fills 0 on voids left as a result. + Similar effect as of dividing the number with some power of two. + Example: + a = 10 + a >> 1 = 5 + + """ + + while num2>0: + num3 :str = str(num2%2) + bin_str = num3 + bin_str + num2=num2>>1 + + if flag: + return "-"+bin_str else: return bin_str + if __name__ == "__main__": testmod(name = 'convert', verbose = True) From e794056c2b95f077bc535735911a915519ee9c16 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Tue, 18 May 2021 13:27:18 +0530 Subject: [PATCH 10/28] Added WhiteSpace around operators. --- conversions/hex-bin.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 8dfdd02875f7..e618dc5deb62 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,4 +1,4 @@ -#convert hexadecimal into binary +# CONVERT HEXADECIMAL TO BINARY from doctest import testmod import math @@ -7,6 +7,14 @@ def convert(num: str) -> str: """ Convert a hexadecimal value to its decimal equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary + Here, we have used " >> " bitwise operator + Bitwise right shift: + Shifts the bits of the number to the right and fills 0 on voids left as a result. + Similar effect as of dividing the number with some power of two. + Example: + a = 10 + a >> 1 = 5 + >>> convert("AC") 10101100 >>> convert("9A4") @@ -26,38 +34,31 @@ def convert(num: str) -> str: """ - bin_str : str="" + bin_str :str="" flag :bool = 0 hex_str :str = num.strip() if not hex_str: - raise ValueError("Empty string was passed to the function") + return False + is_negative : bool= hex_str[0] == "-" if is_negative: flag = 1 hex_string = hex_str[1:] - num2 :int = int(hex_str, 16) - - """ - Here, we have used " >> " bitwise operator - Bitwise right shift: - Shifts the bits of the number to the right and fills 0 on voids left as a result. - Similar effect as of dividing the number with some power of two. - Example: - a = 10 - a >> 1 = 5 - - """ + try: + num2 :int = int(hex_str, 16) + except ValueError: + return False - while num2>0: - num3 :str = str(num2%2) + while num2 > 0: + num3 :str = str(num2 % 2) bin_str = num3 + bin_str - num2=num2>>1 + num2 = num2 >> 1 if flag: - return "-"+bin_str + return "-" + bin_str else: return bin_str From 4a36cad5896ede48f027bf319cb35993e9f700e7 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Tue, 18 May 2021 21:25:22 +0530 Subject: [PATCH 11/28] Made more pythonic --- conversions/hex-bin.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index e618dc5deb62..e2a116a71f1a 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -2,7 +2,7 @@ from doctest import testmod import math -def convert(num: str) -> str: +def convert(num: str) ->str: """ Convert a hexadecimal value to its decimal equivalent @@ -56,11 +56,9 @@ def convert(num: str) -> str: num3 :str = str(num2 % 2) bin_str = num3 + bin_str num2 = num2 >> 1 - - if flag: - return "-" + bin_str - else: - return bin_str + + return "-" + bin_str if flag else bin_str + if __name__ == "__main__": From 9e212c6feee464527d528b29ccd0a31765406851 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Tue, 18 May 2021 21:28:00 +0530 Subject: [PATCH 12/28] removed whitespace --- conversions/hex-bin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index e2a116a71f1a..62e3613834f8 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -2,8 +2,7 @@ from doctest import testmod import math -def convert(num: str) ->str: - +def convert(num: str) -> str: """ Convert a hexadecimal value to its decimal equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary From 63c2096a3e913c5946e281461e156a3acf7473fc Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Tue, 18 May 2021 22:30:05 +0530 Subject: [PATCH 13/28] Updated doctest command --- conversions/hex-bin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 62e3613834f8..bdeae33a6006 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,5 +1,4 @@ # CONVERT HEXADECIMAL TO BINARY -from doctest import testmod import math def convert(num: str) -> str: @@ -56,10 +55,11 @@ def convert(num: str) -> str: bin_str = num3 + bin_str num2 = num2 >> 1 - return "-" + bin_str if flag else bin_str + return "-" + "".join(bin_str) if flag else bin_str if __name__ == "__main__": - testmod(name = 'convert', verbose = True) + import doctest + doctest.testmod() From 8d5c32ab713fc09743a83800aa4bfef1e97edd46 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Wed, 19 May 2021 08:55:57 +0530 Subject: [PATCH 14/28] Removed whitespace --- conversions/hex-bin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index bdeae33a6006..7faf50de0a71 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -44,7 +44,6 @@ def convert(num: str) -> str: if is_negative: flag = 1 hex_string = hex_str[1:] - try: num2 :int = int(hex_str, 16) except ValueError: From 6ba22e3a8f16fa6db7101560a4a3846018bfb952 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Wed, 19 May 2021 11:55:18 +0530 Subject: [PATCH 15/28] imported union --- conversions/hex-bin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 7faf50de0a71..b1920a9805b1 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,7 +1,8 @@ # CONVERT HEXADECIMAL TO BINARY import math +from typing import Union -def convert(num: str) -> str: +def convert(num: str) ->Union[bool, str]: """ Convert a hexadecimal value to its decimal equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary From 180f6027ddd9a51083fec628e1c97641532d2a52 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Wed, 19 May 2021 12:53:07 +0530 Subject: [PATCH 16/28] Replaced flag with is_negative --- conversions/hex-bin.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index b1920a9805b1..19aab9e98d12 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -34,7 +34,6 @@ def convert(num: str) ->Union[bool, str]: """ bin_str :str="" - flag :bool = 0 hex_str :str = num.strip() if not hex_str: @@ -43,7 +42,6 @@ def convert(num: str) ->Union[bool, str]: is_negative : bool= hex_str[0] == "-" if is_negative: - flag = 1 hex_string = hex_str[1:] try: num2 :int = int(hex_str, 16) @@ -55,7 +53,7 @@ def convert(num: str) ->Union[bool, str]: bin_str = num3 + bin_str num2 = num2 >> 1 - return "-" + "".join(bin_str) if flag else bin_str + return "-" + "".join(bin_str) if is_negative else bin_str From a5c49a1889269657162d67b6958f62025913d4aa Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Wed, 19 May 2021 14:00:32 +0530 Subject: [PATCH 17/28] updated return type --- conversions/hex-bin.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 19aab9e98d12..a55c65874c61 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,8 +1,14 @@ # CONVERT HEXADECIMAL TO BINARY +python3 -m pip install pre-commit +pre-commit install +python3 -m pip install black # only required the first time +black . + import math from typing import Union -def convert(num: str) ->Union[bool, str]: + +def convert(num: str) ->Union[bool, int]: """ Convert a hexadecimal value to its decimal equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary @@ -33,27 +39,27 @@ def convert(num: str) ->Union[bool, str]: """ - bin_str :str="" + bin_str :str = "" hex_str :str = num.strip() if not hex_str: return False - is_negative : bool= hex_str[0] == "-" + is_negative : bool = hex_str[0] == "-" if is_negative: hex_string = hex_str[1:] try: - num2 :int = int(hex_str, 16) + int_num :int = int(hex_str, 16) except ValueError: return False - while num2 > 0: - num3 :str = str(num2 % 2) - bin_str = num3 + bin_str - num2 = num2 >> 1 + while int_num > 0: + str_num :str = str(int_num % 2) + bin_str = str_num + bin_str + int_num = int_num >> 1 - return "-" + "".join(bin_str) if is_negative else bin_str + return int("-" + "".join(bin_str)) if is_negative else int(bin_str) From 95e9da19e25844a53291af262a3b883b2c8a9d0a Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Wed, 19 May 2021 14:02:40 +0530 Subject: [PATCH 18/28] removed pip command --- conversions/hex-bin.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index a55c65874c61..e4ea1841091f 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,9 +1,4 @@ # CONVERT HEXADECIMAL TO BINARY -python3 -m pip install pre-commit -pre-commit install -python3 -m pip install black # only required the first time -black . - import math from typing import Union From 77aec40a848270de6d0c147680955c409b696a21 Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Wed, 19 May 2021 15:36:08 +0530 Subject: [PATCH 19/28] Resolved doctest issue --- conversions/hex-bin.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index e4ea1841091f..caccad4d2fcb 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -24,14 +24,9 @@ def convert(num: str) ->Union[bool, int]: >>> convert("FfFf") 1111111111111111 >>> convert("F-f") - Traceback (most recent call last): - ... - ValueError: invalid literal for int() with base 16: + False >>> convert() - Traceback (most recent call last): - ... - ValueError: Empty string was passed to the function: - + False """ bin_str :str = "" From 14a2d8b5a6681ab282be6209a43ff07ed3014daa Mon Sep 17 00:00:00 2001 From: TANMAY SRIVASTAVA <77936821+ktsrivastava29@users.noreply.github.com> Date: Wed, 19 May 2021 16:08:55 +0530 Subject: [PATCH 20/28] Resolved doctest error --- conversions/hex-bin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index caccad4d2fcb..11f861c056cb 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -25,7 +25,7 @@ def convert(num: str) ->Union[bool, int]: 1111111111111111 >>> convert("F-f") False - >>> convert() + >>> convert("") False """ From 350817e7876a4cf0bebda6b80df54f3419939548 Mon Sep 17 00:00:00 2001 From: Tanmay Srivastava Date: Wed, 19 May 2021 17:04:56 +0530 Subject: [PATCH 21/28] Reformated the code --- arithmetic_analysis/newton_method.py | 6 +---- conversions/hex-bin.py | 27 +++++++++---------- matrix/matrix_operation.py | 4 +-- .../can_string_be_rearranged_as_palindrome.py | 4 +-- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/arithmetic_analysis/newton_method.py b/arithmetic_analysis/newton_method.py index a9a94372671e..d1b116567722 100644 --- a/arithmetic_analysis/newton_method.py +++ b/arithmetic_analysis/newton_method.py @@ -7,11 +7,7 @@ # function is the f(x) and derivative is the f'(x) -def newton( - function: RealFunc, - derivative: RealFunc, - starting_int: int, -) -> float: +def newton(function: RealFunc, derivative: RealFunc, starting_int: int,) -> float: """ >>> newton(lambda x: x ** 3 - 2 * x - 5, lambda x: 3 * x ** 2 - 2, 3) 2.0945514815423474 diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 11f861c056cb..772faaa5150e 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -3,7 +3,7 @@ from typing import Union -def convert(num: str) ->Union[bool, int]: +def convert(num: str) -> Union[bool, int]: """ Convert a hexadecimal value to its decimal equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary @@ -29,29 +29,28 @@ def convert(num: str) ->Union[bool, int]: False """ - bin_str :str = "" - hex_str :str = num.strip() - + bin_str: str = "" + hex_str: str = num.strip() + if not hex_str: return False - - is_negative : bool = hex_str[0] == "-" - + + is_negative: bool = hex_str[0] == "-" + if is_negative: - hex_string = hex_str[1:] + hex_str = hex_str[1:] try: - int_num :int = int(hex_str, 16) + int_num: int = int(hex_str, 16) except ValueError: return False - + while int_num > 0: - str_num :str = str(int_num % 2) + str_num: str = str(int_num % 2) bin_str = str_num + bin_str int_num = int_num >> 1 - + return int("-" + "".join(bin_str)) if is_negative else int(bin_str) - - + if __name__ == "__main__": import doctest diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index dca01f9c3183..83059ca655ba 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -168,9 +168,7 @@ def main(): matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]] matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] print(f"Add Operation, {add(matrix_a, matrix_b) = } \n") - print( - f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n", - ) + print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",) print(f"Identity: {identity(5)}\n") print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n") print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n") diff --git a/strings/can_string_be_rearranged_as_palindrome.py b/strings/can_string_be_rearranged_as_palindrome.py index 7fedc5877e26..92bc3b95b243 100644 --- a/strings/can_string_be_rearranged_as_palindrome.py +++ b/strings/can_string_be_rearranged_as_palindrome.py @@ -8,9 +8,7 @@ # Counter is faster for long strings and non-Counter is faster for short strings. -def can_string_be_rearranged_as_palindrome_counter( - input_str: str = "", -) -> bool: +def can_string_be_rearranged_as_palindrome_counter(input_str: str = "",) -> bool: """ A Palindrome is a String that reads the same forward as it does backwards. Examples of Palindromes mom, dad, malayalam From e6abb3803cee41e44604ebdad240e0031e1513bf Mon Sep 17 00:00:00 2001 From: Tanmay Srivastava Date: Thu, 20 May 2021 10:11:22 +0530 Subject: [PATCH 22/28] Changes function name --- conversions/hex-bin.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 772faaa5150e..1f348935285c 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -1,9 +1,8 @@ # CONVERT HEXADECIMAL TO BINARY import math -from typing import Union -def convert(num: str) -> Union[bool, int]: +def hex_to_bin(hex_num: str) -> int: """ Convert a hexadecimal value to its decimal equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary @@ -15,22 +14,22 @@ def convert(num: str) -> Union[bool, int]: a = 10 a >> 1 = 5 - >>> convert("AC") + >>> hex_to_bin("AC") 10101100 - >>> convert("9A4") + >>> hex_to_bin("9A4") 100110100100 - >>> convert(" 12f ") + >>> hex_to_bin(" 12f ") 100101111 - >>> convert("FfFf") + >>> hex_to_bin("FfFf") 1111111111111111 - >>> convert("F-f") + >>> hex_to_bin("F-f") False - >>> convert("") + >>> hex_to_bin("") False """ bin_str: str = "" - hex_str: str = num.strip() + hex_str: str = hex_num.strip() if not hex_str: return False From 62a52e258664880c3146966a433e5b6772a6759c Mon Sep 17 00:00:00 2001 From: Tanmay Srivastava Date: Thu, 20 May 2021 12:56:08 +0530 Subject: [PATCH 23/28] Changed exception handling statements --- conversions/hex-bin.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/conversions/hex-bin.py b/conversions/hex-bin.py index 1f348935285c..9cfcfd5773a9 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex-bin.py @@ -23,16 +23,20 @@ def hex_to_bin(hex_num: str) -> int: >>> hex_to_bin("FfFf") 1111111111111111 >>> hex_to_bin("F-f") - False + Traceback (most recent call last): + ... + ValueError: Invalid value was passed to the function >>> hex_to_bin("") - False + Traceback (most recent call last): + ... + ValueError: No value was passed to the function """ bin_str: str = "" hex_str: str = hex_num.strip() if not hex_str: - return False + raise ValueError("No value was passed to the function") is_negative: bool = hex_str[0] == "-" @@ -41,8 +45,7 @@ def hex_to_bin(hex_num: str) -> int: try: int_num: int = int(hex_str, 16) except ValueError: - return False - + raise ValueError("Invalid value was passed to the function") while int_num > 0: str_num: str = str(int_num % 2) bin_str = str_num + bin_str From c02fa6a2abfd23f15fedde8e5f69ef4172045d38 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 20 May 2021 10:11:09 +0200 Subject: [PATCH 24/28] Update and rename hex-bin.py to hex_to_bin.py --- conversions/{hex-bin.py => hex_to_bin.py} | 41 +++++++++++------------ 1 file changed, 19 insertions(+), 22 deletions(-) rename conversions/{hex-bin.py => hex_to_bin.py} (64%) diff --git a/conversions/hex-bin.py b/conversions/hex_to_bin.py similarity index 64% rename from conversions/hex-bin.py rename to conversions/hex_to_bin.py index 9cfcfd5773a9..155ec3ea193c 100644 --- a/conversions/hex-bin.py +++ b/conversions/hex_to_bin.py @@ -1,19 +1,14 @@ -# CONVERT HEXADECIMAL TO BINARY -import math - - def hex_to_bin(hex_num: str) -> int: """ - Convert a hexadecimal value to its decimal equivalent + Convert a hexadecimal value to its binary equivalent #https://stackoverflow.com/questions/1425493/convert-hex-to-binary - Here, we have used " >> " bitwise operator - Bitwise right shift: - Shifts the bits of the number to the right and fills 0 on voids left as a result. + Here, we have used the bitwise right shift operator: >> + Shifts the bits of the number to the right and fills 0 on voids left as a result. Similar effect as of dividing the number with some power of two. - Example: + Example: a = 10 - a >> 1 = 5 - + a >> 1 = 5 + >>> hex_to_bin("AC") 10101100 >>> hex_to_bin("9A4") @@ -22,6 +17,8 @@ def hex_to_bin(hex_num: str) -> int: 100101111 >>> hex_to_bin("FfFf") 1111111111111111 + >>> hex_to_bin("-fFfF") + -1111111111111111 >>> hex_to_bin("F-f") Traceback (most recent call last): ... @@ -32,26 +29,26 @@ def hex_to_bin(hex_num: str) -> int: ValueError: No value was passed to the function """ - bin_str: str = "" - hex_str: str = hex_num.strip() - - if not hex_str: + hex_num = hex_num.strip() + if not hex_num: raise ValueError("No value was passed to the function") - is_negative: bool = hex_str[0] == "-" - + is_negative = hex_num[0] == "-" if is_negative: - hex_str = hex_str[1:] + hex_num = hex_num[1:] + try: - int_num: int = int(hex_str, 16) + int_num = int(hex_num, 16) except ValueError: raise ValueError("Invalid value was passed to the function") + + bin_str = "" while int_num > 0: - str_num: str = str(int_num % 2) + str_num = str(int_num % 2) bin_str = str_num + bin_str - int_num = int_num >> 1 + int_num >>= 1 - return int("-" + "".join(bin_str)) if is_negative else int(bin_str) + return int(("-" + bin_str) if is_negative else bin_str) if __name__ == "__main__": From 213e22a354687d1451fa018cf71995f1a09ba855 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 20 May 2021 10:11:54 +0200 Subject: [PATCH 25/28] Update newton_method.py --- arithmetic_analysis/newton_method.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arithmetic_analysis/newton_method.py b/arithmetic_analysis/newton_method.py index d1b116567722..a9a94372671e 100644 --- a/arithmetic_analysis/newton_method.py +++ b/arithmetic_analysis/newton_method.py @@ -7,7 +7,11 @@ # function is the f(x) and derivative is the f'(x) -def newton(function: RealFunc, derivative: RealFunc, starting_int: int,) -> float: +def newton( + function: RealFunc, + derivative: RealFunc, + starting_int: int, +) -> float: """ >>> newton(lambda x: x ** 3 - 2 * x - 5, lambda x: 3 * x ** 2 - 2, 3) 2.0945514815423474 From e24b8a325201498694fb278178c30fd865f1dcae Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 20 May 2021 10:12:28 +0200 Subject: [PATCH 26/28] Update matrix_operation.py --- matrix/matrix_operation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index 83059ca655ba..dca01f9c3183 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -168,7 +168,9 @@ def main(): matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]] matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] print(f"Add Operation, {add(matrix_a, matrix_b) = } \n") - print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",) + print( + f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n", + ) print(f"Identity: {identity(5)}\n") print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n") print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n") From 2d9cf6313c4801981f1badb80890fb645cf0a5d2 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 20 May 2021 10:12:57 +0200 Subject: [PATCH 27/28] Update can_string_be_rearranged_as_palindrome.py --- strings/can_string_be_rearranged_as_palindrome.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/strings/can_string_be_rearranged_as_palindrome.py b/strings/can_string_be_rearranged_as_palindrome.py index 92bc3b95b243..7fedc5877e26 100644 --- a/strings/can_string_be_rearranged_as_palindrome.py +++ b/strings/can_string_be_rearranged_as_palindrome.py @@ -8,7 +8,9 @@ # Counter is faster for long strings and non-Counter is faster for short strings. -def can_string_be_rearranged_as_palindrome_counter(input_str: str = "",) -> bool: +def can_string_be_rearranged_as_palindrome_counter( + input_str: str = "", +) -> bool: """ A Palindrome is a String that reads the same forward as it does backwards. Examples of Palindromes mom, dad, malayalam From a80cadd898d4605e2046cce463bcfc25b56dbbb8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 20 May 2021 10:24:23 +0200 Subject: [PATCH 28/28] Update hex_to_bin.py --- conversions/hex_to_bin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/conversions/hex_to_bin.py b/conversions/hex_to_bin.py index 155ec3ea193c..e358d810b581 100644 --- a/conversions/hex_to_bin.py +++ b/conversions/hex_to_bin.py @@ -44,8 +44,7 @@ def hex_to_bin(hex_num: str) -> int: bin_str = "" while int_num > 0: - str_num = str(int_num % 2) - bin_str = str_num + bin_str + bin_str = str(int_num % 2) + bin_str int_num >>= 1 return int(("-" + bin_str) if is_negative else bin_str)