Python | N consecutive Odd or Even Numbers
Last Updated :
15 May, 2023
The problem focused on in this article is quite specific and may be less useful in different domains. But the way this is going to solve may open doors to solve potentially like problems, hence making it worth a read. This article solves the problem of testing if a list contains a series of odd or even elements. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using sum() + list comprehension + zip() + any()
This problem can be solved using a combination of the above functions. This method solves the problem in 2 steps. In 1st step, we compute all the possible pairs of N using list comprehension and zip function and in the second step we use sum and any function to test for N divisible result, if we find any one of them, we return positive.
Python3
test_list = [ 1 , 5 , 6 , 4 , 8 ]
print ( "The original list : " + str (test_list))
N = 3
temp = (test_list[i: i + N] for i in range ( len (test_list) - N + 1 ))
res = any ( sum (ele % 2 for ele in temps) % N = = 0 for temps in temp)
print ( "Does list contain the desired consecution : " + str (res))
|
Output
The original list : [1, 5, 6, 4, 8]
Does list contain the desired consecution : True
Time Complexity: O(n*n) where n is the number of elements in the dictionary. The sum() + list comprehension + zip() + any() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(1) constant additional space is required.
Method #2: Using groupby() + any()
The whole logic of doing the 1st step in the above method can be managed using the groupby function in which we perform the grouping and any function can be used later for checking the consecution.
Python3
from itertools import groupby
test_list = [ 1 , 5 , 6 , 4 , 8 ]
print ( "The original list : " + str (test_list))
N = 3
res = any ( len ( list (sub)) = = N for idx, sub in
groupby([sub % 2 for sub in test_list]))
print ( "Does list contain the desired consecution : " + str (res))
|
Output
The original list : [1, 5, 6, 4, 8]
Does list contain the desired consecution : True
Time complexity: O(N), where N is the length of the input list test_list.
Auxiliary space: O(N), as the groupby() function creates an iterator that produces a new group object for each unique consecutive sequence of parity in the input list.
Method #3: Using all()
The code first initializes test_list and N. Then it iterates over sublists of test_list with length N, using a generator expression. For each sublist, it checks if all elements have the same parity (odd or even) by comparing the parity of the first element to the parity of the other elements. If the condition is met, it sets the result to True and exits the loop. If the loop ends without finding a sublist that meets the condition, it sets the result to False. Finally, it prints the result.
Python3
test_list = [ 1 , 5 , 6 , 4 , 8 ]
N = 3
print ( "The original list : " + str (test_list))
for group in (test_list[i:i + N] for i in range ( len (test_list) - N + 1 )):
if all (num % 2 = = group[ 0 ] % 2 for num in group):
result = True
break
else :
result = False
print ( "Does list contain the desired consecution : " + str (result))
|
Output
The original list : [1, 5, 6, 4, 8]
Does list contain the desired consecution : True
Time complexity: O(n), since it iterates over the elements of test_list once.
Auxiliary space: O(1), since the size of the variables used is constant and independent of the size of the input.
Method #4: Using numpy array slicing and broadcasting
The numpy approach for solving this problem involves converting the input list to a numpy array, slicing the array to create subarrays of length N, and then checking if all the elements in each subarray have the same parity (odd or even) using numpy broadcasting.
Python
import numpy as np
test_list = [ 1 , 5 , 6 , 4 , 8 ]
N = 3
arr = np.array(test_list)
subarrays = arr[np.arange( len (arr) - N + 1 )[:, None ] + np.arange(N)]
result = np. all (subarrays % 2 = = subarrays[ 0 ] % 2 , axis = 1 ). any ()
print ( "Does list contain the desired consecution : " + str (result))
|
Output:
Does list contain the desired consecution : True
Time complexity: O(n), since it involves slicing the numpy array, which takes constant time.
Auxiliary space: O(n), since it creates a numpy array of size n and subarrays of size N.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...