Skip to content

Instantly share code, notes, and snippets.

@HeidiHansen
Created October 5, 2014 00:41
Show Gist options
  • Save HeidiHansen/9c1f62c35ff7cfbaceac to your computer and use it in GitHub Desktop.
Save HeidiHansen/9c1f62c35ff7cfbaceac to your computer and use it in GitHub Desktop.
Playing With Array methods.
#import "Collections.h"
@implementation Collections
//sorts the array in ascending order
-(NSArray *)sortArrayAsc:(NSArray *)array
{
return [array sortedArrayUsingSelector:@selector(compare:)];
}
//sorts the array in descending order
-(NSArray *)sortArrayDesc:(NSArray *)array
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
return [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
}
//swaps index 1 and 2
-(NSArray *)swapElements:(NSArray *)array
{
NSMutableArray *result = [[NSMutableArray alloc] initWithArray:array];
[result exchangeObjectAtIndex:1 withObjectAtIndex:2];
return result;
}
//reverses the array
-(NSArray *)reverseArray:(NSArray *)array
{
return [[array reverseObjectEnumerator] allObjects];
}
//replaces 3rd element of each string with $ sign
-(NSArray *)keshaMaker:(NSArray *)array
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSString *object in array){
NSString *newString = [object stringByReplacingCharactersInRange:NSMakeRange(2, 1) withString:@"$"];
[result addObject:newString];
}
return result;
}
//sorts array into two arrays, one greater and one less than 10, creates a dict with both
-(NSDictionary *)greaterAndLessThan10:(NSArray *)array
{
NSMutableArray *greater_than_10 = [[NSMutableArray alloc] init];
NSMutableArray *less_than_10 = [[NSMutableArray alloc] init];
for (NSNumber *item in array){
if ([item integerValue] > 10){
[greater_than_10 addObject:item];
}
else {
[less_than_10 addObject:item];
}
}
return @{@"greater_than_10": greater_than_10, @"less_than_10": less_than_10};
}
//creates an array of winners from people dictionary
-(NSArray *)findWinners:(NSDictionary *)peopleDictionary
{
return [peopleDictionary allKeysForObject:@"winner"];
}
//returns an array with all words that contain 'a'
-(NSArray *)findA:(NSArray *)array
{
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"SELF beginsWith[c] 'a'"];
return [array filteredArrayUsingPredicate:aPredicate];
}
//returns the sum of the values in the array
-(NSInteger)sumArray:(NSArray *)array
{
NSNumber *sum = [array valueForKeyPath:@"@sum.self"];
return [sum integerValue];
}
//returns an array that added 's' to each word in array except for 2nd element in the array
-(NSArray *)addS:(NSArray *)array
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for (int x = 0; x < array.count; x++){
NSString *currentItem = array[x];
if (x == 1) {
[result addObject:currentItem];
} else {
[result addObject:[NSString stringWithFormat:@"%@s", currentItem]];
}
}
return result;
}
//returns a dictionary with word keys and # of word occurences in story as values
-(NSDictionary *)countWordsInStory:(NSString *)story
{
NSArray *storyWords = [story componentsSeparatedByString:@" "];
NSMutableDictionary *wordDictionary = [[NSMutableDictionary alloc] init];
for (NSString *word in storyWords){
if (wordDictionary[word]){
NSInteger count = [wordDictionary[word] integerValue];
count++;
wordDictionary[word] = [NSNumber numberWithInteger:count];
} else {
wordDictionary[word] = @1;
}
}
return wordDictionary;
}
//creates a dictionary with artist keys and values of songs related to the artist
-(NSDictionary *)organizeSongsByArtist:(NSArray *)array
{
NSMutableDictionary *artists = [[NSMutableDictionary alloc] init];
for (NSString *jam in array){
NSArray *splitJam = [jam componentsSeparatedByString:@" - "];
NSString *artist = splitJam[0];
NSString *song = splitJam[1];
if (artists[artist]){
NSMutableArray *artistTracks = artists[artists];
[artistTracks addObject:song];
} else {
artists[artist] = [[NSMutableArray alloc] initWithObjects:song, nil];
}
}
return artists;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment