Skip to content

Instantly share code, notes, and snippets.

@vikrum vikrum/95th.py
Created Dec 31, 2013

Embed
What would you like to do?
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
You can’t perform that action at this time.