Python Program to test whether the length of rows are in increasing order
Last Updated :
18 May, 2023
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
# Initializing list
test_list = [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]]
# Printing original list
print("The original list is : " + str(test_list))
res = True
for idx in range(len(test_list) - 1) :
# flag off in case next length is not greater
# than current
if len(test_list[idx + 1]) <= len(test_list[idx]):
res = False
break
# Printing result
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
# initializing list
test_list = [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]]
# printing original list
print("The original list is : " + str(test_list))
# checking for truth for all rows in matrix
res = all(len(test_list[idx + 1]) > len(test_list[idx]) for idx in range(len(test_list) - 1))
# printing result
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
# initializing list
test_list = [[3], [1, 7], [10], [8, 6, 5, 1, 4]]
# printing original list
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
# printing result
print("Are rows of increasing lengths ? : " + str(res))
OutputThe 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
# initializing list
test_list = [[3], [1, 7], [10], [8, 6, 5, 1, 4]]
# printing original list
print("The original list is : " + str(test_list))
# Method 4: Using map() and sorted()
lengths = list(map(len, test_list))
res = lengths == sorted(lengths)
# printing result
print("Are rows of increasing lengths ? : " + str(res))
OutputThe 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.
Similar Reads
Python Program that prints the rows of a given length from a matrix Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
4 min read
Python Program to extracts elements from the list with digits in increasing order Given a List of elements, extract all elements which have digits that are increasing in order. Input : test_list = [1234, 7373, 3643, 3527, 148, 49] Output : [1234, 148, 49] Explanation : All elements have increasing digits.Input : test_list = [12341, 7373, 3643, 3527, 1481, 491] Output : [] Explana
5 min read
Python Program to convert a list into matrix with size of each row increasing by a number Given a list and a number N, the task here is to write a python program to convert it to matrix where each row has N elements more than previous row elements from list. Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], N = 3 Output : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10
6 min read
Python program to print Rows where all its Elements' frequency is greater than K Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3
8 min read
Python Program to get the Longest Alphabetic order of Kth index from list values Given a string list, the task is to write a Python program to extract Strings that form the longest increasing alphabetic order at the Kth index. K should be less than the minimum length of all strings. Input : test_list = ["gfg", "is", "best", "for", "geeks", "and", "cs"], K = 0 Output : ['best', '
4 min read
Python program to extract rows with common difference elements Given a Matrix, extract rows with AP sequence. Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]] Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]] Explanation : 3, 4, and 2 are common difference in AP. Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]] Output
3 min read