TIitle

Student(s) having the second lowest grade

Given the names and grades for each student in a class of 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

The ordered list of scores is , so the second lowest score is . There are two students with that score: . Ordered alphabetically, the names are printed as:

alpha
beta

Input Format

The first line contains an integer, , the number of students.
The subsequent lines describe each student over lines.
- The first line contains a student’s name.
- The second line contains their grade.

Constraints

  • 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.

Link

Nested Lists | HackerRank

Input/Output

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 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.

Ideals

方法1:

样例输入:

step1:将输入转化为一个学生列表

step2:找到最小的成绩

step3:找到第二小的成绩

step4:找到成绩等于第二小的学生,形成一个列表

step5:对列表排序

方法2:

和法一的思路一致,但是不需要用到类,也比较简单一些。

Code

代码1

class student():
    def __init__(self, name, score):
        self.name = name
        self.score = score


    def compare(self, s2):
        ''' case1: s1分数大于s2,返回2 case2: s1分数等于s2,但名字大于s2,返回0 case3: s1分数等于s2,但名字小于s2,返回-1 case4: s1分数小于s2,返回1 :param s2: :return: '''
        if self.score > s2.score:
            return 2
        elif self.score == s2.score:
            if self.name > s2.name:
                return 0
            elif self.name < s2.name:
                return -1
            else:
                return -2
        else:
            return 1


if __name__ == '__main__':
    studs = []
    # step1:将输入转成列表
    for _ in range(int(input())):
        name = input()
        score = float(input())
        s = student(name, score)
        studs.append(s)
    min_s = studs[0]
    # step2:找到min的学生
    for s in studs:
        if s.compare(min_s) == 1:
            min_s = s
    # step3:找到second_lowest的学生
    # 1、这里先给runner_up_S赋一个初值,目的是不要让他选到min
    runner_up_s = studs[0]
    for index, s in enumerate(studs):
        if s.compare(min_s) <= 0:
            continue
        else:
            runner_up_s = s
            break
    # 2、找到第二小的学生
    for index,s in enumerate(studs):
        if s.compare(runner_up_s) == 1 and s.compare(min_s) > 0:
            runner_up_s = s
    # step4:将成绩等于runner_up_s的学生姓名形成列表
    ans = []
    for s in studs:
        if s.compare(runner_up_s) <= 0:
            ans.append(s.name)
	# step5:列表排序,输出
    nameList = sorted(ans)
    for name in nameList:
        print(name)

代码2

if __name__ == '__main__':
    names = []
    scores = []
    # step1:将输入转化为list
    for _ in range(int(input())):
        name = input()
        score = float(input())
        names.append(name)
        scores.append(score)
    # step2:找到最小成绩
    min_score = min(scores)
    # step3:找到second_lowest
    second_score = max(scores)
    for s in scores:
        if s < second_score and s != min_score:
            second_score = s
    # step4:获得姓名的列表
    ans = []
    for i,s in enumerate(scores):
        if s == second_score:
            ans.append(names[i])
    # step5:nameList排序
    for name in sorted(ans):
        print(name)

Analyse

复杂度主要来自最后一步排序,即O(nlog n)