Showing posts with label create a function squareT that accepts one argument and returns a tuple containing similar elements given in tuple T and its sqaures in sequence.. Show all posts
Showing posts with label create a function squareT that accepts one argument and returns a tuple containing similar elements given in tuple T and its sqaures in sequence.. Show all posts
Saturday, March 11, 2023

Given a Tuple T, create a function squareT that accepts one argument and returns a tuple containing similar elements given in tuple T and its sqaures in sequence.





Given a Tuple T, create a function squareT that accepts one argument and returns a tuple containing similar elements given in tuple T and its sqaures in sequence.

Input:

(1,2,3)

Output :

(1,2,3,1,4,9)

Explanation:

Tuple T contains (1,2,3) the output tuple contains the original elements of T that is 1,2,3 and its sqaures 1,4,9 in sequence.


Here's a Python function squareT that accepts a tuple as its argument and returns a new tuple containing the original elements of the tuple followed by their squares:

def squareT(T): squared = tuple(x**2 for x in T) return T + squared

if __name__ == "__main__": n = int(input()) L = list(map(int, input().split())) T = tuple(L) ans = squareT(T) if type(ans) == type(T): print(ans)

For example, if we have a tuple T = (1, 2, 3), calling squareT(T) will return a new tuple (1, 2, 3, 1, 4, 9), which contains the original elements of T followed by their squares.

Here's a breakdown of how the function works:

1.We create a new tuple squared by iterating over the elements of T and squaring each one using a list comprehension. We then convert this list into a tuple using the tuple() function.

2.We concatenate the original tuple T and the squared tuple using the + operator, which creates a new tuple that contains all the elements from both tuples.

This function should work for any tuple that contains elements that can be squared.