From aa8379b9a2566f1aa0d717110149ca6a11153fbb Mon Sep 17 00:00:00 2001 From: mahiuddin-dev Date: Sun, 1 Oct 2023 15:29:28 +0600 Subject: [PATCH 1/3] type error check --- maths/number_of_digits.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/maths/number_of_digits.py b/maths/number_of_digits.py index 86bc67f72490..385169e88868 100644 --- a/maths/number_of_digits.py +++ b/maths/number_of_digits.py @@ -16,7 +16,15 @@ def num_digits(n: int) -> int: 1 >>> num_digits(-123456) 6 + >>> num_digits('123') # Raises a TypeError for non-integer input + Traceback (most recent call last): + ... + TypeError: Input must be an integer """ + + if not isinstance(n, int): + raise TypeError("Input must be an integer") + digits = 0 n = abs(n) while True: @@ -42,7 +50,15 @@ def num_digits_fast(n: int) -> int: 1 >>> num_digits_fast(-123456) 6 + >>> num_digits('123') # Raises a TypeError for non-integer input + Traceback (most recent call last): + ... + TypeError: Input must be an integer """ + + if not isinstance(n, int): + raise TypeError("Input must be an integer") + return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1) @@ -61,7 +77,15 @@ def num_digits_faster(n: int) -> int: 1 >>> num_digits_faster(-123456) 6 + >>> num_digits('123') # Raises a TypeError for non-integer input + Traceback (most recent call last): + ... + TypeError: Input must be an integer """ + + if not isinstance(n, int): + raise TypeError("Input must be an integer") + return len(str(abs(n))) @@ -76,7 +100,7 @@ def benchmark_a_function(func: Callable, value: int) -> None: timing = timeit(f"__main__.{call}", setup="import __main__") print(f"{call}: {func(value)} -- {timing} seconds") - for value in (262144, 1125899906842624, 1267650600228229401496703205376): + for value in ('262144', 1125899906842624, 1267650600228229401496703205376): for func in (num_digits, num_digits_fast, num_digits_faster): benchmark_a_function(func, value) print() From bba0c36f96f3e46645119c96e00ec01c0925cf0d Mon Sep 17 00:00:00 2001 From: mahiuddin-dev Date: Sun, 1 Oct 2023 15:33:19 +0600 Subject: [PATCH 2/3] remove str input --- maths/number_of_digits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/number_of_digits.py b/maths/number_of_digits.py index 385169e88868..206ea90ffeda 100644 --- a/maths/number_of_digits.py +++ b/maths/number_of_digits.py @@ -100,7 +100,7 @@ def benchmark_a_function(func: Callable, value: int) -> None: timing = timeit(f"__main__.{call}", setup="import __main__") print(f"{call}: {func(value)} -- {timing} seconds") - for value in ('262144', 1125899906842624, 1267650600228229401496703205376): + for value in (262144, 1125899906842624, 1267650600228229401496703205376): for func in (num_digits, num_digits_fast, num_digits_faster): benchmark_a_function(func, value) print() From 65be417b9a5e415da9a7608c9f95dba4348a2cba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 09:42:24 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/number_of_digits.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maths/number_of_digits.py b/maths/number_of_digits.py index 206ea90ffeda..bb9c0d248fd1 100644 --- a/maths/number_of_digits.py +++ b/maths/number_of_digits.py @@ -21,7 +21,7 @@ def num_digits(n: int) -> int: ... TypeError: Input must be an integer """ - + if not isinstance(n, int): raise TypeError("Input must be an integer") @@ -55,10 +55,10 @@ def num_digits_fast(n: int) -> int: ... TypeError: Input must be an integer """ - + if not isinstance(n, int): raise TypeError("Input must be an integer") - + return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1) @@ -82,10 +82,10 @@ def num_digits_faster(n: int) -> int: ... TypeError: Input must be an integer """ - + if not isinstance(n, int): raise TypeError("Input must be an integer") - + return len(str(abs(n)))