Challenge 1 & 2 solutions


#1

Okay, so, challenge one was very easy - here is my code:

            //Create an empty array
        
        NSMutableArray *grocerieList = [[NSMutableArray alloc]init];

            //Add some items to the array
        
        [grocerieList addObject:@"Milk"];
        [grocerieList addObject:@"Bread"];
        [grocerieList addObject:@"Butter"];
        [grocerieList addObject:@"Eggs"];

            //Print them to the console

        for (NSString *groceries in grocerieList){

            NSLog(@"I bought %@ from the shops", groceries); 
        }

Output:

[quote]2013-06-12 12:18:51.621 Groceries[16529:303] I bought Milk from the shops
2013-06-12 12:18:51.623 Groceries[16529:303] I bought Bread from the shops
2013-06-12 12:18:51.623 Groceries[16529:303] I bought Butter from the shops
2013-06-12 12:18:51.623 Groceries[16529:303] I bought Eggs from the shops
[/quote]

The second challenge was more difficult - but I also read it wrong. I was interpreting Aaron meaning “Go through both files and look for glen and compare them” - However, when I checked some of the solutions it seems he meant: "Go through both files and compare proper names for all words / names in those files. So I was on the right train - just the wrong tracks!

Anyway - I came up with this:

[code] //Read through the contents of the files - Not worring about errors

    NSString *wordString = [NSString stringWithContentsOfFile:@ "/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
    NSString *nameString = [NSString stringWithContentsOfFile:@ "/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];

        //Break them down into strings in an array

    NSArray *wordsArray = [wordString componentsSeparatedByString:@"\n"];
    NSArray *na[quote][/quote]meArray = [nameString componentsSeparatedByString:@ "\n"];

        //Go over both (Fast enum)

    for (NSString *w in wordsArray){
        for (NSString *p in nameArray){

            if ([w caseInsensitiveCompare:p] == NSOrderedSame){
                NSLog (@"%@ is both a word and a proper name",p );                    
            }
        }

[/code]

Output looks like this:

[quote]013-06-12 12:16:02.840 NameStrings[16501:303] Aaron is both a word and a proper name
2013-06-12 12:16:03.005 NameStrings[16501:303] Adam is both a word and a proper name
2013-06-12 12:16:03.037 NameStrings[16501:303] Adlai is both a word and a proper name
2013-06-12 12:16:03.053 NameStrings[16501:303] Adrian is both a word and a proper name
2013-06-12 12:16:03.126 NameStrings[16501:303] Agatha is both a word and a proper name
2013-06-12 12:16:03.158 NameStrings[16501:303] Ahmed is both a word and a proper name
2013-06-12 12:16:03.159 NameStrings[16501:303] Ahmet is both a word and a proper name
2013-06-12 12:16:03.165 NameStrings[16501:303] Aimee is both a word and a proper name


…[/quote]

So there we have it. The second challenge isn’t that hard - I just misunderstood for some reason! :blush: