The difference between running in Xcode and HackerRank is that when run locally, the call
[input availableData]
stops when your program reads the next line from the console. This allows you to call availableDataseveral times, each time you get the next line.
HackerRank, availableData , , .
, .
, HackerRank:
#import <Foundation/Foundation.h>
int main()
{
NSFileHandle *input;
NSString *match = @"hackerrank";
int amount;
input = [NSFileHandle fileHandleWithStandardInput];
NSArray *inputData = [[[NSString alloc] initWithData:[input availableData] encoding:NSUTF8StringEncoding] componentsSeparatedByString: @"\n"];
amount = [inputData[0] intValue];
for (int j = 1; j <= amount; j++)
{
NSString *str = inputData[j];
NSArray *redexArr = @[match,
[NSString stringWithFormat:@"^%@.+", match],
[NSString stringWithFormat:@".+%@$", match]
];
for (int i = 2; i>=-1; i--)
{
if (i <= -1)
{
printf("-1\n");
} else
{
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", redexArr[i]];
if ([pred evaluateWithObject:str])
{
printf("%d\n", i);
break;
}
}
}
}
return 0;
}