Sometimes, while working with records, we can have a problem in which we require to perform an index-wise maximum of tuple elements. This can get complicated with tuple elements being tuples and inner elements again being tuples. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using zip() + max() + nested generator expression
The combination of the above functions can be used to perform the task. In this, we combine the elements across tuples using zip(). The iterations and maximize logic are provided by the generator expression.
Python3
test_tup1 = (( 1 , 3 ), ( 4 , 5 ), ( 2 , 9 ), ( 1 , 10 ))
test_tup2 = (( 6 , 7 ), ( 3 , 9 ), ( 1 , 1 ), ( 7 , 3 ))
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( tuple ( max (a, b) for a, b in zip (tup1, tup2))
for tup1, tup2 in zip (test_tup1, test_tup2))
print ( "The resultant tuple after maximization : " + str (res))
|
Output :
The original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Method #2: Using isinstance() + zip() + max() + loop + list comprehension
The combination of the above functions can be used to perform this particular task. In this, we check for the nesting type and perform recursion. This method can give flexibility of more than 1 level nesting.
Python3
def tup_max(tup1, tup2):
if isinstance (tup1, ( list , tuple )) and isinstance (tup2, ( list , tuple )):
return tuple (tup_max(x, y) for x, y in zip (tup1, tup2))
return max (tup1, tup2)
test_tup1 = (( 1 , 3 ), ( 4 , 5 ), ( 2 , 9 ), ( 1 , 10 ))
test_tup2 = (( 6 , 7 ), ( 3 , 9 ), ( 1 , 1 ), ( 7 , 3 ))
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple (tup_max(x, y) for x, y in zip (test_tup1, test_tup2))
print ( "The resultant tuple after maximization : " + str (res))
|
Output :
The original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity: O(n), where ‘n’ is the length of the tuples.
Auxiliary space: O(n), where ‘n’ is the length of the tuples.
Method #3: Using map+lambda
Approach: Use the map() function to apply a lambda function to each pair of corresponding elements in the input tuples.
The lambda function takes two tuples as input and uses the map() function with the max() function to create a new tuple with the maximum elements from each pair of nested tuples. The map() function returns an iterator over the resulting tuples, which we convert to a tuple using the tuple() function.
Algorithm:
- Use the `map()` function with a lambda function to apply the `max()` function to each pair of corresponding elements in the input tuples.
- The lambda function takes two tuples as input, and uses the `map()` function with the `max()` function to create a new tuple with the maximum elements from each pair of nested tuples.
- Convert the resulting iterator over tuples to a tuple using the `tuple()` function.
- Return the tuple of maximized nested tuples.
Python3
tuple1 = (( 1 , 3 ), ( 4 , 5 ), ( 2 , 9 ), ( 1 , 10 ))
tuple2 = (( 6 , 7 ), ( 3 , 9 ), ( 1 , 1 ), ( 7 , 3 ))
max_tuple = tuple ( map ( lambda t1, t2: tuple ( map ( max , t1, t2)), tuple1, tuple2))
print (max_tuple)
|
Output
((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity: O(n)
Where n is the length of the input tuples. This is because the lambda function takes two tuples as input and applies the map function with the max function to each pair of corresponding elements in the input tuples. Since each tuple has a length of two, the map function with the max function takes constant time. Therefore, the time complexity of the lambda function is O(1). The map function applies the lambda function to each pair of corresponding elements in the input tuples, which takes O(n) time, where n is the length of the input tuples. Finally, the tuple function creates a new tuple with the same length as the input tuples, which also takes O(n) time. Therefore, the overall time complexity of this code is O(n).
Auxiliary Space: O(n)
Where n is the length of the input tuples. This is because the map function and lambda function create a new tuple with the same length as the input tuples, and the resulting tuple is stored in memory. The space complexity of the map function and lambda function is O(n), where n is the length of the input tuples. The space complexity of the tuple function is also O(n), since it creates a new tuple with the same length as the input tuples. Therefore, the overall space complexity of this code is O(n).
Method 4: Using the built-in function map() along with a lambda function
This program initializes two tuples test_tup1 and test_tup2, applies the max() function to corresponding elements of the tuples using map() and lambda, and converts the resulting list of tuples to a tuple of tuples using tuple(). The program then prints the original and resulting tuples.
- Define the two tuples to be maximized: test_tup1 and test_tup2.
- Apply the map() function to the zip() of test_tup1 and test_tup2, with a lambda function that applies max() to each pair of corresponding elements. This generates a list of tuples with the maximum values.
- Convert the resulting list of tuples to a tuple of tuples using the tuple() function.
- Print the original tuples and the resulting tuple after maximization.
Python3
test_tup1 = (( 1 , 3 ), ( 4 , 5 ), ( 2 , 9 ), ( 1 , 10 ))
test_tup2 = (( 6 , 7 ), ( 3 , 9 ), ( 1 , 1 ), ( 7 , 3 ))
max_tuples = map ( lambda tup: tuple ( map ( max , * tup)), zip (test_tup1, test_tup2))
res = tuple (max_tuples)
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
print ( "The resultant tuple after maximization : " + str (res))
|
Output
The original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity of this approach is O(nm)
where n is the length of the tuples and m is the length of the sub-tuples.
Auxiliary space complexity is O(nm)
As a new tuple of tuples is created to store the resulting values.
Method #5: Using recursion
Here’s another approach to solve the problem using recursion. This method works by recursively comparing the nested tuples in the input tuples and returning the maximum values.
Step-by-step approach:
- Define a function max_nested_tuple that takes two tuples as input parameters.
- In the function, check if both the input parameters are tuples or not using the isinstance() method. If not, return the maximum value.
- If both parameters are tuples, initialize an empty list to store the maximum values.
- Loop through each tuple element in the input tuples using the zip() method and recursively call the max_nested_tuple function on each tuple element.
- Append the maximum value of each pair of tuple elements to the list initialized in step 3.
- Convert the list to a tuple using the tuple() method and return the tuple.
Python3
def tup_max(tup1, tup2):
if isinstance (tup1, tuple ) and isinstance (tup2, tuple ):
res = tuple (tup_max(t1, t2) for t1, t2 in zip (tup1, tup2))
return res
return max (tup1, tup2)
test_tup1 = (( 1 , 3 ), ( 4 , 5 ), ( 2 , 9 ), ( 1 , 10 ))
test_tup2 = (( 6 , 7 ), ( 3 , 9 ), ( 1 , 1 ), ( 7 , 3 ))
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tup_max(test_tup1, test_tup2)
print ( "The resultant tuple after maximization : " + str (res))
|
Output
The original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity: The time complexity of this method is O(n^2), where n is the number of elements in the tuples. This is because it involves recursive function calls for each element in the tuples.
Auxiliary space: The auxiliary space used by this method is O(n^2) as well, since it creates new tuples at each recursive call.
Last Updated :
06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...