Challenges

Create a new Foundation Command Line Tool named Groceries. Start by creating an empty NSMutableArray object. Then add several grocery-like items to the array. (You’ll have to create those, too.) Finally, use fast enumeration to print out your grocery list.

This next challenge is, well, more challenging. Read through the following program, which finds common proper names that contain two adjacent A’s.

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​R​e​a​d​ ​i​n​ ​a​ ​f​i​l​e​ ​a​s​ ​a​ ​h​u​g​e​ ​s​t​r​i​n​g​ ​(​i​g​n​o​r​i​n​g​ ​t​h​e​ ​p​o​s​s​i​b​i​l​i​t​y​ ​o​f​ ​a​n​ ​e​r​r​o​r​)​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​n​a​m​e​S​t​r​i​n​g​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ ​[​N​S​S​t​r​i​n​g​ ​s​t​r​i​n​g​W​i​t​h​C​o​n​t​e​n​t​s​O​f​F​i​l​e​:​@​"​/​u​s​r​/​s​h​a​r​e​/​d​i​c​t​/​p​r​o​p​e​r​n​a​m​e​s​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​n​c​o​d​i​n​g​:​N​S​U​T​F​8​S​t​r​i​n​g​E​n​c​o​d​i​n​g​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​r​r​o​r​:​N​U​L​L​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​B​r​e​a​k​ ​i​t​ ​i​n​t​o​ ​a​n​ ​a​r​r​a​y​ ​o​f​ ​s​t​r​i​n​g​s​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​n​a​m​e​s​ ​=​ ​[​n​a​m​e​S​t​r​i​n​g​ ​c​o​m​p​o​n​e​n​t​s​S​e​p​a​r​a​t​e​d​B​y​S​t​r​i​n​g​:​@​"​​n​"​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​o​ ​t​h​r​o​u​g​h​ ​t​h​e​ ​a​r​r​a​y​ ​o​n​e​ ​s​t​r​i​n​g​ ​a​t​ ​a​ ​t​i​m​e​
 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​N​S​S​t​r​i​n​g​ ​*​n​ ​i​n​ ​n​a​m​e​s​)​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​L​o​o​k​ ​f​o​r​ ​t​h​e​ ​s​t​r​i​n​g​ ​"​a​a​"​ ​i​n​ ​a​ ​c​a​s​e​-​i​n​s​e​n​s​i​t​i​v​e​ ​m​a​n​n​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​R​a​n​g​e​ ​r​ ​=​ ​[​n​ ​r​a​n​g​e​O​f​S​t​r​i​n​g​:​@​"​A​A​"​ ​o​p​t​i​o​n​s​:​N​S​C​a​s​e​I​n​s​e​n​s​i​t​i​v​e​S​e​a​r​c​h​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​W​a​s​ ​i​t​ ​f​o​u​n​d​?​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​r​.​l​o​c​a​t​i​o​n​ ​!​=​ ​N​S​N​o​t​F​o​u​n​d​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​n​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​}​
 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

The file /usr/share/dict/propernames contains common proper names. The file/usr/share/dict/words contains regular words (not proper names). Now write a program based on the one above that finds common proper names that are also regular words. For example, Glen is a guy’s name, and glen is a narrow valley.

When a computer orders strings, it typically considers uppercase letters as coming before lowercase letters. To do a comparison that ignores the case, use the method caseInsensitiveCompare:.

N​S​S​t​r​i​n​g​ ​*​a​ ​=​ ​@​"​A​B​C​"​;​
N​S​S​t​r​i​n​g​ ​*​b​ ​=​ ​@​"​a​b​c​"​;​
i​f​ ​(​[​a​ ​c​a​s​e​I​n​s​e​n​s​i​t​i​v​e​C​o​m​p​a​r​e​:​b​]​ ​=​=​ ​N​S​O​r​d​e​r​e​d​S​a​m​e​)​ ​{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​a​ ​a​n​d​ ​b​ ​a​r​e​ ​e​q​u​a​l​"​)​;​
}​

i​f​ ​(​[​a​ ​c​a​s​e​I​n​s​e​n​s​i​t​i​v​e​C​o​m​p​a​r​e​:​b​]​ ​=​=​ ​N​S​O​r​d​e​r​e​d​A​s​c​e​n​d​i​n​g​)​ ​{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​a​ ​c​o​m​e​s​ ​b​e​f​o​r​e​ ​b​"​)​;​
}​

i​f​ ​(​[​a​ ​c​a​s​e​I​n​s​e​n​s​i​t​i​v​e​C​o​m​p​a​r​e​:​b​]​ ​=​=​ ​N​S​O​r​d​e​r​e​d​D​e​s​c​e​n​d​i​n​g​)​ ​{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​b​ ​c​o​m​e​s​ ​b​e​f​o​r​e​ ​a​"​)​;​
}​

In this challenge, use caseInsensitiveCompare: to see that Glen and glen are equivalent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset