Count Consecutive Zeroes in Python

In this class, we write a program for Count Consecutive Zeroes in Python.

For Complete YouTube Video: Click Here

Count Consecutive Zeroes

Q1) Write a program for the given below conditions in python.

Take two inputs.

The first input is a number. And the number should be greater than zero. i.e., positive number.

The second input is an integer number. And the number is considered as the base value. So the base value should be greater than zero and less than the first input.

Convert the given decimal number to the given base number.

Example:

Number=72

Base=2

We have to convert the decimal number to binary number.

After converting the decimal number to the given base number, identify the maximum consecutive zero’s count.

Suppose the decimal number is converted to binary. The result is 1001000.

From the above binary number maximum, consecutive zero’s count is 3.

Output: 3

Logic

In our python course, we discussed converting a decimal number to binary.

The same logic is used here. So half of the program we discussed in python. Click here.

All these examples help you in improving your coding skills and easily crack placement exams.

Refresh the conversion logic once. And continue the remaining part.

Converting a decimal number to any base. We need to divide the number by the given base value.

Reminder value is considered.

Keep dividing the number till the number is zero. Each time consider the reminder.

After conversion, we have to find the consecutive zeroes count.

The logic to find the consecutive zeroes count is to iterate on the output obtained after conversion.

Take two variables count and maximum count.

Both variables should initialize to zero.

Iterate on the converted number. If the digit is zero, then increment the count.

If the digit is not zero, make the count value zero.

Each iteration checks the count variable with the maximum count variable to update the maximum consecutive zero’s count.

Please analyze the program and understand the logic for better practice.

Program

num=int(input("enter the number >0"))
base=int(input("enter the base value >0"))
list1=[]
maximum=0
count=0
while (num>0):
    rem=num%base
    list1.append(rem)
    num=num//base
for i in list1:
    if i==0:
        count+=1
        if count>maximum:
            maximum=count
    else:
        count=0
print(maximum)
print(list1)