easy +5 pts

Digit count of an integer

Count how many digits an integer has without converting to a string.

Write a function `digit_count(n)` that takes an integer `n` and returns the number of digits in its decimal representation. For negative numbers, the minus sign is not counted. The number 0 has exactly one digit.

Constraints

n is an integer between -(10^9) and 10^9 inclusive.

Example

>>> digit_count(12345)
5
>>> digit_count(-7)
1
>>> digit_count(0)
1
>>> digit_count(10)
2
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Work with the absolute value of n.
Use a loop that repeatedly divides by 10 and counts iterations.
Remember that 0 has one digit, so handle that case first.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.