Answer to Question #285526 in Python for Niha

Question #285526

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.

1
Expert's answer
2022-01-08T01:34:43-0500
# 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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS