You are given a list of integers. Your task is to count how many sublists contain only even numbers.
For example, the sublists of the list are:
, , , , , , , , ,
In this case, the desired answer is , because the sublists with only even numbers are , , and .
In a file even.py
, implement the function count_sublists
that takes the list of numbers as a parameter and returns the count of even only sublists.
You must implement an efficient solution with a time complexity . You cannot afford to go through all sublists separately. Your solution should go through the input list only once.
In the last test case of the following code template, the list contains copies of the number and thus all sublists consist of even numbers only. Your function should finish quickly in this test case too.
def count_sublists(numbers): # TODO if __name__ == "__main__": print(count_sublists([2, 4, 1, 6])) # 4 print(count_sublists([1, 2, 3, 4])) # 2 print(count_sublists([1, 1, 1, 1])) # 0 print(count_sublists([2, 2, 2, 2])) # 10 print(count_sublists([1, 1, 2, 1])) # 1 numbers = [2] * 10**5 print(count_sublists(numbers)) # 5000050000