-
-
Save asmallteapot/2216724 to your computer and use it in GitHub Desktop.
An Objective-C method to compute NFL passer rating.
This file contains hidden or 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
#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 |
This file contains hidden or 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
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I should note that I have not actually tested this code. Should be close enough though.