Saturday, November 16, 2024
Google search engine
HomeLanguagesPython Program to Find Largest Item in a Tuple

Python Program to Find Largest Item in a Tuple

Given a tuple, the task is to write a Python program to find the greatest number in a Tuple.

Example:

Input: (10,20,23,5,2,90) #tuple
Output: 90
Explanation: 90 is the largest element from tuple

Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.

Python program to demonstrate the maximum element in a Tuple.

Method 1: Using the max() method

We can use Python built-in max() Method to find the maximum element in a tuple.

Python3




print("Enter number separated by comma:")
t1 = tuple([int(e) for e in input().split(',')])
print("Greatest number in the tuple is:", max(t1))


Output:

Enter number separated by comma:
10,20,23,5,2,80
Greatest number in the tuple is: 80

Time complexity: O(n) where n is the number of elements in the tuple. This is because the max() function has a linear time complexity of O(n) and the tuple creation using a list comprehension also has a time complexity of O(n).
Auxiliary space: O(n), where n is the number of elements in the tuple. 

Method 2: Using for-loop

Here we iterate on the for-loop and compare each element to find the maximum element in the tuple.

Python3




t = (25, 17, 55, 63, 40)
 
max_val = t[0]
 
for i in range(len(t)):
    if t[i] > max_val:
        max_val = t[i]
 
print("Maximum value is:", max_val)


Output

Maximum value is: 63

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

Method 3: Using Recursive Functions

Here, we have used a recursive function to find the maximum element in the tuple.

Python3




def tupleLargest(num_tuple, index=0, max_item=float("-inf")):
    # setting base condition for recursion
    if index == len(num_tuple):
        return max_item
       
    # getting item at current index
    current_item = num_tuple[index]
     
    # update if new greater value is found
    if current_item > max_item:
        max_item = current_item
         
    # recursive call with incremented index value
    return tupleLargest(num_tuple, index + 1, max_item)
 
 
if __name__ == '__main__':
    maxTuple = (11, 65, 54, 23, 76, 33, 82, 98)
    print("Tuple Items = ", maxTuple)
 
    largest_element = tupleLargest(maxTuple)
    print("Maximum Item in Tuple = ", largest_element)


Output

Tuple Items =  (11, 65, 54, 23, 76, 33, 82, 98)
Maximum Item in Tuple =  98

Time Complexity: O(n)
Auxiliary Space: O(n)

One approach that is not mentioned in the given article is to use the Python reduce function from the functools module to find the maximum element in the tuple. The reduce function applies a given function to a sequence of elements, reducing the sequence to a single value.

Here is an example of how this could be implemented:

Python3




from functools import reduce
 
def find_max(t):
    return reduce(lambda x, y: x if x > y else y, t)
 
t = (25, 17, 55, 63, 40)
print(find_max(t))


Output

63

Time complexity: O(n), where n is the length of the tuple,
Auxiliary space: O(1) because it does not create any additional data structures.

Method 4: Using sort() method

Python3




t = (25, 17, 55, 63, 40)
 
x=list(t)
x.sort(reverse=True)
max_val=x[0]
 
print("Maximum value is:", max_val)


Output

Maximum value is: 63

Time complexity: O(nlogn), where n is the length of the tuple,
Auxiliary space: O(n) 

Method 5: Using enumerate() method

Python3




data=(10,21,3141,48,120,57#declaring tuple
max_value=data[0#assuming first value in tuple as maximum value
for i,j in enumerate(data):
  if j>max_value: #condition to find largest value
    max_value=#updating max_value
print("The largest value in tuple is :",max_value)


Output

The largest value in tuple is : 3141

Time complexity: O(n), where n is the length of the tuple,
Auxiliary space: O(1) 

Method 6: Using heapq module: 

Python3




import heapq
 
maxTuple = (11, 65, 54, 23, 76, 33, 82, 98)
largest_element = heapq.nlargest(1, maxTuple)[0]
 
print("Tuple Items: ", maxTuple)
print("Maximum Item in Tuple: ", largest_element)
#This code is contributed by Jyothi pinjala


Output

Tuple Items:  (11, 65, 54, 23, 76, 33, 82, 98)
Maximum Item in Tuple:  98

Time complexity: O(nlogk), where n is the size of the input tuple and k is the number of largest elements to be found (in this case, k=1). The nlargest function uses a heap to efficiently find the largest element, which takes O(logk) time for each element in the input tuple. Therefore, the total time complexity is O(nlogk).
Auxiliary Space: O(k), where k is the number of largest elements to be found. In this case, we only need to find the largest element (k=1), so the space complexity is O(1).

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments