From 7ee79fc05454551ecfdd20fb944feee6d0c8f477 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 29 Dec 2024 18:20:24 +0200 Subject: [PATCH 01/19] Added .py file --- solutions/binarydecimalconversion.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 solutions/binarydecimalconversion.py diff --git a/solutions/binarydecimalconversion.py b/solutions/binarydecimalconversion.py new file mode 100644 index 000000000..e69de29bb From 03186c00748c55a7fdc6fa315ef7ef5a14a7e63a Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Thu, 2 Jan 2025 02:40:34 +0200 Subject: [PATCH 02/19] Added function --- solutions/binarydecimalconversion.py | 87 ++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/solutions/binarydecimalconversion.py b/solutions/binarydecimalconversion.py index e69de29bb..1de9e58c8 100644 --- a/solutions/binarydecimalconversion.py +++ b/solutions/binarydecimalconversion.py @@ -0,0 +1,87 @@ +""" +A module for converting between binary and decimal numbers. +Created by Muna Sattouf on 29/12/2024 +Completed on 1/1/2025 +""" +def DecimalToBinary(decimal: int)-> str: + """ + This function takes a decimal number from command line as paramter, + and returns its binary representation. + + Example: DecimalToBinary('binary', 392) + >>> 110001000 + + """ + if not isinstance(decimal, int): + raise ValueError("Input must be an integer") + if decimal < 0: + decimal = -decimal + return '-'+ DecimalToBinary(decimal) + if decimal == 0: + return 0 + binarynumber = ' ' + while decimal > 0: + binarynumber = str(decimal % 2)+ binarynumber + decimal = decimal // 2 + return binarynumber + +def BinaryToDecimal(binary: str)-> int: + """ + This function takes a binary number from command line as paramter, + and returns its decimal representation. + + Example: BinaryToDecimal('decimal', 10011101) + >>> 157 + """ + if not all(c in '01' for c in binary): + raise ValueError("Input must only contain 0s and 1s") + decimalnumber = 0 + for c in binary: + decimalnumber = decimalnumber*2 + int(c) + return decimalnumber + +def binarydecimalconversion(conversion_type: str, number:str)-> str: + """ + This is the main function: It 'coordinates' and decides + which of the two above functions to use + """ + if conversion_type == "decimal": + if not all(c in '01' for c in number): + raise ValueError("If decimal conversion, input must be in binary, consisting only of 0 and 1") + return BinaryToDecimal(number) + elif conversion_type == "binary": + if not number.isdigit(): + raise ValueError("If binary conversion, input must be a positive integer") + decimal = int(number) + return DecimalToBinary(decimal) + else: + raise ValueError("Conversion Type must be 'binary' or 'decimal ' ") + +if __name__== "__main__": + + import sys + if len(sys.argv)!=3: + print("To use properly, the first argument is conversion type. the second is the number you want to convert.") + sys.exit(1) + + conversion_type = sys.argv[1].lower() + number = sys.argv[2] + + if conversion_type not in ["binary", "decimal"]: + print("Error: Conversion type must be binary or decimal") + sys.exit(1) + + if conversion_type == 'decimal': + if not all(c in '01' for c in number): + print("Error: for decimal conversion, input must be in binary, containing only 1 and 0") + sys.exit(1) + result = BinaryToDecimal(number) + + elif conversion_type == 'binary': + if not number.isdigit(): + print("Error: for binary conversion, input must be positive integer") + sys.exit(1) + result = DecimalToBinary(int(number)) + + print(result) + \ No newline at end of file From 5366fc39138cb5977af1b868dc20f2b19a444ef9 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Thu, 2 Jan 2025 03:03:03 +0200 Subject: [PATCH 03/19] Fixed --- solutions/binarydecimalconversion.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solutions/binarydecimalconversion.py b/solutions/binarydecimalconversion.py index 1de9e58c8..2c7560c96 100644 --- a/solutions/binarydecimalconversion.py +++ b/solutions/binarydecimalconversion.py @@ -83,5 +83,4 @@ def binarydecimalconversion(conversion_type: str, number:str)-> str: sys.exit(1) result = DecimalToBinary(int(number)) - print(result) - \ No newline at end of file + print(result) \ No newline at end of file From 11ae34216e7d8e156423e4ae926705dd34119eef Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Thu, 2 Jan 2025 03:06:27 +0200 Subject: [PATCH 04/19] Renamed binarydecimalconversion.py to binary_decimal_conversion.py --- .../{binarydecimalconversion.py => binary_decimal_conversion.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solutions/{binarydecimalconversion.py => binary_decimal_conversion.py} (100%) diff --git a/solutions/binarydecimalconversion.py b/solutions/binary_decimal_conversion.py similarity index 100% rename from solutions/binarydecimalconversion.py rename to solutions/binary_decimal_conversion.py From 06a02d9a3f8226020535443fbbc4f3ec794121f0 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Thu, 2 Jan 2025 03:10:03 +0200 Subject: [PATCH 05/19] Fixed --- solutions/binary_decimal_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index 2c7560c96..dc0b77f47 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -18,7 +18,7 @@ def DecimalToBinary(decimal: int)-> str: decimal = -decimal return '-'+ DecimalToBinary(decimal) if decimal == 0: - return 0 + return '0' binarynumber = ' ' while decimal > 0: binarynumber = str(decimal % 2)+ binarynumber From 0f75a625bd69f20289e49c33866d3a8638266168 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Thu, 2 Jan 2025 03:15:40 +0200 Subject: [PATCH 06/19] edited --- solutions/binary_decimal_conversion.py | 72 +++++++++++++++----------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index dc0b77f47..9425b0fb7 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -3,84 +3,96 @@ Created by Muna Sattouf on 29/12/2024 Completed on 1/1/2025 """ -def DecimalToBinary(decimal: int)-> str: + + +def DecimalToBinary(decimal: int) -> str: """ This function takes a decimal number from command line as paramter, and returns its binary representation. - + Example: DecimalToBinary('binary', 392) >>> 110001000 - + """ if not isinstance(decimal, int): raise ValueError("Input must be an integer") if decimal < 0: decimal = -decimal - return '-'+ DecimalToBinary(decimal) + return "-" + DecimalToBinary(decimal) if decimal == 0: - return '0' - binarynumber = ' ' + return "0" + binarynumber = " " while decimal > 0: - binarynumber = str(decimal % 2)+ binarynumber + binarynumber = str(decimal % 2) + binarynumber decimal = decimal // 2 return binarynumber -def BinaryToDecimal(binary: str)-> int: + +def BinaryToDecimal(binary: str) -> int: """ This function takes a binary number from command line as paramter, and returns its decimal representation. - + Example: BinaryToDecimal('decimal', 10011101) >>> 157 """ - if not all(c in '01' for c in binary): + if not all(c in "01" for c in binary): raise ValueError("Input must only contain 0s and 1s") decimalnumber = 0 for c in binary: - decimalnumber = decimalnumber*2 + int(c) + decimalnumber = decimalnumber * 2 + int(c) return decimalnumber -def binarydecimalconversion(conversion_type: str, number:str)-> str: + +def binarydecimalconversion(conversion_type: str, number: str) -> str: """ This is the main function: It 'coordinates' and decides which of the two above functions to use """ if conversion_type == "decimal": - if not all(c in '01' for c in number): - raise ValueError("If decimal conversion, input must be in binary, consisting only of 0 and 1") + if not all(c in "01" for c in number): + raise ValueError( + "If decimal conversion, input must be in binary, consisting only of 0 and 1" + ) return BinaryToDecimal(number) elif conversion_type == "binary": if not number.isdigit(): raise ValueError("If binary conversion, input must be a positive integer") decimal = int(number) return DecimalToBinary(decimal) - else: + else: raise ValueError("Conversion Type must be 'binary' or 'decimal ' ") - -if __name__== "__main__": + + +if __name__ == "__main__": import sys - if len(sys.argv)!=3: - print("To use properly, the first argument is conversion type. the second is the number you want to convert.") + + if len(sys.argv) != 3: + print( + "To use properly, the first argument is conversion type. the second is the number you want to convert." + ) sys.exit(1) - + conversion_type = sys.argv[1].lower() number = sys.argv[2] - + if conversion_type not in ["binary", "decimal"]: print("Error: Conversion type must be binary or decimal") sys.exit(1) - - if conversion_type == 'decimal': - if not all(c in '01' for c in number): - print("Error: for decimal conversion, input must be in binary, containing only 1 and 0") + + if conversion_type == "decimal": + if not all(c in "01" for c in number): + print( + "Error: for decimal conversion, input must be in binary, containing only 1 and 0" + ) sys.exit(1) result = BinaryToDecimal(number) - - elif conversion_type == 'binary': + + elif conversion_type == "binary": if not number.isdigit(): print("Error: for binary conversion, input must be positive integer") sys.exit(1) - result = DecimalToBinary(int(number)) - - print(result) \ No newline at end of file + result = DecimalToBinary(int(number)) + + print(result) From b4e6512658aeec0516c1f408b5150d50c2781b8e Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Thu, 2 Jan 2025 23:16:44 +0200 Subject: [PATCH 07/19] Added unit tests for binary-decimal conversion functions and updated main code --- solutions/binary_decimal_conversion.py | 71 +++++++++++--------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index 9425b0fb7..d8ffe214e 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -3,96 +3,83 @@ Created by Muna Sattouf on 29/12/2024 Completed on 1/1/2025 """ - - -def DecimalToBinary(decimal: int) -> str: +def DecimalToBinary(decimal: int)-> str: """ This function takes a decimal number from command line as paramter, and returns its binary representation. - + Example: DecimalToBinary('binary', 392) >>> 110001000 - + """ if not isinstance(decimal, int): raise ValueError("Input must be an integer") if decimal < 0: decimal = -decimal - return "-" + DecimalToBinary(decimal) + return '-'+ DecimalToBinary(decimal) if decimal == 0: - return "0" - binarynumber = " " + return '0' + binarynumber = ' ' while decimal > 0: - binarynumber = str(decimal % 2) + binarynumber + binarynumber = str(decimal % 2)+ binarynumber decimal = decimal // 2 return binarynumber - -def BinaryToDecimal(binary: str) -> int: +def BinaryToDecimal(binary: str)-> int: """ This function takes a binary number from command line as paramter, and returns its decimal representation. - + Example: BinaryToDecimal('decimal', 10011101) >>> 157 """ - if not all(c in "01" for c in binary): + if not all(c in '01' for c in binary): raise ValueError("Input must only contain 0s and 1s") decimalnumber = 0 for c in binary: - decimalnumber = decimalnumber * 2 + int(c) + decimalnumber = decimalnumber*2 + int(c) return decimalnumber - -def binarydecimalconversion(conversion_type: str, number: str) -> str: +def binarydecimalconversion(conversion_type: str, number:str)-> str: """ This is the main function: It 'coordinates' and decides which of the two above functions to use """ if conversion_type == "decimal": - if not all(c in "01" for c in number): - raise ValueError( - "If decimal conversion, input must be in binary, consisting only of 0 and 1" - ) + if not all(c in '01' for c in number): + raise ValueError("If decimal conversion, input must be in binary, consisting only of 0 and 1") return BinaryToDecimal(number) elif conversion_type == "binary": if not number.isdigit(): raise ValueError("If binary conversion, input must be a positive integer") decimal = int(number) return DecimalToBinary(decimal) - else: + else: raise ValueError("Conversion Type must be 'binary' or 'decimal ' ") - - -if __name__ == "__main__": + +if __name__== "__main__": import sys - - if len(sys.argv) != 3: - print( - "To use properly, the first argument is conversion type. the second is the number you want to convert." - ) + if len(sys.argv)!=3: + print("To use properly, the first argument is conversion type. the second is the number you want to convert.") sys.exit(1) - + conversion_type = sys.argv[1].lower() number = sys.argv[2] - + if conversion_type not in ["binary", "decimal"]: print("Error: Conversion type must be binary or decimal") sys.exit(1) - - if conversion_type == "decimal": - if not all(c in "01" for c in number): - print( - "Error: for decimal conversion, input must be in binary, containing only 1 and 0" - ) + + if conversion_type == 'decimal': + if not all(c in '01' for c in number): + print("Error: for decimal conversion, input must be in binary, containing only 1 and 0") sys.exit(1) result = BinaryToDecimal(number) - - elif conversion_type == "binary": + + elif conversion_type == 'binary': if not number.isdigit(): print("Error: for binary conversion, input must be positive integer") sys.exit(1) - result = DecimalToBinary(int(number)) - - print(result) + result = DecimalToBinary(int(number)) + print(result) \ No newline at end of file From 938f1b580148338fd96d97be6ab18fb911f0c971 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Thu, 2 Jan 2025 23:22:51 +0200 Subject: [PATCH 08/19] Added test for binary-decimal conversion functions --- .../tests/test_binary_decimal_conversion.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 solutions/tests/test_binary_decimal_conversion.py diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py new file mode 100644 index 000000000..f04f76951 --- /dev/null +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -0,0 +1,66 @@ +""" +Module: test_binary_decimal_conversion +contains test cases for the solution code. + +Created by Muna Sattouf on January 2, 2024 +Completed on January 2, 2024 +""" +import unittest +from binary_decimal_conversion import DecimalToBinary, BinaryToDecimal, binarydecimalconversion + +class TestBinaryDecimalConversion(unittest.TestCase): + + def test_decimal_to_binary(self): + """Should convert positive decimal numbers to binary""" + self.assertEqual(DecimalToBinary(10), '1010') + + def test_binary_to_decimal(self): + """Should convert positive binary to decimal""" + self.assertEqual(BinaryToDecimal(10011101), 157) + + def test_zero_decimal_to_binary(self): + """should return 0""" + self.assertEqual(DecimalToBinary(0), '0') + + def test_zero_binary_to_decimal(self): + """should return 0""" + self.assertEqual(BinaryToDecimal(0), '0') + + def test_large_decimal_to_binary(self): + """Should correctly handle large decimal numbers""" + self.assertEqual(DecimalToBinary(1000000), '11110100001001000000') + + def test_large_binary_to_decimal(self): + """Should correctly handle large binary numbers""" + self.assertEqual(BinaryToDecimal('11110100001001000000'), 1000000) + + def test_binarydecimalconversion_decimal(self): + """Should convert binary to decimal when conversion type is decimal""" + self.assertEqual(binarydecimalconversion('decimal', '10011101'), 157) + + def test_binarydecimalconversion_binary(self): + """Should convert decimal to binary when conversion type is binary""" + self.assertEqual(binarydecimalconversion('binary', '392'), 110001000) + + def test_invalid_decimal_input(self): + """Should raise ValueError for non-integer decimal input""" + with self.assertRaises(ValueError): + DecimalToBinary("notAnumber") + + def test_invalid_binary_input(self): + """Should raise ValueError for non-binary input""" + with self.assertRaises(ValueError): + BinaryToDecimal("1234") + + def test_invalid_decimal_conversion_input(self): + """Should raise ValueError if non-binary string is provided to convert to decimal""" + with self.assertRaises(ValueError): + binarydecimalconversion("decimal", "1020") + + def test_invalid_binary_conversion_input(self): + """Should raise ValueError if a non-integer was provided to convert to binary""" + with self.assertRaises(ValueError): + binarydecimalconversion('binary', 'word') + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From c5cf48a0008a5b4151d13e8ac0cbb27759b48431 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 5 Jan 2025 18:44:44 +0200 Subject: [PATCH 09/19] Added test for binary-decimal conversion functions --- solutions/tests/test_binary_decimal_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index f04f76951..13fa63b7b 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -53,7 +53,7 @@ def test_invalid_binary_input(self): BinaryToDecimal("1234") def test_invalid_decimal_conversion_input(self): - """Should raise ValueError if non-binary string is provided to convert to decimal""" + """Should raise ValueError if a non-binary string is provided to convert to decimal""" with self.assertRaises(ValueError): binarydecimalconversion("decimal", "1020") From b6fee20050e698d3e6b908de7892fe8c19b6e7bb Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sat, 11 Jan 2025 20:34:58 +0200 Subject: [PATCH 10/19] changed to snake case --- solutions/binary_decimal_conversion.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index d8ffe214e..7f329c0e7 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -3,7 +3,7 @@ Created by Muna Sattouf on 29/12/2024 Completed on 1/1/2025 """ -def DecimalToBinary(decimal: int)-> str: +def decimal_to_binary(decimal: int)-> str: """ This function takes a decimal number from command line as paramter, and returns its binary representation. @@ -13,6 +13,7 @@ def DecimalToBinary(decimal: int)-> str: """ if not isinstance(decimal, int): + raise ValueError("Input must be an integer") if decimal < 0: decimal = -decimal From 70389674312bf0453d5de316b5a550505706d549 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sat, 11 Jan 2025 20:41:08 +0200 Subject: [PATCH 11/19] updated names of functions --- solutions/binary_decimal_conversion.py | 17 ++++++------ .../tests/test_binary_decimal_conversion.py | 26 +++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index 7f329c0e7..59d5afa33 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -8,16 +8,17 @@ def decimal_to_binary(decimal: int)-> str: This function takes a decimal number from command line as paramter, and returns its binary representation. - Example: DecimalToBinary('binary', 392) + Example: decimal_to_binary('binary', 392) >>> 110001000 """ if not isinstance(decimal, int): raise ValueError("Input must be an integer") + if decimal < 0: decimal = -decimal - return '-'+ DecimalToBinary(decimal) + return '-'+ decimal_to_binary(decimal) if decimal == 0: return '0' binarynumber = ' ' @@ -26,7 +27,7 @@ def decimal_to_binary(decimal: int)-> str: decimal = decimal // 2 return binarynumber -def BinaryToDecimal(binary: str)-> int: +def binary_to_decimal(binary: str)-> int: """ This function takes a binary number from command line as paramter, and returns its decimal representation. @@ -41,7 +42,7 @@ def BinaryToDecimal(binary: str)-> int: decimalnumber = decimalnumber*2 + int(c) return decimalnumber -def binarydecimalconversion(conversion_type: str, number:str)-> str: +def binary_decimal_conversion(conversion_type: str, number:str)-> str: """ This is the main function: It 'coordinates' and decides which of the two above functions to use @@ -49,12 +50,12 @@ def binarydecimalconversion(conversion_type: str, number:str)-> str: if conversion_type == "decimal": if not all(c in '01' for c in number): raise ValueError("If decimal conversion, input must be in binary, consisting only of 0 and 1") - return BinaryToDecimal(number) + return binary_to_decimal(number) elif conversion_type == "binary": if not number.isdigit(): raise ValueError("If binary conversion, input must be a positive integer") decimal = int(number) - return DecimalToBinary(decimal) + return decimal_to_binary(decimal) else: raise ValueError("Conversion Type must be 'binary' or 'decimal ' ") @@ -76,11 +77,11 @@ def binarydecimalconversion(conversion_type: str, number:str)-> str: if not all(c in '01' for c in number): print("Error: for decimal conversion, input must be in binary, containing only 1 and 0") sys.exit(1) - result = BinaryToDecimal(number) + result = binary_to_decimal(number) elif conversion_type == 'binary': if not number.isdigit(): print("Error: for binary conversion, input must be positive integer") sys.exit(1) - result = DecimalToBinary(int(number)) + result = decimal_to_binary(int(number)) print(result) \ No newline at end of file diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index 13fa63b7b..9224fb403 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -6,61 +6,61 @@ Completed on January 2, 2024 """ import unittest -from binary_decimal_conversion import DecimalToBinary, BinaryToDecimal, binarydecimalconversion +from binary_decimal_conversion import decimal_to_binary, binary_decimal_conversion, binary_to_decimal class TestBinaryDecimalConversion(unittest.TestCase): def test_decimal_to_binary(self): """Should convert positive decimal numbers to binary""" - self.assertEqual(DecimalToBinary(10), '1010') + self.assertEqual(decimal_to_binary(10), '1010') def test_binary_to_decimal(self): """Should convert positive binary to decimal""" - self.assertEqual(BinaryToDecimal(10011101), 157) + self.assertEqual(binary_to_decimal(10011101), 157) def test_zero_decimal_to_binary(self): """should return 0""" - self.assertEqual(DecimalToBinary(0), '0') + self.assertEqual(decimal_to_binary(0), '0') def test_zero_binary_to_decimal(self): """should return 0""" - self.assertEqual(BinaryToDecimal(0), '0') + self.assertEqual(binary_to_decimal(0), '0') def test_large_decimal_to_binary(self): """Should correctly handle large decimal numbers""" - self.assertEqual(DecimalToBinary(1000000), '11110100001001000000') + self.assertEqual(decimal_to_binary(1000000), '11110100001001000000') def test_large_binary_to_decimal(self): """Should correctly handle large binary numbers""" - self.assertEqual(BinaryToDecimal('11110100001001000000'), 1000000) + self.assertEqual(binary_to_decimal('11110100001001000000'), 1000000) def test_binarydecimalconversion_decimal(self): """Should convert binary to decimal when conversion type is decimal""" - self.assertEqual(binarydecimalconversion('decimal', '10011101'), 157) + self.assertEqual(binary_decimal_conversion('decimal', '10011101'), 157) def test_binarydecimalconversion_binary(self): """Should convert decimal to binary when conversion type is binary""" - self.assertEqual(binarydecimalconversion('binary', '392'), 110001000) + self.assertEqual(binary_decimal_conversion('binary', '392'), 110001000) def test_invalid_decimal_input(self): """Should raise ValueError for non-integer decimal input""" with self.assertRaises(ValueError): - DecimalToBinary("notAnumber") + decimal_to_binary("notAnumber") def test_invalid_binary_input(self): """Should raise ValueError for non-binary input""" with self.assertRaises(ValueError): - BinaryToDecimal("1234") + binary_to_decimal("1234") def test_invalid_decimal_conversion_input(self): """Should raise ValueError if a non-binary string is provided to convert to decimal""" with self.assertRaises(ValueError): - binarydecimalconversion("decimal", "1020") + binary_decimal_conversion("decimal", "1020") def test_invalid_binary_conversion_input(self): """Should raise ValueError if a non-integer was provided to convert to binary""" with self.assertRaises(ValueError): - binarydecimalconversion('binary', 'word') + binary_decimal_conversion('binary', 'word') if __name__ == '__main__': unittest.main() \ No newline at end of file From d8950abafa52e7fa810a9b2a93ab5fd76771133a Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sat, 11 Jan 2025 20:51:14 +0200 Subject: [PATCH 12/19] formatting --- solutions/binary_decimal_conversion.py | 78 +++++++++++++++----------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index 59d5afa33..6e180d9a2 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -1,87 +1,99 @@ """ -A module for converting between binary and decimal numbers. +A module for converting between binary and decimal numbers. Created by Muna Sattouf on 29/12/2024 Completed on 1/1/2025 """ -def decimal_to_binary(decimal: int)-> str: + + +def decimal_to_binary(decimal: int) -> str: """ This function takes a decimal number from command line as paramter, and returns its binary representation. - - Example: decimal_to_binary('binary', 392) + + Example: DecimalToBinary('binary', 392) >>> 110001000 - + """ if not isinstance(decimal, int): - - raise ValueError("Input must be an integer") - + raise ValueError + """ + Input must be an integer + """ if decimal < 0: decimal = -decimal - return '-'+ decimal_to_binary(decimal) + return "-" + decimal_to_binary(decimal) if decimal == 0: - return '0' - binarynumber = ' ' + return "0" + binarynumber = " " while decimal > 0: - binarynumber = str(decimal % 2)+ binarynumber + binarynumber = str(decimal % 2) + binarynumber decimal = decimal // 2 return binarynumber -def binary_to_decimal(binary: str)-> int: + +def binary_to_decimal(binary: str) -> int: """ This function takes a binary number from command line as paramter, and returns its decimal representation. - + Example: BinaryToDecimal('decimal', 10011101) >>> 157 """ - if not all(c in '01' for c in binary): + if not all(c in "01" for c in binary): raise ValueError("Input must only contain 0s and 1s") decimalnumber = 0 for c in binary: - decimalnumber = decimalnumber*2 + int(c) + decimalnumber = decimalnumber * 2 + int(c) return decimalnumber -def binary_decimal_conversion(conversion_type: str, number:str)-> str: + +def binary_decimal_conversion(conversion_type: str, number: str) -> str: """ This is the main function: It 'coordinates' and decides which of the two above functions to use """ if conversion_type == "decimal": - if not all(c in '01' for c in number): - raise ValueError("If decimal conversion, input must be in binary, consisting only of 0 and 1") + if not all(c in "01" for c in number): + raise ValueError( + "If decimal conversion, input must be in binary, consisting only of 0 and 1" + ) return binary_to_decimal(number) elif conversion_type == "binary": if not number.isdigit(): raise ValueError("If binary conversion, input must be a positive integer") decimal = int(number) return decimal_to_binary(decimal) - else: + else: raise ValueError("Conversion Type must be 'binary' or 'decimal ' ") - -if __name__== "__main__": + +if __name__ == "__main__": import sys - if len(sys.argv)!=3: - print("To use properly, the first argument is conversion type. the second is the number you want to convert.") + + if len(sys.argv) != 3: + print( + "To use properly, the first argument is conversion type. the second is the number you want to convert." + ) sys.exit(1) - + conversion_type = sys.argv[1].lower() number = sys.argv[2] - + if conversion_type not in ["binary", "decimal"]: print("Error: Conversion type must be binary or decimal") sys.exit(1) - - if conversion_type == 'decimal': - if not all(c in '01' for c in number): - print("Error: for decimal conversion, input must be in binary, containing only 1 and 0") + + if conversion_type == "decimal": + if not all(c in "01" for c in number): + print( + "Error: for decimal conversion, input must be in binary, containing only 1 and 0" + ) sys.exit(1) result = binary_to_decimal(number) - - elif conversion_type == 'binary': + + elif conversion_type == "binary": if not number.isdigit(): print("Error: for binary conversion, input must be positive integer") sys.exit(1) - result = decimal_to_binary(int(number)) + result = decimal_to_binary(int(number)) print(result) \ No newline at end of file From acc430eae77efc1e4de4f5e2476a976622a0df32 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 12 Jan 2025 17:00:53 +0200 Subject: [PATCH 13/19] updates --- solutions/binary_decimal_conversion.py | 3 ++- solutions/tests/test_binary_decimal_conversion.py | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index 6e180d9a2..dc02b9f54 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -12,8 +12,9 @@ def decimal_to_binary(decimal: int) -> str: Example: DecimalToBinary('binary', 392) >>> 110001000 - + """ + if not isinstance(decimal, int): raise ValueError """ diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index 9224fb403..38f82732c 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -1,7 +1,6 @@ """ Module: test_binary_decimal_conversion contains test cases for the solution code. - Created by Muna Sattouf on January 2, 2024 Completed on January 2, 2024 """ @@ -16,7 +15,7 @@ def test_decimal_to_binary(self): def test_binary_to_decimal(self): """Should convert positive binary to decimal""" - self.assertEqual(binary_to_decimal(10011101), 157) + self.assertEqual(binary_to_decimal('10011101'), 157) def test_zero_decimal_to_binary(self): """should return 0""" @@ -40,7 +39,7 @@ def test_binarydecimalconversion_decimal(self): def test_binarydecimalconversion_binary(self): """Should convert decimal to binary when conversion type is binary""" - self.assertEqual(binary_decimal_conversion('binary', '392'), 110001000) + self.assertEqual(binary_decimal_conversion('binary', '392'), '110001000') def test_invalid_decimal_input(self): """Should raise ValueError for non-integer decimal input""" From 8c562e15237d478390d3187fad351fc794a90e83 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 12 Jan 2025 17:01:44 +0200 Subject: [PATCH 14/19] formatting --- solutions/binary_decimal_conversion.py | 6 +- .../tests/test_binary_decimal_conversion.py | 56 ++++++++++--------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index dc02b9f54..18d0144cf 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -12,9 +12,9 @@ def decimal_to_binary(decimal: int) -> str: Example: DecimalToBinary('binary', 392) >>> 110001000 - + """ - + if not isinstance(decimal, int): raise ValueError """ @@ -97,4 +97,4 @@ def binary_decimal_conversion(conversion_type: str, number: str) -> str: print("Error: for binary conversion, input must be positive integer") sys.exit(1) result = decimal_to_binary(int(number)) - print(result) \ No newline at end of file + print(result) diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index 38f82732c..2bd8da5c8 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -4,62 +4,68 @@ Created by Muna Sattouf on January 2, 2024 Completed on January 2, 2024 """ + import unittest -from binary_decimal_conversion import decimal_to_binary, binary_decimal_conversion, binary_to_decimal +from binary_decimal_conversion import ( + decimal_to_binary, + binary_decimal_conversion, + binary_to_decimal, +) + class TestBinaryDecimalConversion(unittest.TestCase): - def test_decimal_to_binary(self): """Should convert positive decimal numbers to binary""" - self.assertEqual(decimal_to_binary(10), '1010') - + self.assertEqual(decimal_to_binary(10), "1010") + def test_binary_to_decimal(self): """Should convert positive binary to decimal""" - self.assertEqual(binary_to_decimal('10011101'), 157) - + self.assertEqual(binary_to_decimal("10011101"), 157) + def test_zero_decimal_to_binary(self): """should return 0""" - self.assertEqual(decimal_to_binary(0), '0') - + self.assertEqual(decimal_to_binary(0), "0") + def test_zero_binary_to_decimal(self): """should return 0""" - self.assertEqual(binary_to_decimal(0), '0') - + self.assertEqual(binary_to_decimal(0), "0") + def test_large_decimal_to_binary(self): """Should correctly handle large decimal numbers""" - self.assertEqual(decimal_to_binary(1000000), '11110100001001000000') - + self.assertEqual(decimal_to_binary(1000000), "11110100001001000000") + def test_large_binary_to_decimal(self): """Should correctly handle large binary numbers""" - self.assertEqual(binary_to_decimal('11110100001001000000'), 1000000) - + self.assertEqual(binary_to_decimal("11110100001001000000"), 1000000) + def test_binarydecimalconversion_decimal(self): """Should convert binary to decimal when conversion type is decimal""" - self.assertEqual(binary_decimal_conversion('decimal', '10011101'), 157) - + self.assertEqual(binary_decimal_conversion("decimal", "10011101"), 157) + def test_binarydecimalconversion_binary(self): """Should convert decimal to binary when conversion type is binary""" - self.assertEqual(binary_decimal_conversion('binary', '392'), '110001000') - + self.assertEqual(binary_decimal_conversion("binary", "392"), "110001000") + def test_invalid_decimal_input(self): """Should raise ValueError for non-integer decimal input""" with self.assertRaises(ValueError): decimal_to_binary("notAnumber") - + def test_invalid_binary_input(self): """Should raise ValueError for non-binary input""" with self.assertRaises(ValueError): binary_to_decimal("1234") - + def test_invalid_decimal_conversion_input(self): """Should raise ValueError if a non-binary string is provided to convert to decimal""" with self.assertRaises(ValueError): binary_decimal_conversion("decimal", "1020") - + def test_invalid_binary_conversion_input(self): """Should raise ValueError if a non-integer was provided to convert to binary""" with self.assertRaises(ValueError): - binary_decimal_conversion('binary', 'word') - -if __name__ == '__main__': - unittest.main() \ No newline at end of file + binary_decimal_conversion("binary", "word") + + +if __name__ == "__main__": + unittest.main() From d0b8090139ee61a23c436e33490e34f1c5985dfa Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 12 Jan 2025 17:05:25 +0200 Subject: [PATCH 15/19] ruff formatting --- solutions/binary_decimal_conversion.py | 2 ++ solutions/tests/test_binary_decimal_conversion.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index 18d0144cf..d15617758 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- """ A module for converting between binary and decimal numbers. Created by Muna Sattouf on 29/12/2024 diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index 2bd8da5c8..7708919cd 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -1,3 +1,7 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + """ Module: test_binary_decimal_conversion contains test cases for the solution code. From 4142b1729bcd81e557cf8980646e7175a4da116d Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 12 Jan 2025 17:12:44 +0200 Subject: [PATCH 16/19] formatting + comments --- solutions/binary_decimal_conversion.py | 5 ++++- solutions/tests/test_binary_decimal_conversion.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index d15617758..4a80ebcf4 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -39,8 +39,11 @@ def binary_to_decimal(binary: str) -> int: This function takes a binary number from command line as paramter, and returns its decimal representation. - Example: BinaryToDecimal('decimal', 10011101) + Example: binary_to_decimal('decimal', 10011101) >>> 157 + + Example: binary_to_decimal('decimal', 'hello') + >>> ValueError: Input must only contain 0s and 1s """ if not all(c in "01" for c in binary): raise ValueError("Input must only contain 0s and 1s") diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index 7708919cd..9a730e226 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -10,7 +10,7 @@ """ import unittest -from binary_decimal_conversion import ( +from solutions.binary_decimal_conversion import ( decimal_to_binary, binary_decimal_conversion, binary_to_decimal, From 9e99ce741cf3dbea7ff5c70aca942e7e13f9a522 Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 12 Jan 2025 17:15:43 +0200 Subject: [PATCH 17/19] fixed trailing space --- solutions/binary_decimal_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/binary_decimal_conversion.py b/solutions/binary_decimal_conversion.py index 4a80ebcf4..9c5816a13 100644 --- a/solutions/binary_decimal_conversion.py +++ b/solutions/binary_decimal_conversion.py @@ -27,7 +27,7 @@ def decimal_to_binary(decimal: int) -> str: return "-" + decimal_to_binary(decimal) if decimal == 0: return "0" - binarynumber = " " + binarynumber = "" while decimal > 0: binarynumber = str(decimal % 2) + binarynumber decimal = decimal // 2 From 1bdeb78383aaff018aee51cfe95ac78600665eed Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 12 Jan 2025 17:19:12 +0200 Subject: [PATCH 18/19] fixed test error --- solutions/tests/test_binary_decimal_conversion.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index 9a730e226..9f0de303d 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -18,6 +18,8 @@ class TestBinaryDecimalConversion(unittest.TestCase): + """Tests for binaru_decimal_conversion function""" + def test_decimal_to_binary(self): """Should convert positive decimal numbers to binary""" self.assertEqual(decimal_to_binary(10), "1010") @@ -32,7 +34,7 @@ def test_zero_decimal_to_binary(self): def test_zero_binary_to_decimal(self): """should return 0""" - self.assertEqual(binary_to_decimal(0), "0") + self.assertEqual(binary_to_decimal("0"), "0") def test_large_decimal_to_binary(self): """Should correctly handle large decimal numbers""" From c81a87a4e0108853c32f080ab40905cbf5837c8b Mon Sep 17 00:00:00 2001 From: Muna Sattouf Date: Sun, 12 Jan 2025 17:21:15 +0200 Subject: [PATCH 19/19] fixed test issue --- solutions/tests/test_binary_decimal_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_binary_decimal_conversion.py b/solutions/tests/test_binary_decimal_conversion.py index 9f0de303d..5cb8096f4 100644 --- a/solutions/tests/test_binary_decimal_conversion.py +++ b/solutions/tests/test_binary_decimal_conversion.py @@ -34,7 +34,7 @@ def test_zero_decimal_to_binary(self): def test_zero_binary_to_decimal(self): """should return 0""" - self.assertEqual(binary_to_decimal("0"), "0") + self.assertEqual(binary_to_decimal("0"), 0) def test_large_decimal_to_binary(self): """Should correctly handle large decimal numbers"""