w3resource

Python: Count the number of groups of non-zero numbers separated by zeros of a given list of numbers

Python List: Exercise - 209 with Solution

Count Non-Zero Groups

Write a Python program to count the number of groups of non-zero numbers separated by zeros in a given list of numbers.

Sample Solution:

Python Code:

# Define a function 'test' that counts the number of groups of non-zero numbers separated by zeros in a list.
def test(lst):
    # Initialize variables 'previous_digit' to store the previous digit and 'ctr' to count the groups.
    previous_digit = 0
    ctr = 0
    
    # Iterate through the elements in the input list 'lst'.
    for digit in lst:
        # Check if the previous digit was zero and the current digit is non-zero.
        if previous_digit == 0 and digit != 0:
            # Increment the group count.
            ctr += 1
        # Update the 'previous_digit' for the next iteration.
        previous_digit = digit
    
    return ctr

# Define a list of numbers 'nums'.
nums = [3, 4, 6, 2, 0, 0, 0, 0, 0, 0, 6, 7, 6, 9, 10, 0, 0, 0, 0, 0, 5, 9, 9, 7, 4, 4, 0, 0, 0, 0, 0, 0, 5, 3, 2, 9, 7, 1]

# Print a message indicating the original list.
print("\nOriginal list:")
# Print the original list of numbers 'nums'.
print(nums)

# Print a message indicating the purpose of the following lines of code.
print("\nNumber of groups of non-zero numbers separated by zeros of the said list:")
# Call the 'test' function to count the groups of non-zero numbers separated by zeros in 'nums' and print the result.
print(test(nums)) 

Sample Output:

Original list:
[3, 4, 6, 2, 0, 0, 0, 0, 0, 0, 6, 7, 6, 9, 10, 0, 0, 0, 0, 0, 5, 9, 9, 7, 4, 4, 0, 0, 0, 0, 0, 0, 5, 3, 2, 9, 7, 1]

Number of groups of non-zero numbers separated by zeros of the said list:
4

Flowchart:

Flowchart: Count the number of groups of non-zero numbers separated by zeros of a given list of numbers.

Python Code Editor:

Previous: Sum a list of numbers. Write a Python program to sum the first number with the second and divide it by 2, then sum the second with the third and divide by 2, and so on.
Next: Write a Python program to compute the sum of non-zero groups (separated by zeros) of a given list of numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-209.php