From 6c0a05498e7f7fd4862b6f301f1e5f812ae0b863 Mon Sep 17 00:00:00 2001 From: manmita Date: Wed, 18 Oct 2023 01:42:58 +0530 Subject: [PATCH 1/8] added decimal to bcd sequence --- bit_manipulation/bcd_sequence.py | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 bit_manipulation/bcd_sequence.py diff --git a/bit_manipulation/bcd_sequence.py b/bit_manipulation/bcd_sequence.py new file mode 100644 index 000000000000..2e7778d8b7fc --- /dev/null +++ b/bit_manipulation/bcd_sequence.py @@ -0,0 +1,34 @@ +def bcd_sequence(num: int) ->str: + """ + Find binary coded decimal(bcd) of integer base 10. + Each digit of number is represented by 4 bits binary. + Example: + >>> bcd_sequence(0) + '0b0000' + >>> bcd_sequence(3) + '0b0011' + >>> bcd_sequence(2) + '0b0010' + >>> bcd_sequence(12) + '0b00010010' + >>> bcd_sequence(987) + '0b100110000111' + """ + bcd = "" + while num>0 : + digit = num%10 + num = int(num/10) + bin_seq = str(bin(digit))[2:] + bin_seq = "0"*max(0,4-len(bin_seq)) + bin_seq + bcd = bin_seq + bcd + if bcd == "": + bcd = "0"*4 + bcd = "0b" + bcd + return bcd + + + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 5c3b9c6575e409f8f869e8179b2a3486c9f355a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Oct 2023 20:21:42 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/bcd_sequence.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/bit_manipulation/bcd_sequence.py b/bit_manipulation/bcd_sequence.py index 2e7778d8b7fc..d523ac6c9892 100644 --- a/bit_manipulation/bcd_sequence.py +++ b/bit_manipulation/bcd_sequence.py @@ -1,4 +1,4 @@ -def bcd_sequence(num: int) ->str: +def bcd_sequence(num: int) -> str: """ Find binary coded decimal(bcd) of integer base 10. Each digit of number is represented by 4 bits binary. @@ -15,20 +15,19 @@ def bcd_sequence(num: int) ->str: '0b100110000111' """ bcd = "" - while num>0 : - digit = num%10 - num = int(num/10) + while num > 0: + digit = num % 10 + num = int(num / 10) bin_seq = str(bin(digit))[2:] - bin_seq = "0"*max(0,4-len(bin_seq)) + bin_seq + bin_seq = "0" * max(0, 4 - len(bin_seq)) + bin_seq bcd = bin_seq + bcd if bcd == "": - bcd = "0"*4 + bcd = "0" * 4 bcd = "0b" + bcd return bcd - if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 83556cee6f10120c58ce587eb82a2a0ec66b35a0 Mon Sep 17 00:00:00 2001 From: manmita Date: Wed, 18 Oct 2023 01:53:38 +0530 Subject: [PATCH 3/8] updated with fixes --- bit_manipulation/bcd_sequence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/bcd_sequence.py b/bit_manipulation/bcd_sequence.py index 2e7778d8b7fc..7dec3274ab67 100644 --- a/bit_manipulation/bcd_sequence.py +++ b/bit_manipulation/bcd_sequence.py @@ -26,9 +26,9 @@ def bcd_sequence(num: int) ->str: bcd = "0b" + bcd return bcd - if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() + \ No newline at end of file From 7eabc8fed73c4cc9bcafb17fecbfd9ab9fc0506b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 18 Oct 2023 00:26:50 +0200 Subject: [PATCH 4/8] Update and rename bcd_sequence.py to binary_coded_decimal.py --- bit_manipulation/bcd_sequence.py | 33 ------------------------ bit_manipulation/binary_coded_decimal.py | 31 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 33 deletions(-) delete mode 100644 bit_manipulation/bcd_sequence.py create mode 100644 bit_manipulation/binary_coded_decimal.py diff --git a/bit_manipulation/bcd_sequence.py b/bit_manipulation/bcd_sequence.py deleted file mode 100644 index d523ac6c9892..000000000000 --- a/bit_manipulation/bcd_sequence.py +++ /dev/null @@ -1,33 +0,0 @@ -def bcd_sequence(num: int) -> str: - """ - Find binary coded decimal(bcd) of integer base 10. - Each digit of number is represented by 4 bits binary. - Example: - >>> bcd_sequence(0) - '0b0000' - >>> bcd_sequence(3) - '0b0011' - >>> bcd_sequence(2) - '0b0010' - >>> bcd_sequence(12) - '0b00010010' - >>> bcd_sequence(987) - '0b100110000111' - """ - bcd = "" - while num > 0: - digit = num % 10 - num = int(num / 10) - bin_seq = str(bin(digit))[2:] - bin_seq = "0" * max(0, 4 - len(bin_seq)) + bin_seq - bcd = bin_seq + bcd - if bcd == "": - bcd = "0" * 4 - bcd = "0b" + bcd - return bcd - - -if __name__ == "__main__": - import doctest - - doctest.testmod() diff --git a/bit_manipulation/binary_coded_decimal.py b/bit_manipulation/binary_coded_decimal.py new file mode 100644 index 000000000000..fa3a8533585c --- /dev/null +++ b/bit_manipulation/binary_coded_decimal.py @@ -0,0 +1,31 @@ +def binary_coded_decimal(number: int) -> str: + """ + Find binary coded decimal (bcd) of integer base 10. + Each digit of the number is represented by a 4-bit binary. + Example: + >>> binary_coded_decimal(-2) + '0b0000' + >>> binary_coded_decimal(-1) + '0b0000' + >>> binary_coded_decimal(0) + '0b0000' + >>> binary_coded_decimal(3) + '0b0011' + >>> binary_coded_decimal(2) + '0b0010' + >>> binary_coded_decimal(12) + '0b00010010' + >>> binary_coded_decimal(987) + '0b100110000111' + """ + if number < 0: + return "0b0000" + return "0b" + "".join( + str(bin(int(digit)))[2:].zfill(4) for digit in str(number) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From e4b5ff716e3f36348603d36ffa91efceba7ddeff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:29:26 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/binary_coded_decimal.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bit_manipulation/binary_coded_decimal.py b/bit_manipulation/binary_coded_decimal.py index fa3a8533585c..d28aa27763e5 100644 --- a/bit_manipulation/binary_coded_decimal.py +++ b/bit_manipulation/binary_coded_decimal.py @@ -20,9 +20,7 @@ def binary_coded_decimal(number: int) -> str: """ if number < 0: return "0b0000" - return "0b" + "".join( - str(bin(int(digit)))[2:].zfill(4) for digit in str(number) - ) + return "0b" + "".join(str(bin(int(digit)))[2:].zfill(4) for digit in str(number)) if __name__ == "__main__": From 6b80a830c77c895a02824b8822de2da89c02222f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 18 Oct 2023 00:34:01 +0200 Subject: [PATCH 6/8] Update binary_coded_decimal.py --- bit_manipulation/binary_coded_decimal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/binary_coded_decimal.py b/bit_manipulation/binary_coded_decimal.py index d28aa27763e5..c061a18e7244 100644 --- a/bit_manipulation/binary_coded_decimal.py +++ b/bit_manipulation/binary_coded_decimal.py @@ -20,7 +20,7 @@ def binary_coded_decimal(number: int) -> str: """ if number < 0: return "0b0000" - return "0b" + "".join(str(bin(int(digit)))[2:].zfill(4) for digit in str(number)) + return "0b" + "".join(str(bin(int(digit)))[2:].zfill(4) for digit in str(max(0, number))) if __name__ == "__main__": From 7b99ad13966635ca2cfd0d40ce1cf1f47a460929 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 18 Oct 2023 00:34:33 +0200 Subject: [PATCH 7/8] Update binary_coded_decimal.py --- bit_manipulation/binary_coded_decimal.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bit_manipulation/binary_coded_decimal.py b/bit_manipulation/binary_coded_decimal.py index c061a18e7244..a8e39a9113bf 100644 --- a/bit_manipulation/binary_coded_decimal.py +++ b/bit_manipulation/binary_coded_decimal.py @@ -18,8 +18,6 @@ def binary_coded_decimal(number: int) -> str: >>> binary_coded_decimal(987) '0b100110000111' """ - if number < 0: - return "0b0000" return "0b" + "".join(str(bin(int(digit)))[2:].zfill(4) for digit in str(max(0, number))) From a083f421f4706f7e24426fc12638189fa8179904 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:35:13 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/binary_coded_decimal.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/binary_coded_decimal.py b/bit_manipulation/binary_coded_decimal.py index a8e39a9113bf..676fd6d54fc5 100644 --- a/bit_manipulation/binary_coded_decimal.py +++ b/bit_manipulation/binary_coded_decimal.py @@ -18,7 +18,9 @@ def binary_coded_decimal(number: int) -> str: >>> binary_coded_decimal(987) '0b100110000111' """ - return "0b" + "".join(str(bin(int(digit)))[2:].zfill(4) for digit in str(max(0, number))) + return "0b" + "".join( + str(bin(int(digit)))[2:].zfill(4) for digit in str(max(0, number)) + ) if __name__ == "__main__":