Skip to content

Instantly share code, notes, and snippets.

@DrewWeth
Created July 22, 2015 01:04
Show Gist options
  • Save DrewWeth/8ba4cbf6a3560f000d9c to your computer and use it in GitHub Desktop.
Save DrewWeth/8ba4cbf6a3560f000d9c to your computer and use it in GitHub Desktop.
fname = "numbers.txt"
with open(fname, "r") as f:
array = []
for line in f:
array.append(int(line.strip("\n")))
# print array
min = array[0]
max = array[0]
total = 0
dict = {}
for e in array:
total = total + e
if e > max:
max = e
if e < min:
min = e;
if not (bool(dict.get(e))):
dict[e] = 1
else:
dict[e] = dict[e] + 1
mode = 1
for e in dict.values():
if e > mode:
mode = e
if mode == 1:
mode = "N/A"
def med(x):
m,r= divmod(len(x),2)
if r:
return sorted(x)[m]
return sum(sorted(x)[m-1:m+1])/2
print "File name:", fname
print "Num count: ", len(array)
print "Sum: ", total
print "Median: ",med(array)
print "Mean: ", total/len(array)
print "Max: ", max
print "Min: ", min
print "Range: ", max-min
print "Mode: ", mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment