PS/Python
[Python] 백준 2108번: 통계학 - 정렬
yoo.o
2020. 9. 5. 15:45
반응형
문제
2108번: 통계학
첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.
www.acmicpc.net
풀이
collections 모듈의 Counter 클래스에있는 most_common() 메쏘드는 등장한 횟수를 내림차순으로 정리해서 보여준다.
메쏘드 이름이 most_common이라 가장 빈도가 높은 아이템만 리턴하는줄 알았는데 빈도가 높은 순으로 전체를 리턴한다.
import sys
from collections import Counter
N = int(sys.stdin.readline())
li = []
for _ in range(N):
li.append(int(sys.stdin.readline()))
# 산술평균 (소숫점 첫째자리에서 반올림)
print(round(sum(li)/len(li)))
# 중앙값
li.sort()
print(li[(N-1)//2])
# 최빈값
if N == 1:
print(li[0])
else:
c = Counter(li).most_common(2)
if c[0][1] == c[1][1]:
print(c[1][0])
else:
print(c[0][0])
# 범위
print(li[-1] - li[0])
반응형