From 0b8d135b328eb5d0140a59d7102d293cb0d7e4a4 Mon Sep 17 00:00:00 2001 From: inthewalter <21400564@handong.edu> Date: Fri, 29 Mar 2019 03:37:54 +0900 Subject: [PATCH] =?UTF-8?q?Solve=20#5=202108=20=ED=86=B5=EA=B3=84=ED=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/2108/21400564.py | 89 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 problems/2108/21400564.py diff --git a/problems/2108/21400564.py b/problems/2108/21400564.py new file mode 100644 index 0000000..c758565 --- /dev/null +++ b/problems/2108/21400564.py @@ -0,0 +1,89 @@ +# 19-1 +# 21400564 이용호 +import sys + +class Solution: + def __init__(self, N, n_list) : + self.N = N + self.n_list = n_list + + self.ans1 = 0 # 산술평균 + self.ans2 = 0 # 중앙값 + self.ans3 = 0 # 최댓값과 최솟값의 차이 + self.ans4 = 0 # 최빈값 + self.sorted_n_list = [] + + def solution_print(self): + print(self.ans1) + print(self.ans2) + print(self.ans3) + print(self.ans4) + + # 산술평균 + def solution_ans1(self): + self.ans1 = round(sum(self.n_list)/self.N) + + # 중앙값, N은 홀수라고 가정(문제 조건) + def solution_ans2(self): + self.sorted_n_list = sorted(self.n_list) + self.ans2 = self.sorted_n_list[len(self.n_list)//2] + + # 최빈값 + def solution_ans3(self): + # 딕셔너리 형태로, 빈도별로 저장 + # {숫자: 빈도} + distribution = {} + + for i in self.sorted_n_list: + if i in distribution: + distribution[i] += 1 + else: + distribution[i] = 1 + + mode_list = [] # 최빈값 리스트 + max_count = 0 # 무조건 1번이상 나온다. + + # 가장 빈도(value)가 높은 숫자(key)를 찾아냄 + for i in distribution: + if distribution[i] > max_count : + max_count = distribution[i] + + # 최빈값 조사 + for i in distribution: + if distribution[i] == max_count: + mode_list.append(i) + + mode_list = sorted(mode_list) + # 최빈값이 2개 이상이면, 두번째로 작은 것 출력 + if len(mode_list) > 1: + self.ans3 = mode_list[1] + else : + self.ans3 = mode_list[0] + + # 최댓값과 최솟값의 차이 + def solution_ans4(self): + self.ans4 = self.sorted_n_list[len(self.n_list) - 1] - self.sorted_n_list[0] + + def solution(self): + # 산술평균 + self.solution_ans1() + # 중앙값, N은 홀수라고 가정(문제 조건) + self.solution_ans2() + # 최빈값 구하는 과정 + self.solution_ans3() + # 최댓값과 최솟값의 차이 + self.solution_ans4() + + # 솔루션 출력 + self.solution_print() + + +if __name__ == "__main__": + n_list = [] + n = int(sys.stdin.readline()) + + for i in range(n): + n_list.append(int(sys.stdin.readline())) + + solution = Solution(n, n_list) + solution.solution() \ No newline at end of file