Python – Test Consecutive Element Matrix
Last Updated :
03 Jun, 2023
Given a Matrix, test if it is made of consecutive elements.
Input : test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Output : True
Explanation : Elements in Matrix range from 4 to 12.
Input : test_list = [[4, 5, 6], [7], [8, 18, 10], [11, 12]]
Output : False
Explanation : Elements not consecutive and not in a range.
Method #1: Using loop
In this, we check for consecution within the row, and before each row, check for 1st element to be next to last element from previous row.
Python3
test_list = [[ 4 , 5 , 6 ], [ 7 ], [ 8 , 9 , 10 ], [ 11 , 12 ]]
print ( "The original list is : " + str (test_list))
res = True
mem_list = []
for sub in test_list:
flag = True
for ele in sub:
if len (mem_list) ! = 0 :
if mem_list[ - 1 ] ! = ele - 1 :
flag = False
break
mem_list.append(ele)
if not flag:
res = False
break
print ( "Is Matrix Consecutive ? : " + str (res))
|
Output
The original list is : [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Is Matrix Consecutive ? : True
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #2 : Using chain.from_iterable() + all()
In this, we flatten matrix to list, and then check for all elements in consecution using all(), to be 1 greater than the preceding element in flattened Matrix.
Python3
from itertools import chain
test_list = [[ 4 , 5 , 6 ], [ 7 ], [ 8 , 9 , 10 ], [ 11 , 12 ]]
print ( "The original list is : " + str (test_list))
test_list = list (chain.from_iterable(test_list))
res = all (test_list[idx - 1 ] = = test_list[idx] -
1 for idx in range ( 1 , len (test_list)))
print ( "Is Matrix Consecutive ? : " + str (res))
|
Output
The original list is : [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Is Matrix Consecutive ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using extend(),min(),max() and range() methods
Python3
test_list = [[ 4 , 5 , 6 ], [ 7 ], [ 8 , 9 , 10 ], [ 11 , 12 ]]
print ( "The original list is : " + str (test_list))
res = False
x = []
for i in test_list:
x.extend(i)
a = min (x)
b = max (x)
y = list ( range (a,b + 1 ))
if (x = = y):
res = True
print ( "Is Matrix Consecutive ? : " + str (res))
|
Output
The original list is : [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Is Matrix Consecutive ? : True
Time Complexity: O(n^2), where n is the number of sublists in the list.
Auxiliary Space: O(n), as extra space is required of size n.
Method #4: Using set and range()
- Initialize a set to store all the unique elements from the nested list
- Iterate through each list in the nested list and update the set with its elements
- Check if the length of the set is equal to the difference between the maximum and minimum element in the set plus 1 (since we want to check for consecutive elements)
- If the condition is true, set res to True, else set it to False
- Print the final result
Python3
test_list = [[ 4 , 5 , 6 ], [ 7 ], [ 8 , 9 , 10 ], [ 11 , 12 ]]
print ( "The original list is : " + str (test_list))
elements_set = set ()
for sublist in test_list:
elements_set.update(sublist)
if len (elements_set) = = max (elements_set) - min (elements_set) + 1 :
res = True
else :
res = False
print ( "Is Matrix Consecutive ? : " + str (res))
|
Output
The original list is : [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Is Matrix Consecutive ? : True
Time complexity: O(nlogn) – iterating through the nested list takes O(n) time and checking for consecutive elements in the set takes O(nlogn) time due to the set being sorted internally.
Auxiliary space: O(n) – storing all the unique elements in the set.
Method #5: Using numpy.diff() function
- Import the numpy package as np to use its functions.
- Create a nested list test_list containing multiple sublists of integers.
- Print the original list using the print statement and the str() function to convert the list to a string.
- Set the variable res to True.
- Loop through each sublist in the test_list using the for loop.
- Use the numpy diff() function to calculate the difference between adjacent elements in the current sublist. This returns a new array with one less element than the original sublist.
- Use the numpy all() function to check if all the elements in the resulting array are equal to 1. If yes, continue to the next sublist. If not, set res to False and break out of the loop.
- Print the final result using the print statement and the str() function to convert the boolean value to a string.
Python3
import numpy as np
test_list = [[ 4 , 5 , 6 ], [ 7 ], [ 8 , 9 , 10 ], [ 11 , 12 ]]
print ( "The original list is : " + str (test_list))
res = True
for sub in test_list:
if np. all (np.diff(sub) = = 1 ):
continue
else :
res = False
break
print ( "Is Matrix Consecutive ? : " + str (res))
|
OUTPUT :
The original list is : [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Is Matrix Consecutive ? : True
Time complexity: O(nm) where n is the number of sublists in the input list and m is the maximum number of elements in a sublist.
Auxiliary space: O(m) where m is the maximum number of elements in a sublist.
Method 6 : using a list comprehension
- Initialize the test_list with the given values: test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]].
- Initialize the variable res as True. This variable will store the result of whether the matrix is consecutive or not.
- Iterate over each sublist in the test_list using the outer loop for i in range(len(test_list)).
- For each sublist, iterate over its elements using the inner loop for j in range(len(test_list[i]) – 1).
- Inside the loop, check if the consecutive elements condition is satisfied by comparing the difference between the current element and the next element in the sublist with 1: test_list[i][j + 1] – test_list[i][j] == 1.
- If the condition is satisfied for all elements in all sublists, the matrix is consecutive. If any sublist has non-consecutive elements, set res to False and break out of the loop.
- After the loops, res will hold the result of whether the matrix is consecutive or not.
- Print the result by converting res to a string: print(“Is Matrix Consecutive? : ” + str(res)).
Python3
test_list = [[ 4 , 5 , 6 ], [ 7 ], [ 8 , 9 , 10 ], [ 11 , 12 ]]
res = all (test_list[i][j + 1 ] - test_list[i][j] = = 1
for i in range ( len (test_list))
for j in range ( len (test_list[i]) - 1 ))
print ( "Is Matrix Consecutive? : " + str (res))
|
Output
Is Matrix Consecutive? : True
Time complexity: O(n), where n is the total number of elements in the input list.
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...