From 34c1907cb2eff9b4f5425aafdb40e77b1a6f98bd Mon Sep 17 00:00:00 2001 From: Jasper <46252815+jasper256@users.noreply.github.com> Date: Wed, 24 Jul 2019 21:26:02 -0400 Subject: [PATCH 1/4] added automated doctest to decimal_to_hexadecimal.py in conversions --- conversions/decimal_to_hexadecimal.py | 28 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/conversions/decimal_to_hexadecimal.py b/conversions/decimal_to_hexadecimal.py index f91fac063adc..3d7e690b5458 100644 --- a/conversions/decimal_to_hexadecimal.py +++ b/conversions/decimal_to_hexadecimal.py @@ -21,7 +21,21 @@ } def decimal_to_hexadecimal(decimal): - """ take decimal value, return hexadecimal representation as str """ + """ + take integer decimal value, return hexadecimal representation as str + >>> decimal_to_hexadecimal(5) + '5' + >>> decimal_to_hexadecimal(15) + 'f' + >>> decimal_to_hexadecimal(37) + '25' + >>> decimal_to_hexadecimal(255) + 'ff' + >>> decimal_to_hexadecimal(4096) + '1000' + >>> decimal_to_hexadecimal(999098) + 'f3eba' + """ hexadecimal = '' while decimal > 0: remainder = decimal % 16 @@ -30,14 +44,6 @@ def decimal_to_hexadecimal(decimal): decimal /= 16 return hexadecimal -def main(): - """ print test cases """ - print("5 in hexadecimal is", decimal_to_hexadecimal(5)) - print("15 in hexadecimal is", decimal_to_hexadecimal(15)) - print("37 in hexadecimal is", decimal_to_hexadecimal(37)) - print("255 in hexadecimal is", decimal_to_hexadecimal(255)) - print("4096 in hexadecimal is", decimal_to_hexadecimal(4096)) - print("999098 in hexadecimal is", decimal_to_hexadecimal(999098)) - if __name__ == '__main__': - main() \ No newline at end of file + import doctest + doctest.testmod() From 2d5a64d8f6b41af256c7b9b99ff480eaee7c2bef Mon Sep 17 00:00:00 2001 From: Jasper <46252815+jasper256@users.noreply.github.com> Date: Thu, 25 Jul 2019 20:44:30 -0400 Subject: [PATCH 2/4] improved error handling and added more test cases in decimal_to_hexadecimal.py --- conversions/decimal_to_hexadecimal.py | 37 ++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/conversions/decimal_to_hexadecimal.py b/conversions/decimal_to_hexadecimal.py index 3d7e690b5458..953f3b140280 100644 --- a/conversions/decimal_to_hexadecimal.py +++ b/conversions/decimal_to_hexadecimal.py @@ -35,13 +35,44 @@ def decimal_to_hexadecimal(decimal): '1000' >>> decimal_to_hexadecimal(999098) 'f3eba' + >>> # negatives work too + >>> decimal_to_hexadecimal(-256) + '-100' + >>> # floats are acceptable if equivalent to an int + >>> decimal_to_hexadecimal(17.0) + '11' + >>> # other floats will error + >>> decimal_to_hexadecimal(16.16) + Traceback (most recent call last): + File "doctest.py", line 1329, in __run + compileflags, 1), test.globs) + File "", line 1, in + decimal_to_hexadecimal(16.16) + File "decimal_to_hexadecimal.py", line 51, in decimal_to_hexadecimal + assert type(decimal) in (int, float) and decimal == int(decimal) + AssertionError + >>> # strings will error as well + >>> decimal_to_hexadecimal('0xfffff') + Traceback (most recent call last): + File "doctest.py", line 1329, in __run + compileflags, 1), test.globs) + File "", line 1, in + decimal_to_hexadecimal('0xfffff') + File "decimal_to_hexadecimal.py", line 58, in decimal_to_hexadecimal + assert type(decimal) in (int, float) and decimal == int(decimal) + AssertionError """ + assert type(decimal) in (int, float) and decimal == int(decimal) hexadecimal = '' + negative = False + if decimal < 0: + negative = True + decimal *= -1 while decimal > 0: - remainder = decimal % 16 - decimal -= remainder + decimal, remainder = divmod(decimal, 16) hexadecimal = values[remainder] + hexadecimal - decimal /= 16 + if negative: + hexadecimal = '-' + hexadecimal return hexadecimal if __name__ == '__main__': From f299daa83fd296615e611be87ee12d8806d54adb Mon Sep 17 00:00:00 2001 From: Jasper <46252815+jasper256@users.noreply.github.com> Date: Fri, 26 Jul 2019 11:55:04 -0400 Subject: [PATCH 3/4] implemented 0x notation and simplified AssertionError --- conversions/decimal_to_hexadecimal.py | 37 ++++++++++----------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/conversions/decimal_to_hexadecimal.py b/conversions/decimal_to_hexadecimal.py index 953f3b140280..08c214b2f740 100644 --- a/conversions/decimal_to_hexadecimal.py +++ b/conversions/decimal_to_hexadecimal.py @@ -22,44 +22,34 @@ def decimal_to_hexadecimal(decimal): """ - take integer decimal value, return hexadecimal representation as str + take integer decimal value, return hexadecimal representation as str beginning with 0x >>> decimal_to_hexadecimal(5) - '5' + '0x5' >>> decimal_to_hexadecimal(15) - 'f' + '0xf' >>> decimal_to_hexadecimal(37) - '25' + '0x25' >>> decimal_to_hexadecimal(255) - 'ff' + '0xff' >>> decimal_to_hexadecimal(4096) - '1000' + '0x1000' >>> decimal_to_hexadecimal(999098) - 'f3eba' + '0xf3eba' >>> # negatives work too >>> decimal_to_hexadecimal(-256) - '-100' + '0x-100' >>> # floats are acceptable if equivalent to an int >>> decimal_to_hexadecimal(17.0) - '11' + '0x11' >>> # other floats will error - >>> decimal_to_hexadecimal(16.16) + >>> decimal_to_hexadecimal(16.16) # doctest: +ELLIPSIS Traceback (most recent call last): - File "doctest.py", line 1329, in __run - compileflags, 1), test.globs) - File "", line 1, in - decimal_to_hexadecimal(16.16) - File "decimal_to_hexadecimal.py", line 51, in decimal_to_hexadecimal - assert type(decimal) in (int, float) and decimal == int(decimal) + ... AssertionError >>> # strings will error as well - >>> decimal_to_hexadecimal('0xfffff') + >>> decimal_to_hexadecimal('0xfffff') # doctest: +ELLIPSIS Traceback (most recent call last): - File "doctest.py", line 1329, in __run - compileflags, 1), test.globs) - File "", line 1, in - decimal_to_hexadecimal('0xfffff') - File "decimal_to_hexadecimal.py", line 58, in decimal_to_hexadecimal - assert type(decimal) in (int, float) and decimal == int(decimal) + ... AssertionError """ assert type(decimal) in (int, float) and decimal == int(decimal) @@ -73,6 +63,7 @@ def decimal_to_hexadecimal(decimal): hexadecimal = values[remainder] + hexadecimal if negative: hexadecimal = '-' + hexadecimal + hexadecimal = '0x' + hexadecimal return hexadecimal if __name__ == '__main__': From ea565f433992fc28dae54200ef6af9308b79c621 Mon Sep 17 00:00:00 2001 From: Jasper <46252815+jasper256@users.noreply.github.com> Date: Fri, 26 Jul 2019 12:23:00 -0400 Subject: [PATCH 4/4] fixed negative notation and added comparison test against Python hex function --- conversions/decimal_to_hexadecimal.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/conversions/decimal_to_hexadecimal.py b/conversions/decimal_to_hexadecimal.py index 08c214b2f740..e6435f1ef570 100644 --- a/conversions/decimal_to_hexadecimal.py +++ b/conversions/decimal_to_hexadecimal.py @@ -37,7 +37,7 @@ def decimal_to_hexadecimal(decimal): '0xf3eba' >>> # negatives work too >>> decimal_to_hexadecimal(-256) - '0x-100' + '-0x100' >>> # floats are acceptable if equivalent to an int >>> decimal_to_hexadecimal(17.0) '0x11' @@ -51,6 +51,9 @@ def decimal_to_hexadecimal(decimal): Traceback (most recent call last): ... AssertionError + >>> # results are the same when compared to Python's default hex function + >>> decimal_to_hexadecimal(-256) == hex(-256) + True """ assert type(decimal) in (int, float) and decimal == int(decimal) hexadecimal = '' @@ -61,9 +64,9 @@ def decimal_to_hexadecimal(decimal): while decimal > 0: decimal, remainder = divmod(decimal, 16) hexadecimal = values[remainder] + hexadecimal + hexadecimal = '0x' + hexadecimal if negative: hexadecimal = '-' + hexadecimal - hexadecimal = '0x' + hexadecimal return hexadecimal if __name__ == '__main__':