PYTHON TUPLE
1 Problem Statement
Jai wants to write a program that takes a tuple of elements as input, along with an index and a new value. The
program should replace the element at the specified index with the new value and print the updated tuple.
Input Format
The first line of input consists of the integers, representing the elements of the tuple separated by
spaces.
The second line of input consists of an integer, representing the index where the element should be
updated.
The third line of input consists of an integer, representing the value to be updated in the given index.
Output Format
The output will be a tuple with the updated value at the specified index, and the rest of the values will
remain unchanged.
The elements in the tuple are enclosed in parentheses, and they are separated by commas.
Each element in the tuple is enclosed in single quotes.
elements = tuple(input().split())
index = int(input())
new = input()
list1 = list(elements)
list1[index] = new
updated = tuple(list1)
print(updated)
2 Problem Statement
Mike, an aspiring coder, is tackling a programming assignment. His challenge involves receiving a tuple of
numbers and determining its length.
Input Format
The input consists of a series of integers separated by commas, representing the tuple of numbers.
Output Format
The output prints an integer representing the length of the tuple.
Take input and convert it into a tuple
numbers_input = input()
numbers_tuple = tuple(numbers_input.split(',')) # Convert to tuple
print(len(numbers_tuple))
3 Problem Statement
Tom has a tuple of colors, and he needs a program to check if a specific color exists in the tuple. Write a Python
program that takes a tuple of colors as input, and then determines if the entered color is present in the tuple.
Input Format
The first line of input consists of a comma-separated string representing a tuple of colors.
The second line of input consists of a single string representing the color to be checked.
Output Format
The output consists of any one of the following:
If the entered color is found in the tuple, then print "The color '<color>' is in the tuple."
PYTHON TUPLE
If the entered color is not found in the tuple, then print "The color '<color>' is not in the tuple."
tuple_input = input()
colors = tuple(tuple_input.split(','))
element_to_check = input()
element_found = False
for color in colors:
if color == element_to_check:
element_found = True
break
if element_found:
print(f"The color '{element_to_check}' is in the tuple.")
else:
print(f"The color '{element_to_check}' is not in the tuple.")
4 Problem Statement
At a warehouse, two delivery trucks bring boxes of goods every day. Each truck reports the number of boxes
delivered to each section of the warehouse.
Write a program that takes two lists of integers as input, representing the number of boxes delivered to each
section by each truck, computes the element-wise sum, and outputs a tuple containing the total number of boxes
in each section.
Input Format
The first line contains a single integer n, representing the number of sections in the warehouse.
The second line contains n integers separated by commas, representing the boxes delivered by the first
truck.
The third line contains n integers separated by commas, representing the boxes delivered by the second
truck.
Output Format
The output is a single line containing a tuple of integers, representing the total boxes in each
section after combining deliveries.
METHOD 1
n = int(input())
lst1 = list(map(int, input().split(',')))
lst2 = list(map(int, input().split(',')))
res = tuple(a + b for a, b in zip(lst1, lst2))
print(res)
METHOD 2
n = int(input())
lst1 = tuple(map(int, input().split(',')))
lst2 = tuple(map(int, input().split(',')))
PYTHON TUPLE
res = ([],)
for a, b in zip(lst1, lst2):
res[0].append(a + b)
final_result = tuple(res[0])
# Print the result
print(final_result)
5 Problem Statement
Teju is exploring the fascinating world of tuples and their manipulation. She is given a task to transform a list of
integers into a tuple and concatenate its elements into a single string.
Example
Input
3 // number of elements in the tuple
1
20
3
Output
(1, 20, 3)
1203
Input Format
The first line of input consists of a single integer n, the number of elements in the tuple.
The next n lines consist of an integer representing the elements in the tuple.
Output Format
The first line of output consists of a tuple containing the elements of the input list in the format:
(element1, element2, ...)
The second line of output consists of a single string that represents the concatenation of all elements of
the tuple.
n = int(input())
l1 = []
for i in range(n):
v = int(input())
[Link](v)
s = ''
for item in l1:
s = s + str(item)
print(tuple(l1))
print(s)
6 Problem Statement
A meteorologist is analyzing temperature readings recorded throughout a week. She wants to determine the
highest temperature recorded in the week to prepare a weather report.
Write a Python program that takes the temperature readings as a tuple of integers, iterates through the values,
and outputs the maximum temperature.
Input Format
The input consists of the tuple of integers representing daily temperatures as space-separated values.
Output Format
The output should display a single integer, representing the maximum temperature recorded.
input_tuple = tuple(int(x) for x in input().split())
PYTHON TUPLE
max_value = input_tuple[0]
for num in input_tuple:
if num > max_value:
max_value = num
print(max_value)
7 Problem Statement
George is analyzing two data streams representing IDs from different systems. He needs to identify the IDs that
are common between the two systems, but only if they appear an odd number of times in the combined list of
IDs.
Additionally, George wants to ensure that the IDs in the final list are unique and sorted in ascending order.
Input Format
The first line of input consists of an integer n, representing the size of the first tuple.
The second line of input consists of n space-separated integers, representing the elements of the first
tuple.
The third line of input consists of an integer m, representing the size of the second tuple.
The fourth line of input consists of m space-separated integers, representing the elements of the second
tuple.
Output Format
The output displays the unique, common IDs that appear an odd number of times in the combined list
of both tuples, sorted in ascending order.
If there are common IDs, the result will be displayed as a tuple with elements enclosed in parentheses
() and separated by commas.
If no such IDs are found, print "No common elements found."
size1 = int(input())
elements1 = input().split()
tuple1 = tuple(map(int, elements1))
# Input the size and elements of the second tuple
size2 = int(input())
elements2 = input().split()
tuple2 = tuple(map(int, elements2))
# Combine both tuples
combined = tuple1 + tuple2
# Initialize an empty list for the intersection result
result = []
# Loop through the first tuple
for item in set(tuple1):
# Check if the item is in the second tuple and appears an odd number of times
in combined list
if item in tuple2 and [Link](item) % 2 != 0:
[Link](item)
# Sort the result list and convert to a tuple
result = tuple(sorted(result))
# Print the result tuple or a message if no common elements
if result:
print(result)
else:
print("No common elements found.")
8 Problem Statement
PYTHON TUPLE
Charlie needs to analyze rainfall data for multiple years to determine the average monthly rainfall for a specific
year. He has a tuple where each element is another tuple representing the monthly rainfall data for a year.
Input Format
The first line consists of an integer n, representing the number of years.
The next n lines, where each line contains 12 space-separated integers. Each line represents the
monthly rainfall data for one year.
The third line consists of an integer year, representing the year number (1-based index) used to
calculate the average monthly rainfall.
Output Format
The output displays a float value representing the average monthly rainfall for the specified year,
rounded to two decimal points.
n = int(input())
rainfall_data = ()
for _ in range(n):
monthly_rainfall = tuple(map(int, input().split()))
rainfall_data += (monthly_rainfall,)
year = int(input())
year_data = rainfall_data[year - 1]
total_rainfall = 0
for rainfall in year_data:
total_rainfall += rainfall
average_rainfall = total_rainfall / 12
print(f"{average_rainfall:.2f}")
9 Problem Statement
Daniel is organizing a treasure hunt where each participant's clues are represented as a tuple of integers. Each
integer represents the points earned for solving a clue.
To determine who performed best, Daniel needs to calculate the cumulative product of the points for each
participant. Since tuples are immutable, he will use a list to compute and store the intermediate results before
converting it back to a tuple.
Input Format
The first line of input contains an integer n, representing the size of the tuple.
The second line contains n space-separated integers, representing the points gained from solving each
clue.
Output Format
The output format is:(element1, element2, element3, ...)
Each cumulative product is printed in a tuple format, with the values separated by commas
size = int(input().strip())
elements = tuple(map(int, input().strip().split()))
cumulative_product_list = []
product = 1
for num in elements:
product *= num
cumulative_product_list.append(product)
result = tuple(cumulative_product_list)
print(result)
PYTHON TUPLE
10 Problem Statement
James is an engineer working on designing a new rocket propulsion system. He needs to solve a quadratic
equation to determine the optimal launch trajectory. The equation is of the form ax 2 +bx+c=0.
Your task is to help James find the roots of this quadratic equation. Depending on the discriminant, the roots
might be real and distinct, real and equal, or complex. Implement a program to determine and display the roots
of the equation based on the given coefficients.
The roots of the quadratic equation are determined using the quadratic formula:
If the discriminant (b2 − 4ac) is positive, the equation has two distinct real roots.
If the discriminant is zero, the equation has one repeated real root.
If the discriminant is negative, the equation has two complex roots, represented as a tuple with real and
imaginary parts.
Input Format
The first line of the input consists of an integer N, representing the number of coefficients.
The second line contains three space-separated integers a, b, and c representing the coefficients of the
quadratic equation.
Output Format
The output displays:
1. If the discriminant is positive, display the two real roots.
2. If the discriminant is zero, display the repeated real root.
3. If the discriminant is negative, display the complex roots as a tuple with real and imaginary parts.
tuple_size = int(input().strip())
coefficients = tuple(map(int, input().split()))
a, b, c = coefficients
discriminant = b * b - 4 * a * c
if discriminant > 0:
sqrt_discriminant = discriminant ** 0.5
root1 = (-b + sqrt_discriminant) / (2 * a)
root2 = (-b - sqrt_discriminant) / (2 * a)
roots = (root1, root2)
elif discriminant == 0:
root = -b / (2 * a)
roots = (root, root)
else:
temp = -discriminant
sqrt_temp = temp ** 0.5
realPart = -b / (2 * a)
imaginaryPart = sqrt_temp / (2 * a)
roots = ((realPart, imaginaryPart), (realPart, -imaginaryPart))
print(roots)