Posts

Showing posts with the label python list

Python program to find Nth largest element in a list

Image
Python program to find Nth largest element in a list Problem description : The program gets a list of numbers and n as input and returns nth greatest element in the list. For example,  Consider the list  L1 = [5, 6, 1 3, 8, 4, 9] and  N=3, so the result will be the third   greatest element = 6. Consider another list L2 = [0, 3, 1, 7, 5, 8, 4, 9] and  N = 2, here the output will be the second  greatest element  = 8. Procedure : 1. Start. 2.  Get the elements of the list separated by space as input from user. 3.  Get n as input from the user. 4.  Sort the list. 5.  Nth greatest element =  len(l)-n th element in the sorted list. 6.  Print the nth greatest element. 7. End. Code : #get the elements of the list separated by space l = list(map(int,input("Enter the elements of the list separated by space : ").split())) #get the number, n from user n = int(input("Enter n : ")) #sort the list l.sort() #find the nth greatest element in the list max_elt