My Ugly Solution


Here’s my admittedly ugly solution. It takes awhile, but it works! The logic is that it is comparing every word in the names array to every word in the words array. If it’s a proper word that is also a common word, it’ll end up finding it twice, so I just used a counter to increment an integer value. Every time the counter reaches 2, it’s hit on a word that’s both a proper name as well as a common word.

NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
        NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
        // Break it into an array of strings
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];

        int count = 0;
        for (NSString *n in names){
            count = 0;
            for (NSString *w in words){
                if ([n caseInsensitiveCompare:w] == NSOrderedSame) {
                    if(count == 2){
                        NSLog(@"%@ is both a name and a word.",n);



What’s the running time of your algorithm? Can you reduce it?


On my Mac, it takes roughly 16 seconds to run. I’m sure I could reduce it if I sat back down and plugged away at it. It’s only now, after I spent an hour or two on that challenge, that I’ve found out that it is way harder than the authors originally intended since the words list now contains proper words as well as common ones. When this book first came out, it sounds like that wasn’t the case. I’m sure there are more efficient ways of doing it, but not using anything I’ve learned/understood thus far. My main concern was getting it to actually run successfully with the correct results. I may come back to it as I learn more - especially after learning about documentation in the following chapter.