perl -e 'print length 987654'
6
Similarly easy in Lisp with either of write-to-string, prin1-to-string, and princ-to-string. I expect python to have some such built-in function too.I consider that to be wrong - leading zeros are not part of the number and should be ignored. Using `len(str(x))` results in `2` for the input `"01"`.
def numDigits(num):
return len(str(num))
print (numDigits(1)) # Returns the correct answer
print (numDigits("1")) # Returns the correct answer
print (numDigits("01")) # Returns the incorrect answer
The "returns the wrong answer" is the problem. If the input is the wrong type, I expect there to be an error raised, not silently give me wrong answers.