Skip to main content

Posts

Showing posts from December, 2015

Find second largest number in a list

Problem - https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list #Python Program to find Second Largest number in List with Sort #/usr/bin/python3 n = int(input()) if ( n >= 2 and n <= 10): lst = set(list(map(int,input().split()))) #Set's will avoid duplicated new_lst = list(lst) new_lst.sort() print(new_lst[-2]) #  Python Program to find Second Largest number in List Without Sort #/usr/bin/python3 n = int(input()) a = list(map(int,input().split())) f_num = a[0] s_num = 0; for i in range(1,n):  if(f_num > a[i] and a[i] > s_num) :   s_num = a[i]  if(a[i] > f_num):   s_num = f_num;   f_num = a[i] print("Second Large number is :-",s_num) Input - 5 5 2 10 1 2 Output - Second Large number is :- 5

Nested Lists in Python - List Comprehension

Problem -  https://www.hackerrank.com/challenges/nested-list  - Store stundent information using nested list and retrieve the information , use list comprehension #!/usr/bin/python3 #Read input and append the list to main records list using List Compression records = [[input(),float(input())] for i in range(int(input()))] #sort the list using list compression set will avoid duplicates mk_lst  = sorted(set(x[1] for x in records)) #loop through records and if marks matches with second highest sort and display  the names for name in sorted(x[0] for x in records if x[1] == mk_lst[1]):         print(name) Input   -  5 Harry 37.21 Berry 37.21 Tina 37.2 Akriti 41 Harsh 39 Output - Berry Harry