-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_intro
More file actions
35 lines (24 loc) · 886 Bytes
/
stats_intro
File metadata and controls
35 lines (24 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
### https://www.hackerrank.com/challenges/s10-basic-statistics/problem ###
# The following inputs should yield 43900.6, 44627.5, and 4978
# 10
# 64630 11735 14216 99233 14470 4978 73429 38120 51135 67060
# this makes it a bit more portable
#!/usr/bin/env python3
from collections import Counter
from operator import itemgetter
def find_med(arr):
if len(arr) %2 == 0:
return (arr[len(arr)//2 - 1] + arr[len(arr)//2]) / 2
if len(arr) %2 != 0:
return arr[len(arr)//2 - 1]
n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
arr.sort()
mean = sum(arr)/len(arr)
median = find_med(arr)
M = list(Counter(arr).most_common())
M = sorted(M, key = itemgetter(1), reverse = True)
mode = min([x[0] for x in M if x[1] == M[0][1]])
print('{0:.1f}'.format(mean))
print('{0:.1f}'.format(median))
print('{0:.1f}'.format(mode))