In previous articles, we learned about various functions and operations that we can perform on Tuples in Python. In this article we will work on practice programs related to Python Tuples.
Program – Problem Statement
We will be given a nested tuple whose elements will also be tuple. Each tuple element can have multiple numbers in it. For each tuple element, we need to add minimum number from that tuple to each element of that tuple except the maximum number in that tuple. Output should be a nested tuple (similar to given in input) but with its updated elements.
Tuple element can have any number of elements.
Test Case 1
Input: ((10, 20), (30, 40), (50, 60))
Output: ((20, 20), (60, 40), (100, 60))
Test case explanation:
Input is given as nested tuple (tuple of tuples). For each tuple elements e.g. (10, 20) first tuple element, we need add the minimum number from that tuple to to each element of that tuple except the maximum number in that tuple. Minimum number from first tuple (10, 20) is 10 and maximum number from this tuple is 20. Now 10 will be added to each elements in that tuple except 20. Final tuple will become (20, 20).
This process will be followed to all tuple elements. e.g. second tuple element is (30, 40). Minimum element is 30 and maximum element is 40. 30 will be added to 30 but not to 40. Final tuple will become (60, 40).
Similar steps will be followed for the third tuple element and so on.
Program – Solution
Solution Program Steps
- Take a variable ‘input_tuple’ to reference the given input nested tuple.
- Iterate through the elements of input_tuple. We can use for loop or while loop to iterate through the elements.
- For each tuple element, find the minimum and maximum number from that tuple. We can first sort the tuple using built-in ‘sorted’ function and then get the first element (index 0) as minimum and last element (index -1) as maximum. OR we can find the minimum and maximum number by iterating through the tuple elements. OR we can use built-in functions max(tuple) to get maximum number and min(tuple) to get minimum number.
- Define a empty list to store the updated elements
- Iterate through the tuple elements. Check if the element is maximum number or not. If element is not maximum number then add minimum number in that element. Append the updated element in the list.
- As an output, create tuple from the list elements ans print that tuple.
Solution Code 1 – Using built-in ‘sorted’ function
input_tuple = ((30, 15, 8), (86, 39, 20), (312, 45, 25))
output_list = []
for n_element in input_tuple:
sorted_tuple = sorted(n_element) # using built-in sorted function
minimum_value = sorted_tuple[0]
maximum_value = sorted_tuple[-1]
n_list = []
for x in n_element:
if x != maximum_value:
x += minimum_value
n_list.append(x)
output_list.append(tuple(n_list))
print(tuple(output_list))
Test cases
Test Case 1
Input: input_tuple = ((10, 20), (30, 40), (50, 60))
Output: ((20, 20), (60, 40), (100, 60))
Test Case 2
Input: input_tuple = ((3, 5, 8), (6, 9, 10), (12, 14, 8))
Output: ((6, 8, 8), (12, 15, 10), (20, 14, 16))
Test Case 3
Input: input_tuple = ((30, 15, 8), (86, 39, 20), (312, 45, 25))
Output: ((30, 23, 16), (86, 59, 40), (312, 70, 50))
Solution Code 2 – Using built-in ‘max’ and ‘min’ function
input_tuple = ((30, 15, 8), (86, 39, 20), (312, 45, 25))
output_list = []
for n_element in input_tuple:
minimum_value = min(n_element) # using built-in min function
maximum_value = max(n_element) # using built-in max function
n_list = []
for x in n_element:
if x != maximum_value:
x += minimum_value
n_list.append(x)
output_list.append(tuple(n_list))
print(tuple(output_list))
Solution Code 3 – Using for loop for minimum and maximum
import sys
input_tuple = ((30, 15, 8), (86, 39, 20), (312, 45, 25))
output_list = []
for n_element in input_tuple:
minimum_value = sys.maxsize # initially define minimum as sys maximum
maximum_value = -sys.maxsize - 1 # initially define maximum as negative sys max
for x in n_element:
if x < minimum_value:
minimum_value = x
if x > maximum_value:
maximum_value = x
n_list = []
for x in n_element:
if x != maximum_value:
x += minimum_value
n_list.append(x)
output_list.append(tuple(n_list))
print(tuple(output_list))
Summary
In this article, we write a Python program for a problem statement related to Python tuples and following points were covered.
Code – Github Repository
All code snippets and programs for this article and for Python tutorial, can be accessed from Github repository – Comments and Docstring in Python.