Open In App

Python program to sort tuples by frequency of their absolute difference

Given a list of dual tuples, the task here is to write a Python program that can sort them by the frequency of their elements’ absolute differences.

Input : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Output : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]
Explanation : 7 - 5 = 2 occurs only 1 time. 5 occurs twice [( 6 - 1), (11 - 6)] 
              and 8 occurs 3 times as difference.
Input : [(1, 6), (6, 11), (5, 7)]
Output : [(5, 7), (1, 6), (6, 11)]
Explanation : 7 - 5 = 2 occurs only 1 time. 5 occurs twice [( 6 - 1), (11 - 6)].

Method 1: Using sorted(), abs(), count() and list comprehension



In this, we perform the task of computing each absolute difference using abs() and list comprehension. Then, sorted() and count() are used to sort tuples based on computed results of absolute difference.




# initializing list
test_list = [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting differences pairs
diff_list = [abs(x - y) for x, y in test_list]
 
# sorting list by computed differences
res = sorted(test_list, key = lambda sub: diff_list.count(abs(sub[0] - sub[1])))
 
# printing result
print("Sorted Tuples : " + str(res))

Output:



The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]

Sorted Tuples : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]

Time Complexity: O(n*logn)
Auxiliary Space: O(1)

Method 2: operator.countOf() and list comprehension




import operator as op
# initializing list
test_list = [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting differences pairs
diff_list = [abs(x - y) for x, y in test_list]
 
# sorting list by computed differences
res = sorted(test_list, key = lambda sub: op.countOf(diff_list,abs(sub[0] - sub[1])))
 
# printing result
print("Sorted Tuples : " + str(res))

Output
The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Sorted Tuples : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]

Time Complexity: O(NLogN), where n is the length of the given string
Auxiliary Space: O(N)

Method 3: Using the map() function and a lambda function 

Step-by-step approach:




# Using map()  + lambda
 
# initializing list
test_list = [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting differences pairs
diff_list = list(map(lambda t: abs(t[0] - t[1]), test_list))
 
# sorting list by computed differences
res = sorted(test_list, key=lambda t: diff_list[test_list.index(t)])
 
# printing result
print("Sorted Tuples : " + str(res))

Output
The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Sorted Tuples : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]

Time complexity: O(nlogn), where n is the length of the list of tuples since sorting takes O(nlogn) time. 
Auxiliary space: O(n), since we create a list of absolute differences that is the same length as the list of tuples

Method 4: Using the heapq module 




import heapq
 
# initializing list
test_list = [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# computing differences and storing them in a list
diff_list = [abs(t[0] - t[1]) for t in test_list]
 
# sorting list based on computed differences
heap = [(diff, t) for diff, t in zip(diff_list, test_list)]
heapq.heapify(heap)
res = [heapq.heappop(heap)[1] for _ in range(len(heap))]
 
# printing result
print("Sorted Tuples : " + str(res))

Output
The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Sorted Tuples : [(5, 7), (1, 6), (6, 11), (2, 10), (9, 1), (11, 3)]

Time complexity: O(n log n) 
Auxiliary space: O(n)


Article Tags :