My solution to Challenge 2


#1

/ main.m
// Match proper_to_common
//
// Created by Nathan Annenberg on 8/24/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool {
    //This program searches dictionary for all words that match i.e. proper nouns to common nouns which should follow
        
    {    
            //Read in a file as a huge string (ignoring the possibility of error)
        NSString *nameString = [NSString   stringWithContentsOfFile:@"/usr/share/dict/words"
                                    encoding:NSUTF8StringEncoding
                                    error:NULL];
            
            //Break it into an array of strings
            NSArray *names =  [nameString  componentsSeparatedByString:@"\n"];
            
        NSUInteger limit = [names  count];
        int match_counter = 0;
        
    //Go through the array one string at a time and compare with next consecutive string (since matches will be consecutive words)
        for (int i = 0; i < limit-1; i++) {
            NSString *a = [names  objectAtIndex:i];
            NSString *b = [names  objectAtIndex:i+1];
            if ([a  caseInsensitiveCompare:b] == NSOrderedSame)  {
                NSLog(@"Match:  %@ = %@\n",  a, b);
                match_counter++;                                        
                }
            }
        NSLog(@"Out of a total of %lu words, there were %d matches\n", limit, match_counter);
        
        return 0;
    }

}

}


#2

Re: My above program: I got 1515 matches out of the total 235887 words in the dictionary. I thought the run would be too long so I didn’t enter it. Anyway: a typical iteration would read:
“match: Zoa zoa”. I’m a little uneasy that a lot of these word pairs even exist. It’s hard to believe there is both a proper noun version and a common noun version in many of these cases. I deliberately programmed this to test if consecutive words would match, since Aaron said uppercase match would precede lower case.


#3

There were some great examples that really helped as I was a bit stuck at first. Below is my solution and it seems to work quite well. Originally I had variables for [names objectAtIndex:i] caseInsensitiveCompare:[names objectAtIndex:i+1] but when I removed the variables and put the messages directly in the if statement the solution ran in half the time. Has anyone else found that? Is that normal? I have done it a couple of times so while it may be my imagination, al least its repeatable.

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        //read in a file as a huge string ignoring the possibility of an error
        NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
        
        //Break up the string and feed it to an array.
        NSArray * names = [nameString componentsSeparatedByString:@"\n"];
        NSUInteger arrayCount = [names count];
        
       //Check for Proper Names that are also common words and print them out.
        for (int i=0; i < arrayCount-1; i++) {
            if ([[names objectAtIndex:i] caseInsensitiveCompare:[names objectAtIndex:i+1]] == NSOrderedSame){
                NSLog(@"%@ is the same as %@", [names objectAtIndex:i], [names objectAtIndex:i+1]);
            }
            
        
        }
        
        
    }
    return 0;
}