When like me you only process the file "words"


#1

Hi everyone,

Thanks for reading my first ever post on the board.

The way I interpreted the second challenge at the bottom of page 90 (hence the program p90), I understood that only the file “words” had to be processed (unlike most solutions on the board that compare names from “propernames” against nouns from “words”). So here is my solution based on this interpretation.

I would be very happy to get some feedback on how to make the code more elegant, and maybe optimise it for speed… it’s slow, it’s really really slow :geek:

//
//  main.m
//  p90
//
//  Created by Sebastien on 16/04/13.
//  Copyright (c) 2013 Sebastien. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        // Read in a file as a huge string (ignoring the possibility of an error)
        NSString *file
        = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                    encoding:NSUTF8StringEncoding
                                       error:NULL];
        
        // Break it into an array of strings
        NSArray *words = [file componentsSeparatedByString:@"\n"];
        
        // Go through the array one string at a time
        int indexN = 1;
        for (NSString *n in words) {

            // Execute until last object (nil) is reached, to avoid checking character case and generate an error
            if (indexN < [words count]) {

                // If the first letter of the word is uppercase, it is a proper name so we can proceed
                BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[n characterAtIndex:0]];
                if (isUppercase) {
                    
                    // Go through the array one string at a time AGAIN
                    int indexM = 1;
                    for (NSString *m in words) {
                        
                        // Execute until last object (nil) is reached, to avoid checking character case and generate an error
                        if (indexM < [words count]) {

                            // If the first letter of the word is lowercase, it is a common noun so we can proceed
                            BOOL isLowercase = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:[m characterAtIndex:0]];
                            if (isLowercase) {
                                
                                // If both words are equal, display them
                                if ([n caseInsensitiveCompare:m] == NSOrderedSame) {
                                    NSLog(@"%@ : %@",n,m);
                                    
                                    // Leave the loop if two similar words were found
                                    break;
                                }
                            }
                        }
                        indexM++;
                    }
                }
            }
            indexN++;
        }
    }
    return 0;
}

Many thanks for your input,
Seb :wink: