HackerRank-Python

Prepare > Python >Basic Data Types > Nested Lists

stem_sw 2023. 6. 24. 16:29
 

Nested Lists | HackerRank

In a classroom of N students, find the student with the second lowest grade.

www.hackerrank.com

 

문제


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.

 

=> 학생들의 이름과 점수가 제공될 것인데, nested list로 저장하고, 두 번째로 낮은 점수를 받은 학생(들)의 이름(들)을 

알파벳순 정렬, 줄바꿈해 출력하라.

 

 

 

코드


if __name__ == '__main__':

    students = []
    scores = []
    for _ in range(int(input())):
        name = input()
        score = float(input())
        
        
        student = [name, score]
        students.append(student)
        scores.append(score)
    second_low_score = sorted(set(scores))[1]
    
    target_name_score = []
    for i in students:
        if i[1] == second_low_score:
            target_name_score.append(i)
    
    for j in range(len(target_name_score)):
        print(sorted(target_name_score)[j][0])

 

 

 

노트


이름과 점수를 가진 nested list(students) 만들기

if __name__ == '__main__':

    students = []  # 빈 리스트
    scores = []  # 빈 리스트
    for _ in range(int(input())): # 반복문으로 리스트에 value 채우기.
        name = input()
        score = float(input())
        
        student = [name, score] # 휘발성(?) 리스트
        students.append(student)
        scores.append(score)

 

 

두 번째로 낮은 점수 찾기

if __name__ == '__main__':

    students = []
    scores = []
    for _ in range(int(input())):
        name = input()
        score = float(input())
        
        
        student = [name, score]
        students.append(student)
        scores.append(score)
        
        
    second_low_score = sorted(set(scores))[1]  # 두 번째로 낮은 점수 찾기
더보기

INPUT

print(second_low_score)

OUTPUT

37.21

 

 

두 번째로 낮은 점수를 받은 학생(들)을 가진 리스트 만들기

if __name__ == '__main__':

    students = []
    scores = []
    for _ in range(int(input())):
        name = input()
        score = float(input())
        
        
        student = [name, score]
        students.append(student)
        scores.append(score)
    second_low_score = sorted(set(scores))[1]
    

    target_name_score = []  # 목표한 점수를 가진 학생들의 이름, 점수
    for i in students:
        if i[1] == second_low_score:
            target_name_score.append(i)
더보기

INPUT

 print(target_name_score)

OUTPUT

[['Harry', 37.21], ['Berry', 37.21]]

 

 

알파벳순 정렬 후 이름만 출력

if __name__ == '__main__':

    students = []
    scores = []
    for _ in range(int(input())):
        name = input()
        score = float(input())
        
        
        student = [name, score]
        students.append(student)
        scores.append(score)
    second_low_score = sorted(set(scores))[1]
    
    target_name_score = []
    for i in students:
        if i[1] == second_low_score:
            target_name_score.append(i)
    
    for j in range(len(target_name_score)):
        print(sorted(target_name_score)[j][0])
  • nested list(중첩 리스트?) 도 대활호( [ ] )를 이용해 인덱싱이 가능함

 

 

두 번째 시도

if __name__ == '__main__':
    
    student_grade = []
    scores = []
    for _ in range(int(input())):
        name = input()
        score = float(input())
        
        student_grade.append([name, score])
        student_grade.sort()
        scores.append(score)
    
    
    scores = list(set(scores))
    scores.sort()
    second_lowest_grade = scores[1]
    
    for i in student_grade:
        if i[1] == second_lowest_grade:
            print(i[0])

 

  • sort(key=lambda x: (x[1], x[0])) 으로 이중 정렬하는 방법 존재