Solution to Challenge 15


#1

Hi everybody !

This is my solution to Challenge 15 (the one at the end of the NSArray topic) it’s 100% correct.

I hope you like it.

[code]#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    // Read in a file as a hugh string (ignoring the possibility of an error)
    NSString *properNames
        = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                    encoding:NSUTF8StringEncoding
                                       error:NULL];
    
    NSString *words
        = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                    encoding:NSUTF8StringEncoding
                                       error:NULL];
    
    // Break it into an array of strings
    NSArray *properNamesArray   = [properNames componentsSeparatedByString:@"\n"];
    NSArray *wordsArray         = [words componentsSeparatedByString:@"\n"];

    
    // Go through the array one string at a time
    
    for (NSString *properNamesEntry in properNamesArray){
        
         for (NSString *wordsEntry in wordsArray){
             
             if ([properNamesEntry caseInsensitiveCompare:wordsEntry] == NSOrderedSame){

                    if ([properNamesEntry compare:wordsEntry options:NSLiteralSearch] == NSOrderedAscending){
                        NSLog(@"Proper  Name: %@", properNamesEntry);
                        NSLog(@"Regular Word: %@", wordsEntry);
                        
                        break;
                    }
             }
         }
    }
    
}
return 0;

}[/code]


#2

Except by having the break in there you’re assuming that a word is not duplicated. I allowed mine to keep going to the end of the file in case the same word is encountered later.


#3

But the test was to check if the word is both a proper and non-proper name, thus you can break once you find one.


#4

[quote=“newbiec”]Hi everybody !

This is my solution to Challenge 15 (the one at the end of the NSArray topic) it’s 100% correct.

I hope you like it.

if ([properNamesEntry caseInsensitiveCompare:wordsEntry] == NSOrderedSame){ if ([properNamesEntry compare:wordsEntry options:NSLiteralSearch] == NSOrderedAscending){ [/quote]

my code for this portion of the check was:

if (
                    ([properNamesEntry caseInsensitiveCompare:wordsEntry] == NSOrderedSame) &&
                    (![properNamesEntry isEqualToString:wordsEntry])
                    
                    )

Unsure which is more efficient, or if combining ifs is better than having them separated.


#5

Thank you, your answer is helpful.

[quote=“frotty”][quote=“newbiec”]Hi everybody !

This is my solution to Challenge 15 (the one at the end of the NSArray topic) it’s 100% correct.

I hope you like it.

if ([properNamesEntry caseInsensitiveCompare:wordsEntry] == NSOrderedSame){ if ([properNamesEntry compare:wordsEntry options:NSLiteralSearch] == NSOrderedAscending){ [/quote]

my code for this portion of the check was:

if (
                    ([properNamesEntry caseInsensitiveCompare:wordsEntry] == NSOrderedSame) &&
                    (![properNamesEntry isEqualToString:wordsEntry])
                    
                    )

Unsure which is more efficient, or if combining ifs is better than having them separated.[/quote]