easy +8 pts

Count Leading Zeros

Count how many zeros appear before the first non-zero digit in a list of numbers.

Write a function `count_leading_zeros(numbers)` that takes a list of integers and returns the number of zeros (0) at the beginning of the list, stopping at the first non-zero element. If the list contains only zeros or is empty, return the length of the list.

Constraints

Input list length is between 0 and 10^5. Elements are integers. The function must run in O(n) time.

Example

>>> count_leading_zeros([0, 0, 5, 0, 3])
2
>>> count_leading_zeros([1, 0, 0])
0
>>> count_leading_zeros([0, 0, 0])
3
>>> count_leading_zeros([])
0
8 points ~8 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate through the list from the start and count zeros until you hit a non-zero.
If you reach the end of the list without finding a non-zero, all elements are zeros, so return the length.
Use a loop or a generator expression with a break.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.