Challenge 2 Solution (lowercasing names, localizedCompare)


#1

IGNORE MY SOLUTION.
It’s alright but too complicated and runs slow.
Here is the best one I’ve found in this thread:
viewtopic.php?f=453&t=8556&p=25964#p25964

My solution.
A fairly simple one.
To be honest, I bounced around documentation and stumbled upon localizedCompare method. It worked for me.

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

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

@autoreleasepool {
    //Processing Names
    //Read in a file as a huge string (ignoring the possibility of an error)
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
    //Break it into an array of strings
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    
    
    //Processing Words
    NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
    //Break it into an array of strings
    NSArray *words = [wordString componentsSeparatedByString:@"\n"];
    
    
   //creating a count value based on the number of values in an array
    NSUInteger nameCount = [names count];
   
    //looping through accordingly - it will loop as many times as there are names in the system
    for (int i = 0; i < nameCount; i++){
      
        //converting name to a lowercase string
        NSString *n = [names[i] lowercaseString];
        
        //for each word
        for (NSString *w in words) {
            
            
            //loop through the name and compare it with a word
            //however, we want the case to match
            //and given that the name will always start with a capital letter
            //we will convert names to a lowercase before comparing it to a word
            
            NSComparisonResult comparisonResult = [n localizedCompare:w];
            if (comparisonResult == NSOrderedSame) {
                
                //important is names[i] because we want to display proper cases
                NSLog(@"Name \"%@\" matches word (lowercase) \"%@\"", names[i], w);
            }
            
        }
    }
    
    
    }

return 0;

}

[/code]


#2

Here is my output (I’ve spot-checked it by pulling up two files to make sure everything is correct, let me know if my output has errors):

2014-08-06 08:37:37.271 InterestingNames[1602:303] Name "Al" matches word(lowercase) "al" 2014-08-06 08:37:37.372 InterestingNames[1602:303] Name "Alan" matches word(lowercase) "alan" 2014-08-06 08:37:37.779 InterestingNames[1602:303] Name "Alf" matches word(lowercase) "alf" 2014-08-06 08:37:37.887 InterestingNames[1602:303] Name "Alison" matches word(lowercase) "alison" 2014-08-06 08:37:37.938 InterestingNames[1602:303] Name "Allan" matches word(lowercase) "allan" 2014-08-06 08:37:38.246 InterestingNames[1602:303] Name "Ami" matches word(lowercase) "ami" 2014-08-06 08:37:38.353 InterestingNames[1602:303] Name "Amir" matches word(lowercase) "amir" 2014-08-06 08:37:38.454 InterestingNames[1602:303] Name "Amy" matches word(lowercase) "amy" 2014-08-06 08:37:39.130 InterestingNames[1602:303] Name "Ann" matches word(lowercase) "ann" 2014-08-06 08:37:39.181 InterestingNames[1602:303] Name "Anna" matches word(lowercase) "anna" 2014-08-06 08:37:39.690 InterestingNames[1602:303] Name "Ariel" matches word(lowercase) "ariel" 2014-08-06 08:37:39.896 InterestingNames[1602:303] Name "Art" matches word(lowercase) "art" 2014-08-06 08:37:40.255 InterestingNames[1602:303] Name "Barney" matches word(lowercase) "barney" 2014-08-06 08:37:40.357 InterestingNames[1602:303] Name "Barrio" matches word(lowercase) "barrio" 2014-08-06 08:37:40.407 InterestingNames[1602:303] Name "Barry" matches word(lowercase) "barry" 2014-08-06 08:37:40.517 InterestingNames[1602:303] Name "Barton" matches word(lowercase) "barton" 2014-08-06 08:37:40.770 InterestingNames[1602:303] Name "Ben" matches word(lowercase) "ben" 2014-08-06 08:37:40.821 InterestingNames[1602:303] Name "Benjamin" matches word(lowercase) "benjamin" 2014-08-06 08:37:41.127 InterestingNames[1602:303] Name "Beth" matches word(lowercase) "beth" 2014-08-06 08:37:41.230 InterestingNames[1602:303] Name "Betty" matches word(lowercase) "betty" 2014-08-06 08:37:41.330 InterestingNames[1602:303] Name "Bill" matches word(lowercase) "bill" 2014-08-06 08:37:41.432 InterestingNames[1602:303] Name "Billy" matches word(lowercase) "billy" 2014-08-06 08:37:41.583 InterestingNames[1602:303] Name "Blair" matches word(lowercase) "blair" 2014-08-06 08:37:41.633 InterestingNames[1602:303] Name "Blake" matches word(lowercase) "blake" 2014-08-06 08:37:41.743 InterestingNames[1602:303] Name "Bob" matches word(lowercase) "bob" 2014-08-06 08:37:41.850 InterestingNames[1602:303] Name "Bobby" matches word(lowercase) "bobby" 2014-08-06 08:37:42.053 InterestingNames[1602:303] Name "Brad" matches word(lowercase) "brad" 2014-08-06 08:37:42.312 InterestingNames[1602:303] Name "Brandy" matches word(lowercase) "brandy" 2014-08-06 08:37:42.532 InterestingNames[1602:303] Name "Brent" matches word(lowercase) "brent" 2014-08-06 08:37:42.589 InterestingNames[1602:303] Name "Bret" matches word(lowercase) "bret" 2014-08-06 08:37:42.647 InterestingNames[1602:303] Name "Brett" matches word(lowercase) "brett" 2014-08-06 08:37:43.123 InterestingNames[1602:303] Name "Bucky" matches word(lowercase) "bucky" 2014-08-06 08:37:43.181 InterestingNames[1602:303] Name "Bud" matches word(lowercase) "bud" 2014-08-06 08:37:43.233 InterestingNames[1602:303] Name "Butler" matches word(lowercase) "butler" 2014-08-06 08:37:43.490 InterestingNames[1602:303] Name "Carl" matches word(lowercase) "carl" 2014-08-06 08:37:43.641 InterestingNames[1602:303] Name "Carol" matches word(lowercase) "carol" 2014-08-06 08:37:43.749 InterestingNames[1602:303] Name "Caroline" matches word(lowercase) "caroline" 2014-08-06 08:37:43.903 InterestingNames[1602:303] Name "Carter" matches word(lowercase) "carter" 2014-08-06 08:37:44.008 InterestingNames[1602:303] Name "Case" matches word(lowercase) "case" 2014-08-06 08:37:44.417 InterestingNames[1602:303] Name "Celeste" matches word(lowercase) "celeste" 2014-08-06 08:37:44.824 InterestingNames[1602:303] Name "Chip" matches word(lowercase) "chip"


#3

Vitality,

I think the reason for you running too slowly, is the fact you are beginning the search for word items each time time you are looking for a new name item.

if you keep hold of the search index for the word items array, you can restart the search from the index position.

Note: this assumes both arrays are sorted into alphabetical order.