One program to the challenge #2 that lists all solutions


#1

Follow my source code to solve challenge #2:

[code]//
// main.m
// ProperNamesAndWords
//
// Created by Rogério Moraes de Carvalho on 13/05/2012.
//

#import <Foundation/Foundation.h>

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

@autoreleasepool {
	
	// Getting proper names.
	NSString *properNamesString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
															encoding:NSUTF8StringEncoding
															   error:nil];
	NSArray *properNamesList = [properNamesString componentsSeparatedByString:@"\n"];
	NSUInteger numberOfProperNames = [properNamesList count];
	NSLog(@"Number of proper names: %lu.", numberOfProperNames);
	
	// Getting words.
	NSString *wordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
													  encoding:NSUTF8StringEncoding
														 error:nil];
	NSArray *wordsList = [wordsString componentsSeparatedByString:@"\n"];
	NSUInteger numberOfWords = [wordsList count];
	NSLog(@"Number of words: %lu.", numberOfWords);
	
	// Searching for proper names that are words too.
	NSMutableArray *properNamesAndWordsList = [NSMutableArray array];
	
	int indexProperName = 0;
	int indexWord = 0;
	NSString *currentProperName, *currentWord, *nextWord;
	
	while (indexProperName < numberOfProperNames) {
		currentProperName = [properNamesList objectAtIndex:indexProperName++];
		
		do {
			currentWord = [wordsList objectAtIndex:indexWord++];
		} while ([currentWord compare:currentProperName options:NSCaseInsensitiveSearch] == NSOrderedAscending &&
				 indexWord < numberOfWords);
		
		nextWord = indexWord < numberOfWords ? [wordsList objectAtIndex:indexWord++] : nil;
		if ([currentProperName isEqualToString:currentWord] &&
			[currentWord compare:nextWord options:NSCaseInsensitiveSearch] == NSOrderedSame) {
			[properNamesAndWordsList addObject:[NSString stringWithFormat:@"%@, %@", currentWord, nextWord]];
		}
	}
	
	NSUInteger numberOfProperNamesAndWords = [properNamesAndWordsList count];
	NSLog(@"Number of proper names that are words too: %lu.", numberOfProperNamesAndWords);
	
	// Print the list of proper names that are words too.
	NSLog(@"<<<<< Proper names that are words too >>>>>");
	for (int index = 0; index < numberOfProperNamesAndWords; index++) {
		NSLog(@"%4d. %@", index + 1, [properNamesAndWordsList objectAtIndex:index]);
	}
}
return 0;

}[/code]

Follow the output from the execution of the program above:

Number of proper names: 1309. Number of words: 235887. Number of proper names that are words too: 279. <<<<< Proper names that are words too >>>>> 1. Al, al 2. Alan, alan 3. Alf, alf 4. Alison, alison 5. Allan, allan 6. Ami, ami 7. Amir, amir 8. Amy, amy 9. Ann, ann 10. Anna, anna 11. Ariel, ariel 12. Art, art 13. Barney, barney 14. Barrio, barrio 15. Barry, barry 16. Barton, barton 17. Ben, ben 18. Benjamin, benjamin 19. Beth, beth 20. Betty, betty 21. Bill, bill 22. Billy, billy 23. Blake, blake 24. Bob, bob 25. Bobby, bobby 26. Brad, brad 27. Brandy, brandy 28. Bret, bret 29. Brett, brett 30. Bucky, bucky 31. Bud, bud 32. Butler, butler 33. Carl, carl 34. Carol, carol 35. Caroline, caroline 36. Carter, carter 37. Case, case 38. Celeste, celeste 39. Chip, chip 40. Chuck, chuck 41. Clark, clark 42. Clay, clay 43. Clem, clem 44. Cliff, cliff 45. Cole, cole 46. Colin, colin 47. Collin, collin 48. Curt, curt 49. Cyrus, cyrus 50. Dale, dale 51. Dalton, dalton 52. Dan, dan 53. Dawn, dawn 54. Dean, dean 55. Deb, deb 56. Dick, dick 57. Dieter, dieter 58. Dirk, dirk 59. Don, don 60. Dory, dory 61. Drew, drew 62. Duke, duke 63. Earl, earl 64. Eddy, eddy 65. Elaine, elaine 66. Emma, emma 67. Eric, eric 68. Fay, fay 69. Fletcher, fletcher 70. Frank, frank 71. Franklin, franklin 72. Gale, gale 73. Gene, gene 74. Gill, gill 75. Ginny, ginny 76. Glen, glen 77. Grace, grace 78. Graham, graham 79. Grant, grant 80. Griff, griff 81. Gunter, gunter 82. Guy, guy 83. Gypsy, gypsy 84. Hank, hank 85. Harry, harry 86. Hazel, hazel 87. Heather, heather 88. Hector, hector 89. Henry, henry 90. Herb, herb 91. Holly, holly 92. Horst, horst 93. Jack, jack 94. Jane, jane 95. Jarl, jarl 96. Jay, jay 97. Jean, jean 98. Jimmy, jimmy 99. Jinny, jinny 100. Joe, joe 101. Jordan, jordan 102. Josh, josh 103. Judge, judge 104. June, june 105. Kay, kay 106. Kelly, kelly 107. Kelvin, kelvin 108. Ken, ken 109. Kent, kent 110. Kerry, kerry 111. Kiki, kiki 112. Kim, kim 113. Kinch, kinch 114. King, king 115. Kirk, kirk 116. Kit, kit 117. Kitty, kitty 118. Knapper, knapper 119. Knut, knut 120. Kusum, kusum 121. Kyle, kyle 122. Lance, lance 123. Lar, lar 124. Larry, larry 125. Laura, laura 126. Laurel, laurel 127. Lea, lea 128. Lee, lee 129. Lewis, lewis 130. Liber, liber 131. Lin, lin 132. Linder, linder 133. List, list 134. Lord, lord 135. Lori, lori 136. Lum, lum 137. Mac, mac 138. Major, major 139. Manny, manny 140. Marc, marc 141. Marcel, marcel 142. Marco, marco 143. Marguerite, marguerite 144. Maria, maria 145. Mark, mark 146. Marsh, marsh 147. Martin, martin 148. Mary, mary 149. Mason, mason 150. Mat, mat 151. Mick, mick 152. Mike, mike 153. Milner, milner 154. Milo, milo 155. Molly, molly 156. Morgan, morgan 157. Morris, morris 158. Mott, mott 159. Myron, myron 160. Nancy, nancy 161. Neal, neal 162. No, no 163. Norm, norm 164. Norma, norma 165. Old, old 166. Pablo, pablo 167. Page, page 168. Pam, pam 169. Panacea, panacea 170. Pandora, pandora 171. Part, part 172. Pat, pat 173. Patrice, patrice 174. Patty, patty 175. Pedro, pedro 176. Peggy, peggy 177. Penny, penny 178. Per, per 179. Perry, perry 180. Pete, pete 181. Peter, peter 182. Pia, pia 183. Piet, piet 184. Pilar, pilar 185. Pilot, pilot 186. Ping, ping 187. Plastic, plastic 188. Po, po 189. Price, price 190. Rainer, rainer 191. Raj, raj 192. Raja, raja 193. Ralph, ralph 194. Ram, ram 195. Rand, rand 196. Randy, randy 197. Ray, ray 198. Real, real 199. Rex, rex 200. Rich, rich 201. Rick, rick 202. Rob, rob 203. Robbin, robbin 204. Rod, rod 205. Rodent, rodent 206. Rodney, rodney 207. Roger, roger 208. Rogue, rogue 209. Root, root 210. Ross, ross 211. Rusty, rusty 212. Ruth, ruth 213. Sal, sal 214. Sally, sally 215. Sam, sam 216. Sandy, sandy 217. Sanity, sanity 218. Scot, scot 219. Sergeant, sergeant 220. Seth, seth 221. Shadow, shadow 222. Shatter, shatter 223. Shaw, shaw 224. Sho, sho 225. Siping, siping 226. Sir, sir 227. Skeeter, skeeter 228. Skip, skip 229. Son, son 230. Sonny, sonny 231. Sorrel, sorrel 232. Space, space 233. Spencer, spencer 234. Spike, spike 235. Spy, spy 236. Sri, sri 237. Stagger, stagger 238. Steen, steen 239. Steven, steven 240. Sue, sue 241. Sylvan, sylvan 242. Tad, tad 243. Tai, tai 244. Tait, tait 245. Tal, tal 246. Tammy, tammy 247. Tao, tao 248. Ted, ted 249. Terry, terry 250. The, the 251. Think, think 252. Those, those 253. Ti, ti 254. Tigger, tigger 255. Timothy, timothy 256. Toby, toby 257. Toft, toft 258. Tolerant, tolerant 259. Tommy, tommy 260. Tony, tony 261. Tor, tor 262. Travis, travis 263. Trey, trey 264. Troy, troy 265. Trying, trying 266. Tuan, tuan 267. Tuna, tuna 268. Van, van 269. Vice, vice 270. Victor, victor 271. Wade, wade 272. Walt, walt 273. Walter, walter 274. Warren, warren 275. Will, will 276. Willie, willie 277. Win, win 278. Wolf, wolf 279. Woody, woody


#2

“Number of proper names” and “Number of words” are each off by one. There’s a blank line at the end of each of the two text files that’s causing a blank object at the end of each of these arrays.