Python Program to test whether the length of rows are in increasing order
Given a Matrix, the following program is used to test if length all rows of a matrix are in increasing order or not. If yes, it returns True, otherwise False.
Input : test_list = [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]]
Output : True
Explanation : 1 < 2 < 3 < 5, increasing lengths of rows, hence True.
Input : test_list = [[3], [1, 7], [10], [8, 6, 5, 1, 4]]
Output : False
Explanation : 1 <2 >1 < 5, Non-increasing lengths of rows, hence False.
Method 1: Using loop and len()
In this, we are using loop to check whether the length of next row is greater than the present row, if not, result is flagged off.
Python3
test_list = [[ 3 ], [ 1 , 7 ], [ 10 , 2 , 4 ], [ 8 , 6 , 5 , 1 , 4 ]]
print ( "The original list is : " + str (test_list))
res = True
for idx in range ( len (test_list) - 1 ) :
if len (test_list[idx + 1 ]) < = len (test_list[idx]):
res = False
break
print ( "Are rows of increasing lengths ? : " + str (res))
|
Output:
The original list is : [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]]
Are rows of increasing lengths ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using all() and list comprehension
In this, we test for next length being greater than the current length using len() and all() is used to check for all the rows.
Python3
test_list = [[ 3 ], [ 1 , 7 ], [ 10 , 2 , 4 ], [ 8 , 6 , 5 , 1 , 4 ]]
print ( "The original list is : " + str (test_list))
res = all ( len (test_list[idx + 1 ]) > len (test_list[idx]) for idx in range ( len (test_list) - 1 ))
print ( "Are rows of increasing lengths ? : " + str (res))
|
Output:
The original list is : [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]]
Are rows of increasing lengths ? : True
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in all() and list comprehension which all has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re not using additional space other than the input list itself.
Method 3: Using len() and sort()
In this we will append length of each row to a new list, later we will check whether the initial list and list after sorting are same or not.If same the length of rows are in increasing manner
Python3
test_list = [[ 3 ], [ 1 , 7 ], [ 10 ], [ 8 , 6 , 5 , 1 , 4 ]]
print ( "The original list is : " + str (test_list))
res = False
x = []
for i in test_list:
x.append( len (i))
a = x
x = x.sort()
if (a = = x):
res = True
print ( "Are rows of increasing lengths ? : " + str (res))
|
Output
The original list is : [[3], [1, 7], [10], [8, 6, 5, 1, 4]]
Are rows of increasing lengths ? : False
Method 4: Using map() and sorted() function
- Initialize the list lengths with the lengths of each sublist in test_list.
- Sort the list lengths in ascending order using the sorted() function.
- Compare the sorted list lengths with the original list lengths using the == operator to check if the rows are of increasing lengths.
- Return the result as a boolean value.
Python3
test_list = [[ 3 ], [ 1 , 7 ], [ 10 ], [ 8 , 6 , 5 , 1 , 4 ]]
print ( "The original list is : " + str (test_list))
lengths = list ( map ( len , test_list))
res = lengths = = sorted (lengths)
print ( "Are rows of increasing lengths ? : " + str (res))
|
Output
The original list is : [[3], [1, 7], [10], [8, 6, 5, 1, 4]]
Are rows of increasing lengths ? : False
Time complexity: O(n log n), where n is the number of sublists in test_list.
Auxiliary space: O(n), where n is the number of sublists in test_list.
Last Updated :
18 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...