write a method called solve which takes as parameter a list of tuples called A.
You have to print that pair of tuple which have the same sum. There is only 1 such pair.
Example:
[(1,2), (1,0), (4,4), (10,2), (5,-1), (1,1,1)]
For this list, the pair is:
1,2
1,1,1
Because both these tuples sum up to 3.
# Python code to get sum of tuples having same first value
# Initialisation of list of tuple
Input = [(1, 13), (1, 190), (3, 25), (1, 12)]
d = {x:0 for x, _ in Input}
for name, num in Input: d[name] += num
# using map
Output = list(map(tuple, d.items()))
# printing output
print(Output)
Comments
Leave a comment