본문 바로가기

알고리즘/백준 ~ 단계별 풀어보기

백준 2108 "통계학"

Python

import sys
from typing import Counter


n = int(input())
k = []
answer = []
for _ in range(n):
    k.append(int(sys.stdin.readline()))

sortedK = sorted(k)

arithmeticMean = sum(k)/n
median = sortedK[n//2]
r = max(k) - min(k)

mode = 0 
count = Counter(sortedK).most_common()

if len(count) >= 2 and count[0][1] == count[1][1]:
    mode = count[1][0]
else:
    mode = count[0][0]

print(round(arithmeticMean))
print(median) 
print(mode)
print(r)