From 763d1b1610c842ef29c6e6dbf95c6e502993b186 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Thu, 3 Sep 2020 19:57:57 +0100 Subject: [PATCH 01/16] Create hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 conversions/hexadecimal_to_decimal diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal new file mode 100644 index 000000000000..b86b31f9e7ef --- /dev/null +++ b/conversions/hexadecimal_to_decimal @@ -0,0 +1,48 @@ +def hexToDecimal(hex_string: str) -> int: + """ + converts a given hexadecimal value to its decimal value + >>> hexToDecimal("a") + 10 + >>> hexToDecimal("") + ValueError: Empty string value was passed to the function + >>> hexToDecimal("-12m") + ValueError: None hexadecimal value was passed to the function + >>> hexToDecimal("12f") + 303 + >>> hexToDecimal(" 12f ") + 303 + """ + decimal_number = 0 + hex_table = { + "1": 1, + "2": 2, + "3": 3, + "4": 4, + "5": 5, + "6": 6, + "7": 7, + "8": 8, + "9": 9, + "A": 10, + "B": 11, + "C": 12, + "D": 13, + "E": 14, + "F": 15, + } + hex_string = hex_string.strip() + if not hex_string: + raise ValueError("Empty string value was passed to the function") + for i in range(len(hex_string)): + if str.upper(hex_string[i]) not in hex_table: + raise ValueError("None hexadecimal value was passed to the function") + decimal_number += (16 ** (len(hex_string) - i - 1)) * int( + hex_table.get(str.upper(hex_string[i])) + ) + return decimal_number + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From dced099bff821da21553ed614079d4a3c4e59d83 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 11:47:03 +0100 Subject: [PATCH 02/16] Update conversions/hexadecimal_to_decimal Co-authored-by: Tapajyoti Bose <44058757+ruppysuppy@users.noreply.github.com> --- conversions/hexadecimal_to_decimal | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index b86b31f9e7ef..7fc1b307f054 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -32,13 +32,11 @@ def hexToDecimal(hex_string: str) -> int: } hex_string = hex_string.strip() if not hex_string: - raise ValueError("Empty string value was passed to the function") - for i in range(len(hex_string)): - if str.upper(hex_string[i]) not in hex_table: - raise ValueError("None hexadecimal value was passed to the function") - decimal_number += (16 ** (len(hex_string) - i - 1)) * int( - hex_table.get(str.upper(hex_string[i])) - ) + raise ValueError("Invalid Hexadecimal Input") + for char in hex_string: + if char.upper() not in hex_table: + raise ValueError("Invalid Hexadecimal Input") + decimal_number = 16 * decimal_number + hex_table[char.upper()] return decimal_number From 1b4db3f41a01706f645cf21795f56030e72a46d4 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 11:49:01 +0100 Subject: [PATCH 03/16] Update conversions/hexadecimal_to_decimal Co-authored-by: Tapajyoti Bose <44058757+ruppysuppy@users.noreply.github.com> --- conversions/hexadecimal_to_decimal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 7fc1b307f054..5787d1ecfdea 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -1,4 +1,4 @@ -def hexToDecimal(hex_string: str) -> int: +def hex_to_decimal(hex_string: str) -> int: """ converts a given hexadecimal value to its decimal value >>> hexToDecimal("a") From 61aa53d495a27302e314ed8c9e1ab955de83d8b6 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 12:50:15 +0100 Subject: [PATCH 04/16] Update conversions/hexadecimal_to_decimal Co-authored-by: Christian Clauss --- conversions/hexadecimal_to_decimal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 5787d1ecfdea..953b288a3835 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -6,7 +6,7 @@ def hex_to_decimal(hex_string: str) -> int: >>> hexToDecimal("") ValueError: Empty string value was passed to the function >>> hexToDecimal("-12m") - ValueError: None hexadecimal value was passed to the function + ValueError: Non-hexadecimal value was passed to the function >>> hexToDecimal("12f") 303 >>> hexToDecimal(" 12f ") From 1579cf8db3d1e437299b8f75b1b507ce299e2461 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 12:55:09 +0100 Subject: [PATCH 05/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 953b288a3835..65cae33fc1bb 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -13,23 +13,7 @@ def hex_to_decimal(hex_string: str) -> int: 303 """ decimal_number = 0 - hex_table = { - "1": 1, - "2": 2, - "3": 3, - "4": 4, - "5": 5, - "6": 6, - "7": 7, - "8": 8, - "9": 9, - "A": 10, - "B": 11, - "C": 12, - "D": 13, - "E": 14, - "F": 15, - } + hex_table = {hex(i)[2:].upper(): i for i in range(16)} hex_string = hex_string.strip() if not hex_string: raise ValueError("Invalid Hexadecimal Input") From 1365270869fce38c903df4b69a741e0e167bc8b7 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 12:57:15 +0100 Subject: [PATCH 06/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 65cae33fc1bb..a38258732b43 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -13,12 +13,14 @@ def hex_to_decimal(hex_string: str) -> int: 303 """ decimal_number = 0 - hex_table = {hex(i)[2:].upper(): i for i in range(16)} + #https://www.programiz.com/python-programming/methods/built-in/hex + #Hex numbers start with 0x. We use [2:] to get the hex number after 0x + hex_table = {hex(i)[2:]: i for i in range(16)} hex_string = hex_string.strip() if not hex_string: raise ValueError("Invalid Hexadecimal Input") for char in hex_string: - if char.upper() not in hex_table: + if char.lower() not in hex_table: raise ValueError("Invalid Hexadecimal Input") decimal_number = 16 * decimal_number + hex_table[char.upper()] return decimal_number From 664adb6e0a31b811973dfab3112f757e743bfb09 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 12:57:35 +0100 Subject: [PATCH 07/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index a38258732b43..e98985f9d221 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -16,11 +16,11 @@ def hex_to_decimal(hex_string: str) -> int: #https://www.programiz.com/python-programming/methods/built-in/hex #Hex numbers start with 0x. We use [2:] to get the hex number after 0x hex_table = {hex(i)[2:]: i for i in range(16)} - hex_string = hex_string.strip() + hex_string = hex_string.strip().lower() if not hex_string: raise ValueError("Invalid Hexadecimal Input") for char in hex_string: - if char.lower() not in hex_table: + if char not in hex_table: raise ValueError("Invalid Hexadecimal Input") decimal_number = 16 * decimal_number + hex_table[char.upper()] return decimal_number From 234e4bd6a18b716b31f373f2114562e79712e8e4 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:10:47 +0100 Subject: [PATCH 08/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index e98985f9d221..ca374241c96d 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -18,10 +18,10 @@ def hex_to_decimal(hex_string: str) -> int: hex_table = {hex(i)[2:]: i for i in range(16)} hex_string = hex_string.strip().lower() if not hex_string: - raise ValueError("Invalid Hexadecimal Input") + raise ValueError("Empty string value was passed to the function") for char in hex_string: if char not in hex_table: - raise ValueError("Invalid Hexadecimal Input") + raise ValueError("Non-hexadecimal value was passed to the function") decimal_number = 16 * decimal_number + hex_table[char.upper()] return decimal_number From 26789525e1b9beb15ceaf5420ab2f6f48336846a Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:11:42 +0100 Subject: [PATCH 09/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index ca374241c96d..1a231768697d 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -1,15 +1,15 @@ def hex_to_decimal(hex_string: str) -> int: """ converts a given hexadecimal value to its decimal value - >>> hexToDecimal("a") + >>> hex_to_decimal("a") 10 - >>> hexToDecimal("") + >>> hex_to_decimal("") ValueError: Empty string value was passed to the function - >>> hexToDecimal("-12m") + >>> hex_to_decimal("-12m") ValueError: Non-hexadecimal value was passed to the function - >>> hexToDecimal("12f") + >>> hex_to_decimal("12f") 303 - >>> hexToDecimal(" 12f ") + >>> hex_to_decimal(" 12f ") 303 """ decimal_number = 0 From ee8edad9926886f338529833a1f9c0321989881e Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:29:53 +0100 Subject: [PATCH 10/16] Update conversions/hexadecimal_to_decimal Co-authored-by: Christian Clauss --- conversions/hexadecimal_to_decimal | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 1a231768697d..49787608b6b6 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -11,6 +11,8 @@ def hex_to_decimal(hex_string: str) -> int: 303 >>> hex_to_decimal(" 12f ") 303 + >>> hex_to_decimal("FfFf") + 65535 """ decimal_number = 0 #https://www.programiz.com/python-programming/methods/built-in/hex From 1889cc778c2e18fc32b88dc3a0b15c04bd498a00 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:44:09 +0100 Subject: [PATCH 11/16] Update hexadecimal_to_decimal Added negative hexadecimal conversion to decimal number --- conversions/hexadecimal_to_decimal | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 49787608b6b6..d1aac71212fb 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -5,7 +5,7 @@ def hex_to_decimal(hex_string: str) -> int: 10 >>> hex_to_decimal("") ValueError: Empty string value was passed to the function - >>> hex_to_decimal("-12m") + >>> hex_to_decimal("12m") ValueError: Non-hexadecimal value was passed to the function >>> hex_to_decimal("12f") 303 @@ -13,22 +13,31 @@ def hex_to_decimal(hex_string: str) -> int: 303 >>> hex_to_decimal("FfFf") 65535 + >>> hex_to_decimal("-Ff") + -255 + >>> hex_to_decimal("F-f") + ValueError: Non-hexadecimal value was passed to the function """ decimal_number = 0 #https://www.programiz.com/python-programming/methods/built-in/hex #Hex numbers start with 0x. We use [2:] to get the hex number after 0x hex_table = {hex(i)[2:]: i for i in range(16)} hex_string = hex_string.strip().lower() + isNegative = False if not hex_string: raise ValueError("Empty string value was passed to the function") + if(hex_string[0] == "-"): + isNegative = True + hex_string = hex_string[1:] for char in hex_string: if char not in hex_table: raise ValueError("Non-hexadecimal value was passed to the function") - decimal_number = 16 * decimal_number + hex_table[char.upper()] + decimal_number = 16 * decimal_number + hex_table[char] + if isNegative: + decimal_number *= -1 return decimal_number if __name__ == "__main__": from doctest import testmod - testmod() From cd6eac336271ebb681158c6bcaf25ee6e7c21285 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:45:48 +0100 Subject: [PATCH 12/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index d1aac71212fb..3074528b8a2e 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -21,7 +21,6 @@ def hex_to_decimal(hex_string: str) -> int: decimal_number = 0 #https://www.programiz.com/python-programming/methods/built-in/hex #Hex numbers start with 0x. We use [2:] to get the hex number after 0x - hex_table = {hex(i)[2:]: i for i in range(16)} hex_string = hex_string.strip().lower() isNegative = False if not hex_string: @@ -40,4 +39,5 @@ def hex_to_decimal(hex_string: str) -> int: if __name__ == "__main__": from doctest import testmod + hex_table = {hex(i)[2:]: i for i in range(16)} testmod() From 8f35319e7773cb23263d970afd4f7779474ec2b4 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:52:07 +0100 Subject: [PATCH 13/16] Update conversions/hexadecimal_to_decimal Co-authored-by: Christian Clauss --- conversions/hexadecimal_to_decimal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 3074528b8a2e..8c383576e37e 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -25,7 +25,7 @@ def hex_to_decimal(hex_string: str) -> int: isNegative = False if not hex_string: raise ValueError("Empty string value was passed to the function") - if(hex_string[0] == "-"): + if hex_string[0] == "-": isNegative = True hex_string = hex_string[1:] for char in hex_string: From a361d921558d7df697c5109e01c00db5b8eee5d7 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:52:37 +0100 Subject: [PATCH 14/16] Update conversions/hexadecimal_to_decimal Co-authored-by: Christian Clauss --- conversions/hexadecimal_to_decimal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 8c383576e37e..6ff62c64c86c 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -22,7 +22,7 @@ def hex_to_decimal(hex_string: str) -> int: #https://www.programiz.com/python-programming/methods/built-in/hex #Hex numbers start with 0x. We use [2:] to get the hex number after 0x hex_string = hex_string.strip().lower() - isNegative = False + is_negative = False if not hex_string: raise ValueError("Empty string value was passed to the function") if hex_string[0] == "-": From 8c680cc9f95f727a06b5f55de79e75ac60c45338 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:53:06 +0100 Subject: [PATCH 15/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index 6ff62c64c86c..b1b8582291b9 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -26,13 +26,13 @@ def hex_to_decimal(hex_string: str) -> int: if not hex_string: raise ValueError("Empty string value was passed to the function") if hex_string[0] == "-": - isNegative = True + is_negative = True hex_string = hex_string[1:] for char in hex_string: if char not in hex_table: raise ValueError("Non-hexadecimal value was passed to the function") decimal_number = 16 * decimal_number + hex_table[char] - if isNegative: + if is_negative: decimal_number *= -1 return decimal_number From 7bf7698fca664f2290732a254beda8f3423d8091 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 4 Sep 2020 15:37:13 +0200 Subject: [PATCH 16/16] Update hexadecimal_to_decimal --- conversions/hexadecimal_to_decimal | 36 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal index b1b8582291b9..e87caa0f4787 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal @@ -1,12 +1,13 @@ +hex_table = {hex(i)[2:]: i for i in range(16)} # Use [:2] to strip off the leading '0x' + + def hex_to_decimal(hex_string: str) -> int: """ - converts a given hexadecimal value to its decimal value + Convert a hexadecimal value to its decimal equivalent + #https://www.programiz.com/python-programming/methods/built-in/hex + >>> hex_to_decimal("a") 10 - >>> hex_to_decimal("") - ValueError: Empty string value was passed to the function - >>> hex_to_decimal("12m") - ValueError: Non-hexadecimal value was passed to the function >>> hex_to_decimal("12f") 303 >>> hex_to_decimal(" 12f ") @@ -17,27 +18,28 @@ def hex_to_decimal(hex_string: str) -> int: -255 >>> hex_to_decimal("F-f") ValueError: Non-hexadecimal value was passed to the function + >>> hex_to_decimal("") + ValueError: Empty string value was passed to the function + >>> hex_to_decimal("12m") + ValueError: Non-hexadecimal value was passed to the function """ - decimal_number = 0 - #https://www.programiz.com/python-programming/methods/built-in/hex - #Hex numbers start with 0x. We use [2:] to get the hex number after 0x hex_string = hex_string.strip().lower() - is_negative = False if not hex_string: - raise ValueError("Empty string value was passed to the function") - if hex_string[0] == "-": - is_negative = True - hex_string = hex_string[1:] + raise ValueError("Empty string was passed to the function") + is_negative = hex_string[0] == "-" + if is_negative: + hex_string = hex_string[1:] + if not all(char in hex_table for char in hex_string): + raise ValueError("Non-hexadecimal value was passed to the function") + decimal_number = 0 for char in hex_string: - if char not in hex_table: - raise ValueError("Non-hexadecimal value was passed to the function") decimal_number = 16 * decimal_number + hex_table[char] if is_negative: - decimal_number *= -1 + decimal_number = -decimal_number return decimal_number if __name__ == "__main__": from doctest import testmod - hex_table = {hex(i)[2:]: i for i in range(16)} + testmod()