From f2064e08e61213029534edc35b827c76c6d7be7a Mon Sep 17 00:00:00 2001 From: Syed Waleed Hyder Date: Fri, 2 Aug 2019 20:28:18 +0200 Subject: [PATCH 1/7] bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function. --- conversions/decimal_to_binary.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index 43ceee61a388..2a2ec2776d6e 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -3,21 +3,43 @@ def decimal_to_binary(num): """Convert a Decimal Number to a Binary Number.""" + + if num == 0: + return 0 + + negative = False + + if num < 0: + negative = True + num = -num + binary = [] while num > 0: binary.insert(0, num % 2) num >>= 1 + + if negative: + return "-" + "".join(str(e) for e in binary) + return "".join(str(e) for e in binary) def main(): """Print binary equivelents of decimal numbers.""" + print("\n0 in binary is:") + print(decimal_to_binary(0)) # = 0 print("\n2 in binary is:") print(decimal_to_binary(2)) # = 10 print("\n7 in binary is:") print(decimal_to_binary(7)) # = 111 print("\n35 in binary is:") print(decimal_to_binary(35)) # = 100011 + print("\n-2 in binary is:") + print(decimal_to_binary(-2)) # = -10 + print("\n-7 in binary is:") + print(decimal_to_binary(7)) # = -111 + print("\n-35 in binary is:") + print(decimal_to_binary(-35)) # = -100011 print("\n") From 360a7502f0b5b1c69d6155ed5b6e2cbdd2c7809b Mon Sep 17 00:00:00 2001 From: Syed Waleed Hyder Date: Fri, 2 Aug 2019 20:34:53 +0200 Subject: [PATCH 2/7] bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function. --- conversions/decimal_to_binary.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index 2a2ec2776d6e..3cfad7e5b7ac 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -6,7 +6,7 @@ def decimal_to_binary(num): if num == 0: return 0 - + negative = False if num < 0: @@ -34,7 +34,7 @@ def main(): print(decimal_to_binary(7)) # = 111 print("\n35 in binary is:") print(decimal_to_binary(35)) # = 100011 - print("\n-2 in binary is:") + print("\n-2 in binary is:") print(decimal_to_binary(-2)) # = -10 print("\n-7 in binary is:") print(decimal_to_binary(7)) # = -111 @@ -43,5 +43,5 @@ def main(): print("\n") -if __name__ == '__main__': +if __name__ == "__main__": main() From edf3097a2fd3cfd058ce54a7af6722d5a4948e78 Mon Sep 17 00:00:00 2001 From: Syed Waleed Hyder Date: Fri, 2 Aug 2019 21:24:58 +0200 Subject: [PATCH 3/7] Added doctests. bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function. --- conversions/decimal_to_binary.py | 64 ++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index 3cfad7e5b7ac..1cb2b0cb6b3d 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -4,6 +4,42 @@ def decimal_to_binary(num): """Convert a Decimal Number to a Binary Number.""" + """ + Convert a Integer Decimal Number to a Binary Number as str. + >>> decimal_to_binary(0) + '0' + >>> decimal_to_binary(2) + '10' + >>> decimal_to_binary(7) + '111' + >>> decimal_to_binary(35) + '100011' + >>> decimal_to_hexadecimal(4096) + '0x1000' + >>> decimal_to_hexadecimal(999098) + '0xf3eba' + >>> # negatives work too + >>> decimal_to_binary(-2) + '-10' + >>> # floats are acceptable if equivalent to an int + >>> decimal_to_binary(2) + '10' + >>> # other floats will error + >>> decimal_to_binary(16.16) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + AssertionError + >>> # strings will error as well + >>> decimal_to_binary('0xfffff') # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + AssertionError + >>> # results are the same when compared to Python's default bin function + >>> decimal_to_binary(-256) == bin(-256) + True + """ + assert type(num) in (int, float) and num == int(num) + if num == 0: return 0 @@ -19,29 +55,11 @@ def decimal_to_binary(num): num >>= 1 if negative: - return "-" + "".join(str(e) for e in binary) - - return "".join(str(e) for e in binary) - - -def main(): - """Print binary equivelents of decimal numbers.""" - print("\n0 in binary is:") - print(decimal_to_binary(0)) # = 0 - print("\n2 in binary is:") - print(decimal_to_binary(2)) # = 10 - print("\n7 in binary is:") - print(decimal_to_binary(7)) # = 111 - print("\n35 in binary is:") - print(decimal_to_binary(35)) # = 100011 - print("\n-2 in binary is:") - print(decimal_to_binary(-2)) # = -10 - print("\n-7 in binary is:") - print(decimal_to_binary(7)) # = -111 - print("\n-35 in binary is:") - print(decimal_to_binary(-35)) # = -100011 - print("\n") + return "-0b" + "".join(str(e) for e in binary) + + return "0b" + "".join(str(e) for e in binary) if __name__ == "__main__": - main() + import doctest + doctest.testmod() From 1befbca594c3717eef2a110e490dfc0da552c6f2 Mon Sep 17 00:00:00 2001 From: Syed Waleed Hyder Date: Fri, 2 Aug 2019 22:49:35 +0200 Subject: [PATCH 4/7] Added doctests. bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function. --- conversions/decimal_to_binary.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index 1cb2b0cb6b3d..deb86b817090 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -14,10 +14,6 @@ def decimal_to_binary(num): '111' >>> decimal_to_binary(35) '100011' - >>> decimal_to_hexadecimal(4096) - '0x1000' - >>> decimal_to_hexadecimal(999098) - '0xf3eba' >>> # negatives work too >>> decimal_to_binary(-2) '-10' From e26ccfeb3973fe14349f2590ab97bd9adb52c6b0 Mon Sep 17 00:00:00 2001 From: Syed Waleed Hyder Date: Fri, 2 Aug 2019 23:58:55 +0200 Subject: [PATCH 5/7] Added doctests. bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function. --- conversions/decimal_to_binary.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index deb86b817090..5aef49746d0d 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -2,24 +2,23 @@ def decimal_to_binary(num): - """Convert a Decimal Number to a Binary Number.""" """ Convert a Integer Decimal Number to a Binary Number as str. >>> decimal_to_binary(0) - '0' + '0b0' >>> decimal_to_binary(2) - '10' + '0b10' >>> decimal_to_binary(7) - '111' + '0b111' >>> decimal_to_binary(35) - '100011' + '0b100011' >>> # negatives work too >>> decimal_to_binary(-2) - '-10' + '-0b10' >>> # floats are acceptable if equivalent to an int - >>> decimal_to_binary(2) - '10' + >>> decimal_to_binary(2.0) + '0b10' >>> # other floats will error >>> decimal_to_binary(16.16) # doctest: +ELLIPSIS Traceback (most recent call last): @@ -35,9 +34,9 @@ def decimal_to_binary(num): True """ assert type(num) in (int, float) and num == int(num) - + num = int(num) if num == 0: - return 0 + return "0b0" negative = False From 5669935cec8761fcbd036348c285dd8588bb89e9 Mon Sep 17 00:00:00 2001 From: Syed Waleed Hyder Date: Sat, 3 Aug 2019 14:10:32 +0200 Subject: [PATCH 6/7] doctests still failing. --- conversions/decimal_to_binary.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index 5aef49746d0d..d47fefe4ff9a 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -16,25 +16,30 @@ def decimal_to_binary(num): >>> # negatives work too >>> decimal_to_binary(-2) '-0b10' - >>> # floats are acceptable if equivalent to an int - >>> decimal_to_binary(2.0) - '0b10' >>> # other floats will error >>> decimal_to_binary(16.16) # doctest: +ELLIPSIS Traceback (most recent call last): ... - AssertionError + TypeError: 'float' object cannot be interpreted as an integer >>> # strings will error as well >>> decimal_to_binary('0xfffff') # doctest: +ELLIPSIS Traceback (most recent call last): ... - AssertionError + TypeError: 'str' object cannot be interpreted as an integer >>> # results are the same when compared to Python's default bin function - >>> decimal_to_binary(-256) == bin(-256) + >>> decimal_to_binary(-256) == bin(-256) # doctest: +ELLIPSIS + True + >>> bin("2") == decimal_to_binary("2") # doctest: +ELLIPSIS + True + >>> decimal_to_binary(2.0) == bin(2.0) # doctest: +ELLIPSIS True """ - assert type(num) in (int, float) and num == int(num) - num = int(num) + + if type(num) == float: + raise TypeError("'float' object cannot be interpreted as an integer") + if type(num) == str: + raise TypeError("'str' object cannot be interpreted as an integer") + if num == 0: return "0b0" From 7a2f0780f26b17d731868084dbcf2c44ce7070fd Mon Sep 17 00:00:00 2001 From: Syed Waleed Hyder Date: Sat, 3 Aug 2019 19:55:23 +0200 Subject: [PATCH 7/7] Doctests added. --- conversions/decimal_to_binary.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index d47fefe4ff9a..934cf0dfb363 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -26,15 +26,8 @@ def decimal_to_binary(num): Traceback (most recent call last): ... TypeError: 'str' object cannot be interpreted as an integer - >>> # results are the same when compared to Python's default bin function - >>> decimal_to_binary(-256) == bin(-256) # doctest: +ELLIPSIS - True - >>> bin("2") == decimal_to_binary("2") # doctest: +ELLIPSIS - True - >>> decimal_to_binary(2.0) == bin(2.0) # doctest: +ELLIPSIS - True """ - + if type(num) == float: raise TypeError("'float' object cannot be interpreted as an integer") if type(num) == str: