Write a Python program that takes a tuple of tuples as an input from the user. Then calculates the average value of the numbers for each tuple of tuples and find the tuple whose sum is the maximum. [You are not allowed to use max () function here. You are not allowed to use Regex split in this task.] Hints: Since the input function converts everything to string by default. You might need to use strip() and split() to get the data/tuples. =========================================================== Sample Input1: ((33, 22, 11), (30, 45, 56, 45,20), (81, 90, 39, 45), (1, 2, 3, 4,5,6)) Sample Output2: Average of tuples: [22.0, 39.2, 63.75, 3.5] Tuple with maximum sum is (81, 90, 39, 45)
inp = eval(input())
max_tuple = tuple()
s , max_s = 0, 0
res_lst = []
for el in inp:
if len(max_tuple) == 0:
max_tuple = el
max_s = len(el)
for num in el:
s += num
res_lst.append(s/len(el))
if s > max_s:
max_tuple = el
max_s = s
s = 0
print('Average of tuples: ' + str(res_lst))
print('Tuple with maximum sum is ' +str(max_tuple))
Comments
Leave a comment