Created
May 14, 2014 14:57
-
-
Save r0drigopaes/401b298741c2fa88f70a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stats(lst): | |
_min = None | |
_max = None | |
freq = {} | |
for i in lst: | |
i = abs(i) | |
if _min is None or i < _min: | |
_min = i | |
if _max is None or i > _max: | |
_max = i | |
if i in freq: | |
freq[i] += 1 | |
else: | |
freq[i] = 1 | |
lst_sorted = sorted(lst) | |
if len(lst_sorted) % 2 == 0: | |
middle = int(len(lst_sorted) / 2) | |
median = int((lst_sorted[middle] + lst_sorted[middle - 1]) / 2) | |
else: | |
median = lst_sorted[int(len(lst_sorted) / 2)] | |
mode_times = None | |
for i in freq.values(): | |
if mode_times is None or i > mode_times: | |
mode_times = i | |
mode = [] | |
for (num, count) in freq.items(): | |
if count == mode_times: | |
mode.append(num) | |
print("list = " + str(lst)) | |
print("min = " + str(_min)) | |
print("max = " + str(_max)) | |
print("median = " + str(median)) | |
print("mode(s) = " + str(mode)) | |
return {"min":_min, "max":_max,"median":median,"modes":mode} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment