I am working through challenges at CodeEval.com and wanted to get your input on my code for this challenge.

Given a list of numbers and a positive integer k, reverse the elements of the list, k items at a time. If the number of elements is not a multiple of k, then the remaining items in the end should be left as is.

Input sample:

Your program should accept as its first argument a path to a filename. Each line in this file contains a list of numbers and the number k, separated by a semicolon. The list of numbers are comma delimited.

E.g. input:


Output sample:

Print out the new comma separated list of numbers obtained after reversing. E.g.

    NSString *line = @"22,23,24,25,26,27,28,29,30;3";   //input sample
    NSArray *lineArray = [line componentsSeparatedByString:@";"];
    NSUInteger numbElements = [[lineArray lastObject] intValue];
    NSArray *elementsArray = [[lineArray objectAtIndex:0] componentsSeparatedByString:@","];
    NSMutableArray *resultLineArray = [[NSMutableArray alloc]init];

    NSUInteger pos = 0;
    while (pos<[elementsArray count]) {
        //itterating through the elements
        if (pos+numbElements <= [elementsArray count]) {
            //make sure that i have enough elements to reverse
            for (NSUInteger i = pos+numbElements; i>pos; i--) {
                //add elements in reverse to the result array
                [resultLineArray addObject:[elementsArray objectAtIndex:i-1]];
        }else {
            for (NSUInteger j = pos; j<[elementsArray count]; j++) {
                //add elements that are left to the result array
                [resultLineArray addObject:[elementsArray objectAtIndex:j]];
        pos += numbElements;  // move the postion by number of elements to reverse
    //convert results array to string and add comma after each element as required for output
    NSString *resultString = [[resultLineArray valueForKey:@"description"] componentsJoinedByString:@","];


1 Answer 1


It's pretty simple task and I think code does its job well. I would point couple minor style issues:

  • You don't need to use objectAtIndex method to access elements inside an array. elementsArray[i] is enough and it's more readable.

  • You can access count property using dot-notation - array.count. Again, it's more readable.

  • Combining result string from array looks cumbersome and complicated. And rely on description property. Why don't you create result string as you go without having to create temp array first?

  • You are dividing line into lineArray. Then you're accessing first and last element using lastObject and objectAtIndex:0. I would prefer to use either [0] and [1] or lastObject and firstObject.

  • Pass the code through standard Xcode formatter to clean up spacing and indentation.

  • \$\begingroup\$ Thank you for the reply. I would use elementsArray[i] to access the elements but codeeval uses objc 2.0 and it returns an error. I thought combining the result would be easier later to print the words with comma. Next i would use a NSMutableString and just append in the code. \$\endgroup\$
    – Yan
    Commented Dec 12, 2014 at 21:25
  • \$\begingroup\$ firstObject and lastObject should always be what you use when you can. \$\endgroup\$
    – nhgrif
    Commented Dec 12, 2014 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.