My Solution to 2nd challenge is not 293! Am I wrong?


#1

When I read the description of of the words file, it says it contains regular words AND proper names. Based on the description in the book, I created this:

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        int number=0;
        
        // create a large string from the file
        NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
        
        // enter all the words into an array; words are separated by new line escape character
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];
        
        // create a for loop using the number of elements in the array (count)
        for (int i = 0; i < [words count]; i++){
            
            // this if statement is to prevent any out of range exceptions
            if (i > 0)
            {
                // this if statement converts the current element to lowercase then compares it to
                // the word just before it (also converted to lowercase)
                if ([[words[i] lowercaseString] isEqualToString:[words[i-1] lowercaseString]])
                {
                    // prints the word and the proper name
                    NSLog(@"%@, %@", words[i], words[i-1]);
                    number++;
                }
            }
        }
        NSLog(@"Word count:  %d", number);
        
    }

    return 0;
}

Am I reading the book wrong? I’m looking at everyone else’s solution and they are using both the propernames file and the words file but I don’t see the point in doing that if the words file contains them all anyway. Am I missing something in the way the book wants us to do this?

Here’s a sample of my solution:

2014-02-21 15:10:31.343 InterestingNames[11090:303] zeppelin, Zeppelin
2014-02-21 15:10:31.344 InterestingNames[11090:303] zeuglodon, Zeuglodon
2014-02-21 15:10:31.344 InterestingNames[11090:303] zipper, Zipper
2014-02-21 15:10:31.345 InterestingNames[11090:303] zoa, Zoa
2014-02-21 15:10:31.345 InterestingNames[11090:303] zwieback, Zwieback
2014-02-21 15:10:31.346 InterestingNames[11090:303] Word count: 1515


#2

Nevermind. I just read the problem again. While my code lists all of the proper names that are also words, the problem wants you to find, COMMON proper names. I’m going to make a quick edit to my code and see if I can’t come up with the 293.


#3

I’m getting 1309. All the names in propernames are common.

Edit: Just read that that the proper names in the word files are capitalized, I was just treating them all as regular words.