Nested Lists HackerRank python Solution pypy2 + pypy3

Given the names and grades for each student in a class of N students, store them in a nested list and print the name(s) of any student(s) having the s
Admin
Nested Lists HackerRank python Solution

Given the names and grades for each student in a class of N students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.

Note: If there are multiple students with the second lowest grade, order their names alphabetically and print each name on a new line.

Example

records = [["chi",20.0],["beta",50.0],["alpha",50.0]]

The ordered list of scores is [20.0,50.0], so the second lowest score is 50.0. There are two students with that score:["beta","alpha"] . Ordered alphabetically, the names are printed as:

alpha
beta

Input Format

The first line contains an integer,N, the number of students.

The 2N subsequent lines describe each student over 2 lines.

- The first line contains a student's name.

- The second line contains their grade.

Constraints

2 ≤ N ≤ 5

There will always be one or more students having the second lowest grade.

Output Format

Print the name(s) of any student(s) having the second lowest grade in. If there are multiple students, order their names alphabetically and print each one on a new line.

Sample Input 0

5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39

Sample Output 0

Berry
Harry

Explanation 0

There are 5 students in this class whose names and grades are assembled to build the following list:

python students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]

The lowest grade of 37.2 belongs to Tina. The second lowest grade of 37.21 belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.

Solution pypy2

score_list = [];
for _ in range(int(raw_input())):
    name = raw_input()
    score = float(raw_input())
    score_list.append([name, score])
second_highest = sorted(set([score for name, score in score_list]))[1]
print('\n'.join(sorted([name for name, score in score_list if score == second_highest])))

Solution pypy3

marksheet = []
n = int(input())
marksheet = [[input(), float(input())] for _ in range(n)]

second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.