Friday, September 5, 2025
HomeLanguagesPython – Sort row by K multiples

Python – Sort row by K multiples

Given a Matrix, perform row sorting by number of multiple of K present in row.

Input : test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]], K = 4 
Output : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]] 
Explanation : 0 < 1 < 2 < 4, multiple of 4 occurrence order.

Input : test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]], K = 2 
Output : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]] 
Explanation : 0 < 2 = 2 < 4, multiple of 2 occurrence order. 

Method #1 : Using sort() + % operator + len()

In this, we test for multiple using % operator and then compute count by getting length of filtered elements, provided to key to sort() which performs inplace sorting of rows.

Python3




# Python3 code to demonstrate working of
# Sort row by K multiples
# Using sort() + % operator + len()
 
# checking for multiples count
 
 
def k_mul(row):
    return len([ele for ele in row if ele % K == 0])
 
 
# initializing list
test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# performing sort
test_list.sort(key=k_mul)
 
# printing result
print("Sorted result : " + str(test_list))


Output:

The original list is : [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]] 
Sorted result : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]]

Time Complexity: O(nlogn*mlogm)
Auxiliary Space: O(1)

Method #2 : Using sorted() + lambda + len()

In this, sorting is done using sorted(), len() is used to get length of all the multiples of K as in above method. The lambda function provides single statement alternative to perform logical injection.

Python3




# Python3 code to demonstrate working of
# Sort row by K multiples
# Using sorted() + lambda + len()
 
# initializing list
test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# performing sort using sorted()
# lambda avoiding external function call
res = sorted(test_list, key=lambda row: len(
    [ele for ele in row if ele % K == 0]))
 
# printing result
print("Sorted result : " + str(res))


Output:

The original list is : [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]] 
Sorted result : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]]

Time Complexity: O(n*logn), where n is the length of the input list. This is because we’re using the built-in sorted() function which has a time complexity of O(nlogn) in the worst case.
Auxiliary Space: O(n), as we’re using additional space other than the input list itself. 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11860 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS