Last active
December 19, 2015 16:55
-
-
Save guangLess/8876fe65d5db553c5565 to your computer and use it in GitHub Desktop.
Array Question FlatironSchool-interview-Question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSArray *germanMakes = @[@"A"]; | |
NSArray * fin = [self sortMidtoFront:germanMakes]; | |
NSLog(@"fin = [%@]",fin); | |
NSLog(@"testing Number is %lu",(unsigned long)[self checkMidNumber:2]); | |
NSLog(@"testing Number is %lu",(unsigned long)[self checkMidNumber:10]); | |
} | |
-(void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
} | |
-(NSMutableArray *)sortMidtoFront:(NSArray *)originArray{ | |
if (!originArray.count) { | |
return nil; | |
}else if (originArray.count == 1){ | |
return originArray[0]; | |
}else{ | |
NSMutableArray * originCopy = originArray.mutableCopy; | |
NSUInteger midNumber = [self checkMidNumber:originCopy.count]; | |
id object = originCopy[midNumber]; | |
[originCopy removeObjectAtIndex:midNumber]; | |
[originCopy insertObject: object atIndex:0]; | |
return originCopy; | |
} | |
} | |
-(NSUInteger)checkMidNumber:(NSUInteger)arrayCount { | |
unsigned long midIndex = arrayCount/2; | |
if (! (midIndex == (int)midIndex) ) { | |
midIndex = floor(arrayCount - 1)/2; | |
} else { | |
midIndex = (arrayCount - 1)/2; | |
} | |
return midIndex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment