Skip to content

Instantly share code, notes, and snippets.

@zeeshanalisyed
Last active October 26, 2021 08:45
Show Gist options
  • Save zeeshanalisyed/42bf3b2df8ba018a34f55db303bc87d8 to your computer and use it in GitHub Desktop.
Save zeeshanalisyed/42bf3b2df8ba018a34f55db303bc87d8 to your computer and use it in GitHub Desktop.
10-days-of-statistics-python-1.py
import sys
import functools
import statistics
lines = sys.stdin.readlines()
nOfLines = int(lines[0][:-1])
nlist = [float(x) for x in lines[1].split()]
def summation(arr):
return functools.reduce(lambda a, b: a+b, arr);
def mean(arr, n):
return summation(arr)/n;
def median(arr, n):
arr.sort()
mid_1 = int(n/2)-1;
mid_2 = int(n/2);
return (arr[mid_1]+arr[mid_2])/2;
def most_frequent(List):
return max(set(List), key = List.count)
def mode(arr, n):
count = {}
current_mode = min(arr);
prev_counter = 1
for elem in arr:
key = str(elem)
if (key not in count):
count[key] = 1;
else:
count[key] += 1;
for k in count:
if (count[k] > 1 and current_mode != k and prev_counter < count[k]):
current_mode = k
prev_counter = count[k];
return int(float(current_mode));
print(mean(nlist, nOfLines))
print(median(nlist, nOfLines))
print(mode(nlist, nOfLines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment