GeorgeDI

sum_tuples

Jan 12th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. def sum_tuples(first_tuple, second_tuple):
  2. """
  3. Function that takes in two tuples as arguments, and returns a new tuple that is the result of the summing of the two input tuples.
  4. If the tuples have different length, it will sum the values with the same index and fill the rest with zeroes.
  5.  
  6. Args:
  7. first_tuple (tuple): The first tuple to be summed
  8. second_tuple (tuple): The second tuple to be summed
  9.  
  10. Returns:
  11. tuple: A new tuple that is the result of the summing of the two input tuples
  12.  
  13. Example:
  14. result = sum_tuples((1, 2, 3), (4, 5, 6))
  15. # result: (5, 7, 9)
  16. result = sum_tuples((1, 2, 3), (4, 5))
  17. # result: (5, 7, 3)
  18.  
  19. """
  20. # Create a new empty list to store the sum of the tuples
  21. summed_list = []
  22. # Iterate through the elements of the first tuple
  23. for i in range(max(len(first_tuple), len(second_tuple))):
  24. try:
  25. # Sum the elements of the tuples with the same index
  26. summed_list.
Advertisement
Add Comment
Please, Sign In to add comment