[Python] 백준 2108 통계학 풀이

지민·2023년 1월 26일
0
post-thumbnail
import sys
import math
import collections
input = sys.stdin.readline


def 평균(numbers):
    return round(sum(numbers) / len(numbers))


def 중앙값(numbers):
    return numbers[len(numbers) // 2]


def 최빈값(board):
    result = []
    target = max(board.values())
    for key, value in board.items():
        if value == target:
            result.append(key)
    if len(result) == 1:
        return result[0]
    return sorted(result)[1]


def 범위(numbers):
    return 0 if len(numbers) == 1 else max(numbers) - min(numbers)


board = collections.defaultdict(int)
n = int(input().rstrip())
numbers = sorted([int(input()) for _ in range(n)])

for number in numbers:
    board[number] += 1

print(평균(numbers))
print(중앙값(numbers))
print(최빈값(board))
print(범위(numbers))
profile
남들 개발 공부할 때 일기 쓰는 사람

0개의 댓글