From 72ecc1ed54aa3773ee343e4c4d5ba98809370195 Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal Date: Sun, 15 Oct 2023 17:40:52 +0530 Subject: [PATCH 1/8] Added Octal to Hexadecimal Conversion program under 'conversions' directory --- conversions/octal_to_hexadecimal.py | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 conversions/octal_to_hexadecimal.py diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py new file mode 100644 index 000000000000..345d5fbd6b66 --- /dev/null +++ b/conversions/octal_to_hexadecimal.py @@ -0,0 +1,73 @@ +def octal_to_hex(octal: str) -> str: + """ + Convert an Octal number to Hexadecimal number. + + >>> octal_to_hex("100") + '0X40' + >>> octal_to_hex("235") + '0X9D' + >>> octal_to_hex(17) + Traceback (most recent call last): + ... + TypeError: Expected a string as input + >>> octal_to_hex("Av") + Traceback (most recent call last): + ... + ValueError: Not a Valid Octal Number + >>> octal_to_hex("") + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function + + For more information: https://en.wikipedia.org/wiki/Octal + """ + + if not isinstance(octal, str): + raise TypeError("Expected a string as input") + if octal.startswith("0o"): + octal = octal[2:] + if octal == "": + raise ValueError("Empty string was passed to the function") + for char in octal: + if char not in "01234567": + raise ValueError("Not a Valid Octal Number") + + decimal = 0 + for char in str(octal): + decimal <<= 3 + decimal |= int(char) + + hex_char = "0123456789ABCDEF" + + revhex = "" + while decimal: + revhex += hex_char[decimal & 15] + decimal >>= 4 + + hexadecimal = "0X" + revhex[::-1] + return hexadecimal + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + nums = ["030", "100", "247", "235", "007"] + + ## Main Tests + + for num in nums: + hexadecimal = octal_to_hex(num) + expected = hex(int(num, 8)).upper() + + assert hexadecimal == expected + + print( + "Hex of '0o" + + num + + "' is : " + + hexadecimal + + " - and Expected was : " + + expected + ) From 7c20a126429d041f5ec91831e55c8f61d56f0412 Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal <94402194+developer-kush@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:51:26 +0530 Subject: [PATCH 2/8] Update conversions/octal_to_hexadecimal.py fix: minor improvement to directly return hexadecimal value Co-authored-by: Tianyi Zheng --- conversions/octal_to_hexadecimal.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py index 345d5fbd6b66..221eb5a09de1 100644 --- a/conversions/octal_to_hexadecimal.py +++ b/conversions/octal_to_hexadecimal.py @@ -44,8 +44,7 @@ def octal_to_hex(octal: str) -> str: revhex += hex_char[decimal & 15] decimal >>= 4 - hexadecimal = "0X" + revhex[::-1] - return hexadecimal + return "0x" + revhex[::-1] if __name__ == "__main__": From 0b7eefc237f781daddc31d97f909bacbdc55ccce Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal <94402194+developer-kush@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:52:13 +0530 Subject: [PATCH 3/8] Update conversions/octal_to_hexadecimal.py fix: improvement updates to octal to hexadecimal Co-authored-by: Tianyi Zheng --- conversions/octal_to_hexadecimal.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py index 221eb5a09de1..8c959066105b 100644 --- a/conversions/octal_to_hexadecimal.py +++ b/conversions/octal_to_hexadecimal.py @@ -62,11 +62,5 @@ def octal_to_hex(octal: str) -> str: assert hexadecimal == expected - print( - "Hex of '0o" - + num - + "' is : " - + hexadecimal - + " - and Expected was : " - + expected - ) + print(f"Hex of '0o{num}' is: {hexadecimal}") + print(f"Expected was: {expected}") From ea016c926d2f1f7a55326e001e3eca0826c8bf3a Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal <94402194+developer-kush@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:52:51 +0530 Subject: [PATCH 4/8] Update conversions/octal_to_hexadecimal.py fix: Readablility improvements to octal to hexadecimal convertor Co-authored-by: Tianyi Zheng --- conversions/octal_to_hexadecimal.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py index 8c959066105b..085b29e843dc 100644 --- a/conversions/octal_to_hexadecimal.py +++ b/conversions/octal_to_hexadecimal.py @@ -1,6 +1,7 @@ def octal_to_hex(octal: str) -> str: """ Convert an Octal number to Hexadecimal number. + For more information: https://en.wikipedia.org/wiki/Octal >>> octal_to_hex("100") '0X40' @@ -18,8 +19,6 @@ def octal_to_hex(octal: str) -> str: Traceback (most recent call last): ... ValueError: Empty string was passed to the function - - For more information: https://en.wikipedia.org/wiki/Octal """ if not isinstance(octal, str): From cb34c5f70471e828fcfd550b9fe797f18159fb1c Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal <94402194+developer-kush@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:53:42 +0530 Subject: [PATCH 5/8] Update conversions/octal_to_hexadecimal.py fix: readability improvements in octal_to_hexadecimal.py Co-authored-by: Tianyi Zheng --- conversions/octal_to_hexadecimal.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py index 085b29e843dc..d07716c8c3a4 100644 --- a/conversions/octal_to_hexadecimal.py +++ b/conversions/octal_to_hexadecimal.py @@ -27,9 +27,8 @@ def octal_to_hex(octal: str) -> str: octal = octal[2:] if octal == "": raise ValueError("Empty string was passed to the function") - for char in octal: - if char not in "01234567": - raise ValueError("Not a Valid Octal Number") + if any(char not in "01234567" for char in octal) + raise ValueError("Not a Valid Octal Number") decimal = 0 for char in str(octal): From baac75caf43c688057f87372101815df6ecd3217 Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal <94402194+developer-kush@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:54:49 +0530 Subject: [PATCH 6/8] Update conversions/octal_to_hexadecimal.py fix: readability improvements in octal_to_hexadecimal.py Co-authored-by: Tianyi Zheng --- conversions/octal_to_hexadecimal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py index d07716c8c3a4..f882099f78ba 100644 --- a/conversions/octal_to_hexadecimal.py +++ b/conversions/octal_to_hexadecimal.py @@ -31,7 +31,7 @@ def octal_to_hex(octal: str) -> str: raise ValueError("Not a Valid Octal Number") decimal = 0 - for char in str(octal): + for char in octal: decimal <<= 3 decimal |= int(char) From 4a3e04298ca4b6e3fe409225043f173b325a29cf Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal Date: Sun, 15 Oct 2023 21:00:30 +0530 Subject: [PATCH 7/8] fix: Fixed all the errors in octal_to_hexadecimal.py after commiting suggested changes --- conversions/octal_to_hexadecimal.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py index f882099f78ba..1cc414dbee7c 100644 --- a/conversions/octal_to_hexadecimal.py +++ b/conversions/octal_to_hexadecimal.py @@ -27,7 +27,7 @@ def octal_to_hex(octal: str) -> str: octal = octal[2:] if octal == "": raise ValueError("Empty string was passed to the function") - if any(char not in "01234567" for char in octal) + if any(char not in "01234567" for char in octal): raise ValueError("Not a Valid Octal Number") decimal = 0 @@ -42,7 +42,7 @@ def octal_to_hex(octal: str) -> str: revhex += hex_char[decimal & 15] decimal >>= 4 - return "0x" + revhex[::-1] + return "0X" + revhex[::-1] if __name__ == "__main__": @@ -62,3 +62,4 @@ def octal_to_hex(octal: str) -> str: print(f"Hex of '0o{num}' is: {hexadecimal}") print(f"Expected was: {expected}") + print("---") From 77b296e15860e85f66cb8c03d6d4a58c7a1653b2 Mon Sep 17 00:00:00 2001 From: Kushagra Agarwal Date: Mon, 16 Oct 2023 12:20:34 +0530 Subject: [PATCH 8/8] fix: modified the prefix of hex numbers to the '0x' standard in octal_to_hexadecimal.py --- conversions/octal_to_hexadecimal.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conversions/octal_to_hexadecimal.py b/conversions/octal_to_hexadecimal.py index 1cc414dbee7c..0615d79b5c53 100644 --- a/conversions/octal_to_hexadecimal.py +++ b/conversions/octal_to_hexadecimal.py @@ -4,9 +4,9 @@ def octal_to_hex(octal: str) -> str: For more information: https://en.wikipedia.org/wiki/Octal >>> octal_to_hex("100") - '0X40' + '0x40' >>> octal_to_hex("235") - '0X9D' + '0x9D' >>> octal_to_hex(17) Traceback (most recent call last): ... @@ -42,7 +42,7 @@ def octal_to_hex(octal: str) -> str: revhex += hex_char[decimal & 15] decimal >>= 4 - return "0X" + revhex[::-1] + return "0x" + revhex[::-1] if __name__ == "__main__": @@ -56,7 +56,7 @@ def octal_to_hex(octal: str) -> str: for num in nums: hexadecimal = octal_to_hex(num) - expected = hex(int(num, 8)).upper() + expected = "0x" + hex(int(num, 8))[2:].upper() assert hexadecimal == expected