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:
1,2,3,4,5;2 1,2,3,4,5;3
Output sample:
Print out the new comma separated list of numbers obtained after reversing. E.g.
2,1,4,3,5 3,2,1,4,5
-(void)reversedGroups
{
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:@","];
NSLog(@"%@",resultString);
}
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 usinglastObject
andobjectAtIndex:0
. I would prefer to use either[0]
and[1]
orlastObject
andfirstObject
.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– Yan2014年12月12日 21:25:38 +00:00Commented Dec 12, 2014 at 21:25
-
\$\begingroup\$
firstObject
andlastObject
should always be what you use when you can. \$\endgroup\$nhgrif– nhgrif2014年12月12日 21:39:29 +00:00Commented Dec 12, 2014 at 21:39
Explore related questions
See similar questions with these tags.