Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1T GDI
- def sum_tuples(first_tuple, second_tuple):
- """
- Returns a new tuple with the elements of the input tuples added together element-wise.
- Parameters:
- first_tuple (tuple): The first tuple to be added.
- second_tuple (tuple): The second tuple to be added.
- Returns:
- tuple: A new tuple with the elements of the input tuples added together element-wise.
- >>> result = sum_tuples((1, 2, 3), (4, 5, 6))
- (5, 7, 9)
- >>> result = sum_tuples((1, 2, 3), (4, 5))
- (5, 7, 3)
- """
- if len(first_tuple) != len(second_tuple):
- raise ValueError("Both tuples should have the same number of elements.")
- return tuple(x + y for x, y in zip(first_tuple, second_tuple))
- #T GDI
- def sum_tuple_with(the_tuple, number):
- """
- Returns a new tuple with the elements of the input tuple added to the given number.
- Parameters:
- the_tuple (tuple): The tuple to be added.
- number (int or float): The number to add to each element of the tuple.
- Returns:
- tuple: A new tuple with the elements of the input tuple added to the given number.
- >>> result = sum_tuple_with((1, 2, 3), 2)
- (3, 4, 5)
- >>> result = sum_tuple_with((1, 2, 3), -2)
- (-1, 0, 1)
- """
- new_tuple = []
- for x in the_tuple:
- new_tuple.append(x + number)
- return tuple(new_tuple)
- #T GDI
- # 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.
- def contains_subtuple(sub_tuple, the_tuple):
- """
- Returns True if the input tuple contains the input sub-tuple, False otherwise.
- Parameters:
- sub_tuple (tuple): The sub-tuple to check for in the input tuple.
- the_tuple (tuple): The tuple to check for the input sub-tuple.
- Returns:
- bool: True if the input tuple contains the input sub-tuple, False otherwise.
- >>> is_subtuple = contains_subtuple((2,3), (1,2,3))
- is_subtuple: True
- >>> is_subtuple = contains_subtuple((1,3), (1,2,3))
- is_subtuple: False
- """
- return sub_tuple in the_tuple
- #T GDI
- def delete_subtuple(sub_tuple, the_tuple):
- """
- Returns a new tuple with the input sub-tuple removed from the input tuple.
- Parameters:
- sub_tuple (tuple): The sub-tuple to remove from the input tuple.
- the_tuple (tuple): The tuple to remove the input sub-tuple from.
- Returns:
- tuple: A new tuple with the input sub-tuple removed.
- >>> new_tuple = delete_subtuple((2,3), (1,2,3,4))
- (1,4)
- >>> new_tuple = delete_subtuple((1,3), (1,2,3,4))
- (1,2,3,4)
- """
- return tuple(x for x in the_tuple if x != sub_tuple)
Advertisement
Add Comment
Please, Sign In to add comment