![]() |
VOOZH | about |
Given a tuple list, get all the tuples which are sorted in ascending order.
Input : test_list = [(5, 4, 6, 2, 4), (3, 4, 6), (2, 5, 6), (9, 1)]
Output : [(3, 4, 6), (2, 5, 6)]
Explanation : Sorted tuples are extracted.Input : test_list = [(5, 4, 6, 2, 4), (3, 4, 1), (2, 5, 4), (9, 1)]
Output : []
Explanation : No Sorted tuples.
Method #1 : Using list comprehension + sorted()
In this, we check if tuple is ordered using sorted(), and list comprehension is used to iterate for each tuple.
The original list is : [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)] Ordered Tuples : [(3, 4, 6), (9, 10, 34), (2, 5, 6)]
Time Complexity: O(nlogn), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using filter() + lambda + sorted()
In this, the task of filtering is done using filter(), sorted() fed to lambda for with comparison to get required result.
The original list is : [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)] Ordered Tuples : [(3, 4, 6), (9, 10, 34), (2, 5, 6)]
Method#3: Using Recursive method.
The original list is : [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)] Ordered Tuples : [(3, 4, 6), (9, 10, 34), (2, 5, 6)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using all()
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)] Ordered Tuples: [(3, 4, 6), (9, 10, 34), (2, 5, 6)]
Time complexity: The time complexity of this approach is O(n), where n is the length of the input string. This is because we need to traverse through each character in the string once.
Auxiliary space: The auxiliary space complexity of this approach is O(n), where n is the length of the input string. This is because we need to store the opening parenthesis in the stack and the maximum size of the stack is equal to the length of the input string.
Method #5: Using the itertools.groupby() function:
Algorithm:
1.Import itertools module
2.Initialize an empty list ordered_tuples to store ordered tuples
3.Iterate over the groups returned by itertools.groupby() method, grouped based on whether each tuple satisfies the ordering condition
4.If a group contains only ordered tuples, extend the ordered_tuples list with the tuples in the group
5.Print the ordered_tuples list
The original list is : [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)] Ordered Tuples: [(3, 4, 6), (9, 10, 34), (2, 5, 6)]
Time complexity: O(nlogn) where n is the total number of elements in all tuples. This is because the itertools.groupby() method and the all() function take O(n) time complexity, while the extend() method takes O(m) time complexity, where m is the number of elements in the group of ordered tuples. The total time complexity is dominated by the sorting that happens inside itertools.groupby(), which has a time complexity of O(nlogn).
Auxiliary Space: O(m) where m is the number of elements in the largest group of ordered tuples. This is because the itertools.groupby() method groups the input list into separate sublists, and the largest sublist needs to be stored in memory.
Method #6: Using map() and set()
Step-by-step approach:
Below is the implementation of the above approach:
Ordered Tuples: [(3, 4, 6), (9, 10, 34), (2, 5, 6)]
Time complexity: O(nklog k)
Auxiliary space: O(n*k)