import math
def digitLengthCorrect(n):
return len(str(n))
def digitLengthClever2(n):
return 1 if n == 0 else (math.floor(math.log10(n)) + 1)
testcases = (
[10 ** i for i in range(300)] +
[(10 ** i) - 1 for i in range(300)]
)
for t in testcases:
a = digitLengthCorrect(t)
b = digitLengthClever2(t)
assert a == b, (t, a, b)EDIT: Setting Decimal module's precision manually via getcontext() may be required if you work with "long" numbers.
import math
import numpy as np
from decimal import getcontext, Decimal
def digit_length_correct(n):
return len(str(n))
def digit_length_clever_2(n):
if n ==0:
return 1
else:
return math.floor((np.log10(Decimal(n)))) + 1 # You can use np.floor() and eschew the use of math. module altogether, but I left it intact to show the minimal necessary modifications
def generate_cases(n):
getcontext().prec = n + 3
return (
[10 ** i for i in range(n)] +
[(10 ** i) - 1 for i in range(n)]
)
cases = generate_cases(300)
for t in cases:
a = digit_length_correct(t)
b = digit_length_clever_2(t)
assert a == b, (t, a, b)
for t in cases[-2:]:
print(t)
print(digit_length_correct(t))
print(digit_length_clever_2(t))