Skip to content

Instantly share code, notes, and snippets.

@vikrum
Created December 31, 2013 21:50
Show Gist options
  • Save vikrum/8202564 to your computer and use it in GitHub Desktop.
Save vikrum/8202564 to your computer and use it in GitHub Desktop.
Get 95th percentile from stdin
#!/usr/bin/env python
#
# based on http://stackoverflow.com/a/2753343o
#
# input needs to be presorted
#
import math
import functools
import fileinput
def percentile(N, percent, key=lambda x:x):
if not N:
return None
k = (len(N)-1) * percent
f = math.floor(k)
c = math.ceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) * (c-k)
d1 = key(N[int(c)]) * (k-f)
return d0+d1
numbers = []
for line in fileinput.input():
numbers.append(float(line))
print percentile(numbers, 0.95)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment