10 Day Statistics Challenge

Day 1 Mean, Median, Mode, and Range

  • The “mean“ is the “average” you’re used to, where you add up all the numbers and then divide by the number of numbers. $\mu = \frac{\sum^n_i x_i}{n}$
  • The “median“ is the “middle” value in the list of numbers. To find the median, your numbers have to be listed in numerical order from smallest to largest, so you may have to rewrite your list before you can find the median.
  • The “mode“ is the value that occurs most often.If no number in the list is repeated, then there is no mode for the list.
  • The “range“ of a list a numbers is just the difference between the largest and smallest values.
  • Weighted Mean: $\frac{\sum_i^N w_in_i}{\sum_i^N w_i}$
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
import sys


def mmm(arr, arrlen):
# mean
mean = sum(arr)/arrlen
print("{:1}".format(mean))
# median
sar = sorted(arr)
midx = arrlen//2
if arrlen%2 == 1:
print(sar[midx])
else:
median = (sar[midx-1]+sar[midx])/2
print("{:1}".format(median))

# mode
import collections
cnt = collections.Counter(arr)
m = max(cnt.values())
for k in sorted(cnt.keys()):
if cnt[k] == m:
print(k)
break

if __name__ == '__main__':
arrlen=int(sys.stdin.readline()) #integer input
arrs = sys.stdin.readline()
arrs = [int(a) for a in arrs.split(" ")]
mmm(arrs[:arrlen], arrlen)

###

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import sys 

def weighted_mean(arrs, weights):
weighted = [w*a for w,a in zip(arrs, weights)]
w_m = sum(weighted)/sum(weights)
print("{:.1f}".format(w_m))

if name == 'main':
arrlen =int(sys.stdin.readline()) #integer input
arrs = sys.stdin.readline()
weights = sys.stdin.readline()
arrs = [int(a) for a in arrs.split(" ")]
weights = [int(a) for a in weights.split(" ")]
weighted_mean(arrs[:arrlen], weights[:arrlen])