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)So, the discussion of this problem can be extended further into a discussion of specs, tradeoffs, benchmarking, etc.