Created
March 27, 2012 14:46
-
-
Save jeremyjbowers/2216530 to your computer and use it in GitHub Desktop.
A python function to compute NFL passer rating.
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
def handle_nfl_passer_rating(cmpls, yds, tds, ints): | |
""" Defines a function which handles passer rating calculation for the NFL.""" | |
def _min_max(x, xmin, xmax): | |
""" | |
Defines a function which enforces a minimum and maximum value. | |
Input: x, the value to check. | |
Input: xmin, the minumum value. | |
Input: xmax, the maximum value. | |
Output: Either x, xmin or xmax, depending. | |
""" | |
# Check if x is less than the minimum. If so, return the minimum. | |
if x < xmin: | |
return xmin | |
# Check if x is greater than the maximum. If so, return the maximum. | |
elif x > xmax: | |
return xmax | |
# Otherwise, just return x. And weep for the future. | |
else: | |
return x | |
# Step 0: Make sure these are floats, dammit. | |
cmpls = cmpls + 0.0 | |
yds = yds + 0.0 | |
tds = tds + 0.0 | |
ints = ints + 0.0 | |
# Step 1: The completion percentage. | |
step_1 = cmpls - 0.3 | |
step_1 = step_1 * 5 | |
step_1 = _min_max(step_1, 0, 2.375) | |
# Step 2: The yards per attempt. | |
step_2 = yds - 3 | |
step_2 = step_2 * 0.25 | |
step_2 = _min_max(step_2, 0, 2.375) | |
# Step 3: Touchdown percentage. | |
step_3 = tds * 20 | |
step_3 = _min_max(step_3, 0, 2.375) | |
# Step 4: Interception percentage. | |
step_4 = ints * 25 | |
step_4 = 2.375 - step_4 | |
step_4 = _min_max(step_4, 0, 2.375) | |
# Step 5: Compute the rating based on the sum of steps 1-4. | |
rating = step_1 + step_2 + step_3 + step_4 + 0.0 | |
rating = rating / 6 | |
rating = rating * 100 | |
# Step 6: Return the rating, formatted to 1 decimal place, as a Decimal. | |
return rating |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ported it to Objective-C: https://gist.github.com/2216724