Skip to content

Instantly share code, notes, and snippets.

@asmallteapot
Forked from jeremyjbowers/passer_rating.py
Created March 27, 2012 15:08
Show Gist options
  • Save asmallteapot/2216724 to your computer and use it in GitHub Desktop.
Save asmallteapot/2216724 to your computer and use it in GitHub Desktop.
An Objective-C method to compute NFL passer rating.
#import <Foundation/Foundation.h>
#define NUMBER_IN_RANGE(num, floor, ceiling) ((num >= floor && num <= ceiling) ? num : ((num < floor) ? floor : ceiling))
@interface NFLQuarterback
@property (nonatomic, strong) NSNumber *completions;
@property (nonatomic, strong) NSNumber *interceptions;
@property (nonatomic, strong) NSNumber *touchdowns;
@property (nonatomic, strong) NSNumber *yards;
@property (readonly) NSNumber *passerRating;
@end
#import "NFLQuarterback.h"
@implementation NFLQuarterback
@dynamic completions;
@dynamic interceptions;
@dynamic touchdowns;
@dynamic yards;
// Defines a function which handles passer rating calculation for the NFL.
- (NSNumber *)passerRating {
float rating;
// Step 1: Completion percentage
float completionPercentage = ([self.completions floatValue] - 0.3) * 5.0;
completionPercentage = NUMBER_IN_RANGE(completionPercentage, 0, 2.375);
rating += completionPercentage;
// Step 2: Yards per attempt
float yardsPerAttempts = ([self.yards floatValue] - 3.0) * 0.25;
yardsPerAttempt = NUMBER_IN_RANGE(yardsPerAttempt, 0, 2.375);
rating += yardsPerAttempt;
// Step 3: Touchdown percentage
float touchdownPercentage = NUMBER_IN_RANGE([self.touchdowns floatValue] * 20, 0, 2.375);
rating += touchdownPercentage;
// Step 4: Interception percentage
float interceptionPercentage = 2.375 - (self.interceptions * 25);
interceptionPercentage = NUMBER_IN_RANGE(interceptionPercentage, 0, 2.375);
rating += interceptionPercentage;
// Step 5: Voodoo
rating = (rating / 6) * 100;
// Step 6: Return an NSNumber of the passer rating
return [NSNumber numberWithFloat:rating];
}
@end
@asmallteapot
Copy link
Author

I should note that I have not actually tested this code. Should be close enough though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment