ADVERTISEMENT
ADVERTISEMENT

Python program to find the maximum nested tuple

In this program, we are given two tuples. We need to create a Python program to maximize nested Tuples.
Submitted by Shivang Yadav, on November 22, 2021

Extracting fruitful information from data sets is very important when it comes to working with complex collections in python. Program to perform Comparative selection, extraction of stuff based on a certain condition may contribute to your code and make your program work more effectively. In this program, we will find the maximum nested tuple in python.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Finding the Maximum Nested Tuple

To find the maximum nested tuple, we will loop through both the tuple and for each nested tuple element find the maximum of each and create a nested collection similar to the initial one.

Input:
tuple1 = ((1, 6), (2, 5), (8, 9))
tuple2 = ((3, 4), (5, 7), (9, 3))

Output:
((3, 6), (5, 7), (9, 9))

In Python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.

Method 1:

One method to solve the problem is by finding the maximum of elements zipped together using the zip() method using the max() method. All the logic is put together using the generator expression.

# Python program to find the Maximum Nested Tuple

# Initialize tuples
# Initializing and printing initial Tuples
tuple1 = ((1, 6), (2, 5), (8, 9))
tuple2 = ((3, 4), (5, 7), (9, 3))
print("The values of Tuple 1 are " + str(tuple1))
print("The values of Tuple 2 are " + str(tuple2))

# Finding maximum nested Tuples 
maxTuple = tuple(tuple(max(val1, val2) for val1, val2 in zip(tup1, tup2))\
	for tup1, tup2 in zip(tuple1, tuple2))

# Printing the maximum nested Tuple 
print("The values of tuple after maximization : " + str(maxTuple))

Output:

The values of Tuple 1 are ((1, 6), (2, 5), (8, 9))
The values of Tuple 2 are ((3, 4), (5, 7), (9, 3))
The values of tuple after maximization : ((3, 6), (5, 7), (9, 9))

Method 2:

An alternate method to solve the problem is by checking for the nesting type of the tuple using the instance() method. Then for each instance, we will be finding the maximum elements of the same index for both the tuples using max() and zip() putting them together using list comprehension.

# Python program to find the Maximum Nested Tuple

# Utility function to find maximum Nested Tuple 
def findMax(tup1, tup2):
    if isinstance(tup1, (list, tuple)) and isinstance(tup2, (list, tuple)):
        return tuple(findMax(x, y) for x, y in zip(tup1, tup2))
    return max(tup1, tup2)
    
# Initialize tuples
# Initializing and printing initial Tuples
tuple1 = ((1, 6), (2, 5), (8, 9))
tuple2 = ((3, 4), (5, 7), (9, 3))
print("The values of Tuple 1 are " + str(tuple1))
print("The values of Tuple 2 are " + str(tuple2))

# Finding maximum nested Tuples 
maxTuple = tuple(findMax(x, y) for x, y in zip(tuple1, tuple2))

# Printing the maximum nested Tuple 
print("The values of tuple after maximization : " + str(maxTuple))

Output:

The values of Tuple 1 are ((1, 6), (2, 5), (8, 9))
The values of Tuple 2 are ((3, 4), (5, 7), (9, 3))
The values of tuple after maximization : ((3, 6), (5, 7), (9, 9))

Python Tuple Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.