From 7e9d13f08b1bab63c3cf910027fc7e76f6ba427f Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Mon, 30 Oct 2023 17:18:13 +0100 Subject: [PATCH 1/6] feat: binary_converter exercise --- src/binary_converter.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/binary_converter.py diff --git a/src/binary_converter.py b/src/binary_converter.py new file mode 100644 index 0000000..1bd3d7e --- /dev/null +++ b/src/binary_converter.py @@ -0,0 +1,8 @@ +# Write a program that given a number as input convert it in binary. + +# Output: +# Insert first number: 8 +# The binary number is: 1000 + +x = int(input("Insert first number: ")) +print("The binary number is: " + bin(x)[2:]) \ No newline at end of file From 399b0d879d752d0984ddfc437f9d40cebcb44b2b Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Mon, 30 Oct 2023 17:20:37 +0100 Subject: [PATCH 2/6] refactor: change variable name --- src/binary_converter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/binary_converter.py b/src/binary_converter.py index 1bd3d7e..a501601 100644 --- a/src/binary_converter.py +++ b/src/binary_converter.py @@ -4,5 +4,5 @@ # Insert first number: 8 # The binary number is: 1000 -x = int(input("Insert first number: ")) -print("The binary number is: " + bin(x)[2:]) \ No newline at end of file +decimal_number = int(input("Insert first number: ")) +print("The binary number is: " + bin(decimal_number)[2:]) \ No newline at end of file From 098abc27a7596af7897d36f5241a9e0579d0f030 Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Mon, 30 Oct 2023 17:40:39 +0100 Subject: [PATCH 3/6] refactor: add new line at the eof --- src/binary_converter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/binary_converter.py b/src/binary_converter.py index a501601..7618ae8 100644 --- a/src/binary_converter.py +++ b/src/binary_converter.py @@ -5,4 +5,4 @@ # The binary number is: 1000 decimal_number = int(input("Insert first number: ")) -print("The binary number is: " + bin(decimal_number)[2:]) \ No newline at end of file +print("The binary number is: " + bin(decimal_number)[2:]) From f7f69e7d75867dc8900c28b8e00a92665a1b65dd Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Tue, 31 Oct 2023 16:43:49 +0100 Subject: [PATCH 4/6] refactor: add comment to explain decimal_to_binary function --- src/binary_converter.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/binary_converter.py b/src/binary_converter.py index 7618ae8..01c7d63 100644 --- a/src/binary_converter.py +++ b/src/binary_converter.py @@ -4,5 +4,9 @@ # Insert first number: 8 # The binary number is: 1000 +#The lambda function decimal_to_binary takes a decimal number and returns a binary number in which we remove the first 2 characters that identifies a binary digit (0b). +decimal_to_binary = lambda decimal_number: bin(decimal_number)[2:] + decimal_number = int(input("Insert first number: ")) -print("The binary number is: " + bin(decimal_number)[2:]) + +print("The binary number is: " + decimal_to_binary(decimal_number)) From 10b8760df244666abe9e4b95431518cd755dbe41 Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Tue, 31 Oct 2023 18:01:43 +0100 Subject: [PATCH 5/6] refactor: remove lambda function and add a normal function for binary conversion --- src/binary_converter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/binary_converter.py b/src/binary_converter.py index 01c7d63..ed022a5 100644 --- a/src/binary_converter.py +++ b/src/binary_converter.py @@ -4,8 +4,9 @@ # Insert first number: 8 # The binary number is: 1000 -#The lambda function decimal_to_binary takes a decimal number and returns a binary number in which we remove the first 2 characters that identifies a binary digit (0b). -decimal_to_binary = lambda decimal_number: bin(decimal_number)[2:] +#The function decimal_to_binary takes a decimal number and returns a binary number in which we remove the first 2 characters that identifies a binary digit (0b). +def decimal_to_binary(decimal_number): + return bin(decimal_number)[2:] decimal_number = int(input("Insert first number: ")) From 0ca0650090277b52a81c0738336fb4e24a0214ea Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Tue, 31 Oct 2023 18:03:00 +0100 Subject: [PATCH 6/6] refactor: remove lambda function and add a normal function for binary conversion --- src/binary_converter.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/binary_converter.py b/src/binary_converter.py index ed022a5..533f5a8 100644 --- a/src/binary_converter.py +++ b/src/binary_converter.py @@ -4,10 +4,12 @@ # Insert first number: 8 # The binary number is: 1000 -#The function decimal_to_binary takes a decimal number and returns a binary number in which we remove the first 2 characters that identifies a binary digit (0b). + +# The function decimal_to_binary takes a decimal number and returns a binary number in which we remove the first 2 characters that identifies a binary digit (0b). def decimal_to_binary(decimal_number): return bin(decimal_number)[2:] -decimal_number = int(input("Insert first number: ")) -print("The binary number is: " + decimal_to_binary(decimal_number)) +input_number = int(input("Insert first number: ")) + +print("The binary number is: " + decimal_to_binary(input_number))