Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created February 17, 2017 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/efc72d2d4bd195a9541f5504fe9fd5ef to your computer and use it in GitHub Desktop.
Save anonymous/efc72d2d4bd195a9541f5504fe9fd5ef to your computer and use it in GitHub Desktop.
Full code for Objective-C 2.0 sample
#import <Foundation/Foundation.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSException.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSObjCRuntime.h>
#include <string.h>
#import <GNUstepBase/GSObjCRuntime.h>
#import <CW/CWTest.h>
NSNumber *ICKGetMaxProfit(NSArray *stockPricesYesterday, NSUInteger length);
void test_func(NSArray *tests) {
for (NSArray *test in tests) {
describe(@"Compare output results of function with ref values.", ^() {
it(@"should match ref value", ^() {
equal(ICKGetMaxProfit([test objectAtIndex:0], [[test objectAtIndex:0] count]),
(NSNumber*)[test objectAtIndex:1]);
});
});
}
}
void test(BOOL includePositive, BOOL includeNegative) {
NSArray *positiveTests = @[
@[@[@10, @20, @5], @10],
@[@[@10, @5, @10, @20], @15],
@[@[@10, @10, @10], @0]
];
NSArray *negativeTests = @[
@[@[@35, @20, @10], @(-10)],
@[@[@30, @20, @10], @(-10)],
@[@[@100, @70, @50], @(-20)]
];
if (includePositive) {
test_func(positiveTests);
}
if (includeNegative) {
test_func(negativeTests);
}
}
NSNumber *ICKGetMaxProfit(NSArray *stockPricesYesterday, NSUInteger length) {
NSInteger minPrice, maxProfit;
// make sure we have at least 2 prices
NSString *errorDesc = [NSString stringWithFormat:@"parameter length: expected 2 or more but got %lu", (unsigned long)length];
NSCAssert(length >= 2, errorDesc);
// we'll greedily update minPrice and maxProfit, so we initialize
// them to the first price and the first possible profit
minPrice = [[stockPricesYesterday objectAtIndex:0] integerValue];
maxProfit = [[stockPricesYesterday objectAtIndex:1] integerValue] - [[stockPricesYesterday objectAtIndex:0] integerValue];
// start at the second (index 1) time
// we can't sell at the first time, since we must buy first,
// and we can't buy and sell at the same time!
// if we started at index 0, we'd try to buy *and* sell at time 0.
// this would give a profit of 0, which is a problem if our
// maxProfit is supposed to be *negative*--we'd return 0!
for (NSUInteger i = 1; i < length; i++) {
NSInteger currentPrice = [[stockPricesYesterday objectAtIndex:i] integerValue];
// see what our profit would be if we bought at the
// min price and sold at the current price
NSInteger potentialProfit = currentPrice - minPrice;
// update maxProfit if we can do better
maxProfit = MAX(maxProfit, potentialProfit);
// update minPrice so it's always
// the lowest price we've seen so far
minPrice = MIN(minPrice, currentPrice);
}
return @(maxProfit);
}
int main (int argc, const char * argv[])
{
@autoreleasepool {
test(YES, YES);
// TODO: tests for exceptions
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment