Skip to content

Commit 28f0f61

Browse files
Fix incorrect doctest and improve log handling in num_digits functions
1 parent e2a78d4 commit 28f0f61

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

maths/number_of_digits.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def num_digits(n: int) -> int:
2121
...
2222
TypeError: Input must be an integer
2323
"""
24-
2524
if not isinstance(n, int):
2625
raise TypeError("Input must be an integer")
2726

@@ -50,22 +49,21 @@ def num_digits_fast(n: int) -> int:
5049
1
5150
>>> num_digits_fast(-123456)
5251
6
53-
>>> num_digits('123') # Raises a TypeError for non-integer input
52+
>>> num_digits_fast('123') # Raises a TypeError for non-integer input
5453
Traceback (most recent call last):
5554
...
5655
TypeError: Input must be an integer
5756
"""
58-
5957
if not isinstance(n, int):
6058
raise TypeError("Input must be an integer")
6159

62-
return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1)
60+
return 1 if n == 0 else math.floor(math.log10(abs(n)) + 1)
6361

6462

6563
def num_digits_faster(n: int) -> int:
6664
"""
6765
Find the number of digits in a number.
68-
abs() is used for negative numbers
66+
abs() is used for negative numbers.
6967
7068
>>> num_digits_faster(12345)
7169
5
@@ -77,12 +75,11 @@ def num_digits_faster(n: int) -> int:
7775
1
7876
>>> num_digits_faster(-123456)
7977
6
80-
>>> num_digits('123') # Raises a TypeError for non-integer input
78+
>>> num_digits_faster('123') # Raises a TypeError for non-integer input
8179
Traceback (most recent call last):
8280
...
8381
TypeError: Input must be an integer
8482
"""
85-
8683
if not isinstance(n, int):
8784
raise TypeError("Input must be an integer")
8885

@@ -91,14 +88,14 @@ def num_digits_faster(n: int) -> int:
9188

9289
def benchmark() -> None:
9390
"""
94-
Benchmark multiple functions, with three different length int values.
91+
Benchmark multiple functions with three different integer sizes.
9592
"""
9693
from collections.abc import Callable
9794

9895
def benchmark_a_function(func: Callable, value: int) -> None:
9996
call = f"{func.__name__}({value})"
10097
timing = timeit(f"__main__.{call}", setup="import __main__")
101-
print(f"{call}: {func(value)} -- {timing} seconds")
98+
print(f"{call}: {func(value)} -- {timing:.6f} seconds")
10299

103100
for value in (262144, 1125899906842624, 1267650600228229401496703205376):
104101
for func in (num_digits, num_digits_fast, num_digits_faster):

0 commit comments

Comments
 (0)