[BOJ][Python][실4][2108] 통계학

문제 링크

문제링크

첫 번째 풀이

정답코드

최빈값을 파이토닉하게 찾기가 어려웠습니다.

Counter(arr).most_common()를 사용하면 값과 등장 횟수가 내림차순으로 정렬됩니다. most_common()의 리턴타입은 튜플의 리스트이기 때문에 인덱스로 접근할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import sys
from math import pi, sqrt
from collections import deque, Counter

# sys.stdin = open("input.txt", 'r')

n = int(sys.stdin.readline())
arr = sorted(map(int, sys.stdin.read().split()))
arr.sort()
print(round(sum(arr) / n))
print(arr[n // 2])
cnt = Counter(arr).most_common()
if len(cnt) >= 2:
    if cnt[0][1] == cnt[1][1]:
        print(cnt[1][0])
    else:
        print(cnt[0][0])
else:
    print(cnt[0][0])
print(arr[-1] - arr[0])

Success Notice: 수고하셨습니다. :+1:

Leave a comment