![]() |
VOOZH | about |
Given a list of tuples. The task is to get all the tuples that have all positive elements.
Examples:
Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [(4, 5, 9)] Explanation : Extracted tuples with all positive elements. Input : test_list = [(-4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [] Explanation : No tuple with all positive elements.
Method #1 : Using list comprehension + all()
In this, all() is used to check for all the tuples, list comprehension helps in the iteration of tuples.
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time complexity: O(n*m), where n is the number of tuples in the list and m is the number of elements in each tuple.
Auxiliary space: O(n), as a new list is created to store the positive tuples.
Method #2 : Using filter() + lambda + all()
In this, the task of filtration is performed using filter() and lambda function.
The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time complexity: O(n) where n is the number of elements in the list.
Auxiliary space: O(1) as the only extra space used is to store the result in a new list.
Method #3 : Using find(),map(),list() and join()
The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), as the result list is storing the positive tuples.
Method #4 : Using list(),map(),join() and startswith() methods
The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time complexity: O(nlogn) where n is the length of the input list.
Auxiliary space: O(n) where n is the length of the input list.
Method #5: By defining a function and using len() method
The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time complexity: O(nm), where n is the length of the input list and m is the length of the tuples in the list.
Auxiliary space: O(k), where k is the length of the output list.
Method #6: Using list comprehension + not any()
In this, not any() is used to check for all the tuples, list comprehension helps in the iteration of tuples.
The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time Complexity:O(n)
Auxiliary Space :O(n)
Method #7: Without using built-in function
The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time Complexity:O(n)
Auxiliary Space :O(n)
Method #8: Using for loop and if condition to filter tuples with positive elements:
Approach :
Positive elements Tuples : [(4, 5, 9), (4, 6)]
Time Complexity: O(n)
Auxiliary Space: O(n)
We cam check if a tuple contains a negative number "-" in the string of tuple, we can use regular expressions (re) in Python. The approach is to convert the tuple to a string using str() and then search for a "-" using re.search(). If the string contains a "-", it means that there is at least one negative number in the tuple.
Here's the algorithm for the same:
Algorithm:
Original List: [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)] Positive List: [(4, 5, 9), (4, 6)]
The time complexity of the given code is O(nm), where n is the length of the test_list and m is the maximum length of the tuple in the list.
This is because the code involves a loop through each tuple in the list, and for each tuple, it converts it to a string and searches for the "-" character using the re.search() function, which has a time complexity of O(m).
The space complexity of the given code is also O(nm), because it creates a new string representation of each tuple, which takes up O(m) space, and stores the positive tuples in a new list, which takes up O(nm) space in the worst case.