Q. Write a function having tuple as an argument and returns True if the elements of the first half of the tuple are sorted in ascending order otherwise False.
Write a complete program that inputs elements of a tuple and invokes the function defined above.
(Enter even number of elements in the tuple)
Example 1 : tuple with even elements as shown below
(1, 3, 5, 14, 7, 34)
Output :- First half of the tuple elements are upto index 2
First half elements of tuple are sorted
Example 2 : tuple with even elements as shown below
(51, 3, 5, 21, 7, 34, 291, -78)
Output :- First half of the tuple elements are upto index 3
First half elements of tuple are not sorted
Answer :-
def Path( tup ): n = len( tup)//2 sort = True for i in range (n-1): if tup[i] > tup [i+1]: sort = False return sort tup = eval(input("Enter tuple of even elements :- ")) print( Path(tup) )
Output :-
Enter tuple of even elements :- (1, 3, 5, 14, 7, 34)
True
>>>
True
>>>
Enter tuple of even elements :- (51, 3, 5, 21, 7, 34, 291, -78)
False
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )