Write a python program to read n values from user into a tuple. Using the values of tuple, display and count inversion. Count and Display the leader(s) from the tuple elements.
Inversion Count indicates – how far (or close) the tuple elements are far from being sorted in ascending order. If tuple is already sorted then inversion count is 0. If tuple is sorted in reverse order that inversion count is the maximum.
An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader.
Input
Number of Elements in the Tuple
5
Enter the Elements of the Tuple
2 4 5 1 3
Output
Count Inversion (5) - [(2, 1), (4, 1), (4, 3), (5, 1), (5, 3)]
Leader (2) – [5, 3]
n = int(input('Enter number here: '))
list1 = []
list2 = []
for i in range(n):
l = int(input('Enter nos here: '))
list1.append(l)
tuple1 = tuple(list1)
for i in tuple1:
for j in tuple1[tuple1.index(i):]:
if i > j:
list2.append((i,j))
list2
Enter number here: 5
Enter nos here: 2
Enter nos here: 4
Enter nos here: 5
Enter nos here: 1
Enter nos here: 3
[(2, 1), (4, 1), (4, 3), (5, 1), (5, 3)]
Comments
Leave a comment