Example:
tuple = ("python", "includehelp", 43, 54.23)
Finding maximum and minimum k elements in a tuple
We have a tuple and value k. Then we will return k maximum and k minimum elements from the tuple.
Example:
Input:
myTuple = (4, 2, 5,7, 1, 8, 9), k = 2
Output:
(9, 8) , (1, 2)
A simple method to solve the problem is by sorting the tuple and then finding k maximum and k minimum values from the tuple by extracting k from start and k from end.
Program to find maximum and minimum k elements in a tuple in Python
Output:
Alternate method
We can use slicing methods on the sorted list created from the tuple to extract first k and last k values.
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer