Find out how many zeros a number has at the end (string index out of range error when number is 0)

Viewed 2445

I am trying to solve the problem in the title, and I'm getting an IndexError.

def count_zeros(number:int) -> int:
    number = str(number)
    i = 0
    j = -1

    while number[j] == '0'
        i += 1
        j += -1

    return i

This code spits out the IndexError only when the number is 0. I cannot figure out why.

>>>count_zeros(0)


IndexError  Traceback (most recent call last)
<ipython-input-45-688099e7700c> in <module>
----> 1 end_zeros(0)

<ipython-input-43-181de92af60c> in end_zeros(number)
      4     i = 0
      5 
----> 6     while number[j] == '0':
      7         i += 1
      8         j -= 1

IndexError: string index out of range
5 Answers

I would use the modulus here and count the number of zeroes on the right:

def count_zeros(number:int) -> int:
    if number == 0:
        return 1
    else:
        count = 0
        while True:
            if number % 10 != 0 or number == 0:
                 break
            count = count + 1 
            number = number / 10

        return count

This approach is straightforward, and will almost certainly outperform casting the input integer to a string and then checking for trailing zeroes.

As an alternative approach to iteration, using regular expressions proves to be a simple approach.

Since your code converts the number to a string already, it doesn't hurt to pass the string directly to re.search to find the trailing zeros for you effortlessly.

Here is a working example, along with some test cases:

import re

def count_zeros(number: int) -> int:
    count = re.search(r'0+$', str(number))
    return len(count[0]) if count else 0

print(count_zeros(1))
print(count_zeros(123))
print(count_zeros(0))
print(count_zeros(10))
print(count_zeros(12300))
print(count_zeros(123000))

Output:

0
0
1
1
2
3

How many zeros does a number have at the end

def count_zeros(num: int) -> int:
    return len(str(num)) - len(str(num).rstrip('0'))

    

print(count_zeros(0))       #1
print(count_zeros(1))       #0
print(count_zeros(10))      #1
print(count_zeros(245))     #0
print(count_zeros(101))     #0
print(count_zeros(100100))  #2

For this problem, honestly, the simplest solution is likely going to be to handle the case where number==0 separately, by putting something like this at the top.

    if number ==0:
        return 1

Your loop with an iterator and a count works, but I would prefer to cast to a list so I don't have to keep track of two variables...assuming performance is not a huge concern here.

So something like:

def count_zeros(number:int) -> int:

    if number==0:
        return 1

    number_list = list(str(number))

    is_zero = True
    zero_count = 0

    while is_zero:
        if int(number_list.pop())==0:
            zero_count += 1
        else:
            is_zero = False

    return zero_count
def end_zeros(a: int) -> int:  
    count = 0 
    list = [int(x) for x in str(a)]
    list.reverse()
    list.append(1)
    for x in list:
        if x == 0:
            count += 1            
        else:
            return count
Related