use format to remove '0b' (#11307)

* use format to remove '0b'

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: error message for float input

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Vishal Kumar Gupta 2024-06-02 02:41:40 +01:00 committed by GitHub
parent 723cf9c428
commit edee8e644b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,7 +26,7 @@ def binary_and(a: int, b: int) -> str:
>>> binary_and(0, 1.1) >>> binary_and(0, 1.1)
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: 'float' object cannot be interpreted as an integer ValueError: Unknown format code 'b' for object of type 'float'
>>> binary_and("0", "1") >>> binary_and("0", "1")
Traceback (most recent call last): Traceback (most recent call last):
... ...
@ -35,8 +35,8 @@ def binary_and(a: int, b: int) -> str:
if a < 0 or b < 0: if a < 0 or b < 0:
raise ValueError("the value of both inputs must be positive") raise ValueError("the value of both inputs must be positive")
a_binary = str(bin(a))[2:] # remove the leading "0b" a_binary = format(a, "b")
b_binary = str(bin(b))[2:] # remove the leading "0b" b_binary = format(b, "b")
max_len = max(len(a_binary), len(b_binary)) max_len = max(len(a_binary), len(b_binary))