python courses − Python exercises and packages

Exercise 205

Given the following set of numerical tuples:

E = {(12, 3 , 7 , 11) , (2, 9 , 5 , 13) , (1, 4 ,8)}

Write a Python program that creates a set F whose elements are (Tuple , average of Tuple). Exemple for the set E cited above, the output must be:

F =  {((2, 9, 5, 13), 7.25), ((12, 3, 7, 11), 8.25), ((1, 4, 8), 4.333333333333333)}

Solution

E = {(12, 3 , 7 , 11) , (2, 9 , 5 , 13) , (1, 4 ,8)}
# creating a function that determines the average of given tuple
def averageTuple(T):
n = len(T)
Sum = 0
for i in range(0 , n):
Sum = Sum + T[i]
average = Sum/n
return average

# initializing the set F
F = set({})
for Tuple in E:
F.add((Tuple , averageTuple(Tuple)))
# Testing algorithm
print("The searched set is F = " , F)

Younes Derfoufi
my-courses.net

Leave a Reply