cool hamsters never sleep
백준 2108. 통계학 본문
import sys
from collections import Counter
a = []
for i in range (int(sys.stdin.readline())) :
a.append(int(sys.stdin.readline()))
# 산술평균
print(round(sum(a)/len(a)))
# 중앙값 (갯수는 홀수이므로 한 가지 경우만 계산)
a.sort()
print(a[len(a)//2])
# 최빈값 (1개, 여러개일 때가 다름)
a2 = Counter(a).most_common()
if len(a2) > 1 and a2[0][1] == a2[1][1] :
print(a2[1][0])
else :
print(a2[0][0])
# 범위
print(max(a) - min(a))
from collections import Counter
Counter(a).most_common(n) : a의 요소를 세고 최빈값 n개 반환
정렬을 하였으므로
최빈값 중 최소값 : count[0][0]
최빈값 중 두번째로 작은 값 : count[1][0]
'Python Algorithm' 카테고리의 다른 글
백준 2839. 설탕 배달 (0) | 2022.08.15 |
---|---|
백준 10814. 나이순 정렬 (0) | 2022.08.14 |
백준 2609. 최대공약수와 최소공배수 (0) | 2022.08.12 |
백준 24416. 알고리즘 수업 (미해결) (0) | 2022.08.12 |
백준 3009. 네 번째 점 (0) | 2022.08.11 |
Comments