Hackerrank.com find-hackerrank solution on Obj-C

I am trying to solve https://www.hackerrank.com/challenges/find-hackerrank on Obj-C and get ok through xCode, but not through hackerrank "Run Code".

xcode output:

http://monosnap.com/image/SBZTETlf6WjEiNPr50zpYQ7G119wC7

hackerrank output:

http://monosnap.com/image/9WApf0Q5ptYAkN7Oc1ipw9tBBauSJA

Thus, it is strange to see different exits.

my code is:

#import <Foundation/Foundation.h>

int main()
{
    NSFileHandle *input;
    NSData *inputData;

    NSString *match = @"hackerrank";
    int amount;
    NSString *str;

    input = [NSFileHandle fileHandleWithStandardInput];
    inputData = [input availableData];
    amount = [[[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding] intValue];

    for (int j = 0; j < amount; j++)
    {
        inputData = [input availableData];

        str = [[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding];
        str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];

        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;
}

Any ideas?

+4
source share
1 answer

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;
}
+4

Source: https://habr.com/ru/post/1568756/


All Articles