Created
August 7, 2018 14:28
-
-
Save Gormador/9bcf31bb46eabc8aea83534ce5021949 to your computer and use it in GitHub Desktop.
Code for two different approaches of percentile computation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Naive, brute-force approach. | |
# You simply count how many values are smaller than the one for which you want to compute the percentile. | |
# | |
def getPercentileOfDensity(density): | |
nbInf = 0 | |
for d in sortedDensities: | |
if d < density: | |
nbInf+=1 | |
else: | |
break | |
return (nbInf / len(sortedDensities)) * 100 | |
## | |
# Dichotomy-based, tail-recursive solution. | |
# | |
# | |
def getPercentileOfDensityRecTerm(density, densitiesList, lowerBound, higherBound): | |
if (higherBound - lowerBound) <= 1: | |
return lowerBound | |
step = (lowerBound + higherBound)//2 | |
if densitiesList[step] == density: | |
return step + 1 | |
elif densitiesList[step] < density: | |
return getPercentileOfDensityRecTerm(density, densitiesList, step+1, higherBound) | |
else: | |
return getPercentileOfDensityRecTerm(density, densitiesList, lowerBound, step-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment