Python program for Maximum and Minimum K elements in Tuple
In this tutorial, you will learn to write a python program that will display K maximum and minimum elements from a tuple. A tuple is one of the data structures in Python which are used to store multiple items in a single variable.
We will give a tuple and a value of K as input in our program and then we should get the K maximum and minimum elements as output.
Look at the examples to understand the input and output format.
Input:
tup=(1, 2, 3, 4, 5, 6)
k=1
Output: (1,6)
Input:
tup=(3, 4, 5, 6,10)
k=2
Output: (3, 4, 6, 10)
To solve this problem in python, we can use the following approaches-
-
using a loop and sorted() method
-
using slicing and sorted() method
We will be discussing both these approaches in detail below.
Approach 1: loop and sorted()
In this approach, we will use a loop and the sorted()
method to get all the K maximum and minimum elements from the given tuple.
The sorted()
method is a built-in method of Python that sorts the given data structure in ascending order and returns a sorted list. To get the maximum and minimum elements we will use a loop.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function to find elements from a tuple
Step 2- Declare a result list
Step 3- Convert tuple to list
Step 4- Sort the list using sorted() and store it in a variable
Step 5- Iterate through the sorted tuple and search for maximum and minimum elements
Step 6- Add the elements in the list
Step 7- Print list as the final result
Step 8- Declare a tuple and K then pass in the function to get output
Python Program 1
In this program, we have defined a function that accepts a tuple and value of K as parameters and finds the K maximum and minimum elements from it. To convert a tuple to a list we will use the list()
method. To get a counter value for a tuple we can get the enumerate()
method. We have also used string methods like append()
and len()
in our program to add elements to a list and to get the length of a list respectively.
def Findel(tup,K):
result = []
test_tup = list(tup)
temp = sorted(tup)
for i, val in enumerate(temp):
if i < K or i >= len(temp) - K:
result.append(val)
result = tuple(result)
# printing result
print("Max and Min K elements : ",result)
tup = (13, 10, 23, 2, 5, 6, 12)
K = 2
print("The original tuple: ", tup)
Findel(tup,K)
The original tuple: (13, 10, 23, 2, 5, 6, 12)
Max and Min K elements : (2, 5, 13, 23)
Approach 2: slicing and sorted()
In this approach, we will use the concept of list slicing and the sorted()
method to find elements from the tuple. We will use slicing instead of a loop to get the max and min elements in this. Slicing a list simply means creating another list which will be the subset of the original list, where the starting and the ending position is specified.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function to find elements from a tuple
Step 2- Convert tuple to a list and store it in a variable
Step 3- Sort the elements in the list
Step 4- Declare a result list and with the help of slicing store only the elements which are needed
Step 5- Print this list as the final result
Step 6- Declare a tuple and K then pass in the function to get output
Python Program 2
In this program, we have defined a function that finds the required elements from the tuple using some methods and list slicing. The list()
method will be used to convert tuple to a list. After sorting, the K max and min elements will be at the starting and end of the list.
def Findel(tup,K):
tup = list(tup)
temp = sorted(tup)
result = tuple(temp[:K] + temp[-K:])
print("Max and Min K elements : ",result)
tup = (13, 10, 23, 2, 5, 6, 12, 7, 1, 8)
K = 3
print("The original tuple: ", tup)
Findel(tup,K)
The original tuple: (13, 10, 23, 2, 5, 6, 12, 7, 1, 8)
Max and Min K elements : (1, 2, 5, 12, 13, 23)
Conclusion
In this tutorial, we have seen two different approaches of extracting K maximum and minimum elements from a tuple in Python. The values in the tuple and the value of K is already specified in the program.