Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Math way, no strings used solution in Clear category for Number Length by marcopunteri
from math import log10, ceil
def number_length(a: int) -> int:
if a%10 == 0:
return 1 if a == 0 else log10(a) + 1
else:
return ceil(log10(a))
if __name__ == '__main__':
print("Example:")
print(number_length(10))
# These "asserts" are used for self-checking and not for an auto-testing
assert number_length(10) == 2
assert number_length(0) == 1
assert number_length(4) == 1
assert number_length(44) == 2
print("Coding complete? Click 'Check' to earn cool rewards!")
April 10, 2021
Comments: