cool hamsters never sleep

백준 2108. 통계학 본문

Python Algorithm

백준 2108. 통계학

슈슈 susu 2022. 8. 13. 16:45
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]

 

 

Comments