GeorgeDI

GDii

Jan 14th, 2023
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #1T GDI
  2. def sum_tuples(first_tuple, second_tuple):
  3. """
  4. Returns a new tuple with the elements of the input tuples added together element-wise.
  5.  
  6. Parameters:
  7. first_tuple (tuple): The first tuple to be added.
  8. second_tuple (tuple): The second tuple to be added.
  9.  
  10. Returns:
  11. tuple: A new tuple with the elements of the input tuples added together element-wise.
  12.  
  13. >>> result = sum_tuples((1, 2, 3), (4, 5, 6))
  14. (5, 7, 9)
  15.  
  16. >>> result = sum_tuples((1, 2, 3), (4, 5))
  17. (5, 7, 3)
  18. """
  19. if len(first_tuple) != len(second_tuple):
  20. raise ValueError("Both tuples should have the same number of elements.")
  21. return tuple(x + y for x, y in zip(first_tuple, second_tuple))
  22.  
  23.  
  24. #T GDI
  25. def sum_tuple_with(the_tuple, number):
  26. """
  27. Returns a new tuple with the elements of the input tuple added to the given number.
  28.  
  29. Parameters:
  30. the_tuple (tuple): The tuple to be added.
  31. number (int or float): The number to add to each element of the tuple.
  32.  
  33. Returns:
  34. tuple: A new tuple with the elements of the input tuple added to the given number.
  35.  
  36. >>> result = sum_tuple_with((1, 2, 3), 2)
  37. (3, 4, 5)
  38.  
  39. >>> result = sum_tuple_with((1, 2, 3), -2)
  40. (-1, 0, 1)
  41. """
  42. new_tuple = []
  43. for x in the_tuple:
  44. new_tuple.append(x + number)
  45. return tuple(new_tuple)
  46.  
  47.  
  48. #T GDI
  49.  
  50. # This function takes two tuples as input and use in operator to check if the sub_tuple is present in the tuple or not, if it is present then it returns True otherwise False.
  51.  
  52. def contains_subtuple(sub_tuple, the_tuple):
  53. """
  54. Returns True if the input tuple contains the input sub-tuple, False otherwise.
  55.  
  56. Parameters:
  57. sub_tuple (tuple): The sub-tuple to check for in the input tuple.
  58. the_tuple (tuple): The tuple to check for the input sub-tuple.
  59.  
  60. Returns:
  61. bool: True if the input tuple contains the input sub-tuple, False otherwise.
  62.  
  63. >>> is_subtuple = contains_subtuple((2,3), (1,2,3))
  64. is_subtuple: True
  65.  
  66. >>> is_subtuple = contains_subtuple((1,3), (1,2,3))
  67. is_subtuple: False
  68. """
  69. return sub_tuple in the_tuple
  70.  
  71.  
  72. #T GDI
  73.  
  74. def delete_subtuple(sub_tuple, the_tuple):
  75. """
  76. Returns a new tuple with the input sub-tuple removed from the input tuple.
  77.  
  78. Parameters:
  79. sub_tuple (tuple): The sub-tuple to remove from the input tuple.
  80. the_tuple (tuple): The tuple to remove the input sub-tuple from.
  81.  
  82. Returns:
  83. tuple: A new tuple with the input sub-tuple removed.
  84.  
  85. >>> new_tuple = delete_subtuple((2,3), (1,2,3,4))
  86. (1,4)
  87.  
  88. >>> new_tuple = delete_subtuple((1,3), (1,2,3,4))
  89. (1,2,3,4)
  90. """
  91. return tuple(x for x in the_tuple if x != sub_tuple)
  92.  
Advertisement
Add Comment
Please, Sign In to add comment