From 88249fae8333b2956a716ee7ca5954bb1d54beb8 Mon Sep 17 00:00:00 2001 From: manmita Date: Thu, 26 Oct 2023 19:42:35 +0530 Subject: [PATCH 1/6] added excess-3 code --- bit_manipulation/excess_3_code.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 bit_manipulation/excess_3_code.py diff --git a/bit_manipulation/excess_3_code.py b/bit_manipulation/excess_3_code.py new file mode 100644 index 000000000000..824f9f03d81a --- /dev/null +++ b/bit_manipulation/excess_3_code.py @@ -0,0 +1,27 @@ +def excess_3_code(number:int)->str: + """ + Find excess 3 code of integer base 10. + We add 3 to all digits in the decimal number then convert to binary coded decimal number. + Example: + >>> excess_3_code(0) + '0b0011' + >>> excess_3_code(3) + '0b0110' + >>> excess_3_code(2) + '0b0101' + >>> excess_3_code(20) + '0b01010011' + >>> excess_3_code(120) + '0b010001010011' + """ + num = "" + for digit in str(max(0, number)): + num += str(bin(int(digit)+ 3))[2:].zfill(4) + return "0b" + num + + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 049d9b2d431a2e83f0abf0c40fc58880ae9a2b8f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Oct 2023 15:02:05 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/excess_3_code.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/excess_3_code.py b/bit_manipulation/excess_3_code.py index 824f9f03d81a..5ead8811c083 100644 --- a/bit_manipulation/excess_3_code.py +++ b/bit_manipulation/excess_3_code.py @@ -1,4 +1,4 @@ -def excess_3_code(number:int)->str: +def excess_3_code(number: int) -> str: """ Find excess 3 code of integer base 10. We add 3 to all digits in the decimal number then convert to binary coded decimal number. @@ -16,11 +16,10 @@ def excess_3_code(number:int)->str: """ num = "" for digit in str(max(0, number)): - num += str(bin(int(digit)+ 3))[2:].zfill(4) + num += str(bin(int(digit) + 3))[2:].zfill(4) return "0b" + num - if __name__ == "__main__": import doctest From 43cebe09e1877a2924e18220138b1d4136436cca Mon Sep 17 00:00:00 2001 From: manmita Date: Thu, 26 Oct 2023 20:36:29 +0530 Subject: [PATCH 3/6] updated with fixes --- bit_manipulation/excess_3_code.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bit_manipulation/excess_3_code.py b/bit_manipulation/excess_3_code.py index 824f9f03d81a..bb488b62d533 100644 --- a/bit_manipulation/excess_3_code.py +++ b/bit_manipulation/excess_3_code.py @@ -1,4 +1,4 @@ -def excess_3_code(number:int)->str: +def excess_3_code(number: int) -> str: """ Find excess 3 code of integer base 10. We add 3 to all digits in the decimal number then convert to binary coded decimal number. @@ -20,7 +20,6 @@ def excess_3_code(number:int)->str: return "0b" + num - if __name__ == "__main__": import doctest From 3af377c32b6a149cb4b3185e55b0d249b464bf44 Mon Sep 17 00:00:00 2001 From: manmita Date: Thu, 26 Oct 2023 21:19:56 +0530 Subject: [PATCH 4/6] updated with fixes --- bit_manipulation/excess_3_code.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/excess_3_code.py b/bit_manipulation/excess_3_code.py index 5ead8811c083..0f7467dd2a8a 100644 --- a/bit_manipulation/excess_3_code.py +++ b/bit_manipulation/excess_3_code.py @@ -1,7 +1,8 @@ def excess_3_code(number: int) -> str: """ Find excess 3 code of integer base 10. - We add 3 to all digits in the decimal number then convert to binary coded decimal number. + We add 3 to all digits in the decimal number + then convert to binary coded decimal number. Example: >>> excess_3_code(0) '0b0011' From e1ac7e6d1bfde9847abcf4efe816699602f8fb28 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Oct 2023 15:51:49 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/excess_3_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/excess_3_code.py b/bit_manipulation/excess_3_code.py index 0f7467dd2a8a..927bbb1c0f8d 100644 --- a/bit_manipulation/excess_3_code.py +++ b/bit_manipulation/excess_3_code.py @@ -1,7 +1,7 @@ def excess_3_code(number: int) -> str: """ Find excess 3 code of integer base 10. - We add 3 to all digits in the decimal number + We add 3 to all digits in the decimal number then convert to binary coded decimal number. Example: >>> excess_3_code(0) From 60c0cac07e1839fe88e789c81131713552d0751f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 27 Oct 2023 23:30:24 +0200 Subject: [PATCH 6/6] Update excess_3_code.py --- bit_manipulation/excess_3_code.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bit_manipulation/excess_3_code.py b/bit_manipulation/excess_3_code.py index 927bbb1c0f8d..7beaabd90e8a 100644 --- a/bit_manipulation/excess_3_code.py +++ b/bit_manipulation/excess_3_code.py @@ -1,9 +1,9 @@ def excess_3_code(number: int) -> str: """ - Find excess 3 code of integer base 10. - We add 3 to all digits in the decimal number - then convert to binary coded decimal number. - Example: + Find excess-3 code of integer base 10. + Add 3 to all digits in a decimal number then convert to a binary-coded decimal. + https://en.wikipedia.org/wiki/Excess-3 + >>> excess_3_code(0) '0b0011' >>> excess_3_code(3)