Skip to content

Instantly share code, notes, and snippets.

@r0drigopaes
Created May 14, 2014 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r0drigopaes/401b298741c2fa88f70a to your computer and use it in GitHub Desktop.
Save r0drigopaes/401b298741c2fa88f70a to your computer and use it in GitHub Desktop.
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