[Python] 백준 25325번 : 학생 인기도 측정
2025. 4. 18. 18:20
99클럽 코테 스터디 | 비기너 | 15일차
접근 방법
1. student라고 하는 dictionary 안에 입력 받은 n명의 학생 이름을 key 값으로 세팅한다.
2. n명의 학생들로부터 받은 좋아하는 학생 이름에 대하여 student dict 안에 key가 있으면 value를 하나씩 더한다.
3. student dict를 정렬한다.
4. student의 key, value를 순서대로 프린트한다.
import sys
n = sys.stdin.readline()
names = sys.stdin.readline().split()
# set student dict
student = {}
for name in names:
student.setdefault(name, 0)
# vote popularity of each student
for _ in range(int(n)):
popular_students = sys.stdin.readline().split()
for p_s in popular_students:
if p_s in student.keys():
student[p_s] += 1
# sort student dict
sorted_student = dict(sorted(student.items(), key=lambda item: item[1], reverse=True))
# print the result
for s, count in sorted_student.items():
print(s, count)
'코딩테스트 > BOJ' 카테고리의 다른 글
[Python] 백준 4949번 : 균형잡힌 세상 (0) | 2025.04.19 |
---|---|
[Python] 백준 9012번 : 괄호 (0) | 2025.04.19 |
[Python] 백준 20723번 : 브실이의 입시전략 (0) | 2025.04.18 |
[Python] 백준 1181번 : 단어 정렬 (0) | 2025.04.17 |
[Python] 백준 25757번 : 임스와 함께하는 미니게임 (0) | 2025.04.15 |