Skip to content

Instantly share code, notes, and snippets.



Created Dec 31, 2013
What would you like to do?
Get 95th percentile from stdin
#!/usr/bin/env python
# based on
# 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():
print percentile(numbers, 0.95)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.