백준 2108 python [통계학]

인지용·2025년 2월 17일
0

알고리즘

목록 보기
43/46
post-thumbnail

https://www.acmicpc.net/problem/2108

import sys
from collections import Counter

# with open("./data.txt", "r") as file:
#     def input():
#         return file.readline().strip()
    
def input():
    return sys.stdin.readline().strip()

# 산술평균
def fun1():
    value = sum(arr) / N
    return round(value)

# 중앙값
def fun2():
    tempArr = arr
    tempArr.sort()
    
    idx = len(tempArr) // 2
    return arr[idx]

# 최빈값
def fun3():
    count = Counter(arr) 
    tempArr = count.most_common()
    maxCnt = tempArr[0][1]
    modes = []
    
    for data in tempArr:
        if(data[1] == maxCnt):
            modes.append(data[0])
    
    if(len(modes) > 1):
        return sorted(modes)[1]
    else:
        return modes[0]

# 최대값 최소값 사이 범위
def fun4():
    return max(arr) - min(arr)
        

N = int(input())
arr = []

for _ in range(N):
    arr.append(int(input()))
    
print(fun1())
print(fun2())
print(fun3())
print(fun4())

1,2 번은 그냥 풀고

4번은 -에 -를 하면 양수로 바뀐다는 것만 인지하면 되고

3번이 좀 헷갈렸는데
Counter라는 최빈값을 구해주는 함수가 있다.

이를 이용해서 같은 개수가 있는지 여부만 확인해서
정렬 후 두번째것만 리턴해주면 된다.
profile
한-줄

0개의 댓글