Crossword+exercise

Exercise for 2011.12.5

A cheap-shot for showing several versions from independent people: paste each new version in a new code widget. Jeff: Rik moved yours to the bottom. But this wiki does keep me guessing too: I'll put further versions on Crossword2 With luck, as you read down the page, you will see the program improve:

Rubin's original: Note that right out of the gate, he made the decision to read the data in, which makes a huge difference in performance. code format="c" $ cat xword.c
 * 1) include 
 * 2) include 

main{ FILE *File; char Words[100000][30]; int ix=0;

File=fopen("/usr/share/dict/american-english","r"); printf("My file = %i\n",File);

while (fgets(Words[ix],30,File)!=0) ix++; printf("word ->=%s=\n",Words[0]); printf("word ->=%s=\n",Words[1]); printf("word ->=%s=\n",Words[500]); }

// start modestly... // find word pairs where the 7th of 9 letters in the vertical word is the // same as the 5th of 8 letters of the horizontal word // use /usr/share/dict/american-english // // step-wise improvements... // parameterize: letter mix of mlen = nix of nlen // // Omit proper nouns // Omit possessives: A zircon's shape is a cube. // last few entries which do not look like regular word? // // migrate common code out of loops // // make it fast: //     use data structures to prepare for answering MANY such questions

code

Rik's version which omits proper nouns and uses an array of pointers to malloc'd strings. code format="c" cat xw2.c
 * 1) include 
 * 2) include 
 * 3) include 

char *words[100000];   // This pair is a de-facto structure int wordCount=0;         // An OrderedCollection would be the data structure of choice

main{ FILE *inFile; char bufr[30]; int sz;

inFile=fopen("/usr/share/dict/american-english","r"); printf("My file = %i\n",inFile);

while (fgets(bufr,30,inFile)!=0) { if (!isupper (bufr[0]) ) { bufr[(sz = strlen(bufr)) -1] = '\0'; words[wordCount] = malloc(sz); strcpy(words[wordCount], bufr); wordCount++; }       }    printf("Read %i words\n", wordCount); printWord(0); printWord(1); printWord(500); printWord(wordCount-1); }

printWord(int ix) { printf("word[%i]=\"%s\"\n", ix, words[ix]); }

code

Jeff: Rik has another version to paste in shortly... my connection went goofy Monday aft. -Rik This version, with just a couple of tests, shows that one of the next "issues" is handling potentially bulky output. code format="c"
 * 1) include 
 * 2) include 
 * 3) include 

char *words[100000];   // This pair is a de-facto structure int wordCount=0;         // An OrderedCollection would be the data structure of choice

printWord(int ix) { printf("word[%i]=\"%s\"\n", ix, words[ix]); }

int findAny(char seek, int lix, int wordLen) { // answer the index of the first one found, -1 if none int ix; for(ix=0; ix<wordCount; ix++) { if(strlen(words[ix]) == wordLen) { // printf("ix=%i len=%i lix=%i seek=%c word=%s.\n",ix, wordLen, lix, seek, words[ix]); if ((words[ix])[lix] == seek) return(ix); }   }    return(-1); }

printAllSuchThat(char seek, int lix, int wordLen) { // use when at least one has been found int ix, cnt=1; for(ix=0; ix 0) { if (findAny(ch,lix2,wordLen2) > 0) { printf("%i/%i :: %i/%i  :: %c\n", lix1, wordLen1, lix2, wordLen2, ch); printAllSuchThat(ch,lix1,wordLen1); printAllSuchThat(ch,lix2,wordLen2); }       }    } }

main{ FILE *inFile; char bufr[30]; int sz, fix;

inFile=fopen("/usr/share/dict/american-english","r"); printf("My file = %i\n",inFile);

while (fgets(bufr,30,inFile)!=0) { if (!isupper (bufr[0]) ) { bufr[(sz = strlen(bufr)) -1] = '\0'; words[wordCount] = malloc(sz); strcpy(words[wordCount], bufr); wordCount++; }   }    printf("Read %i words\n", wordCount); printWord(0); printWord(1); printWord(500); printWord(wordCount-1);

fix = findAny('b', 3, 5); printf("fix=%i ", fix); if (fix > 0) printWord(fix);

fix = findAny('x', 2, 5); printf("fix=%i ", fix); if (fix > 0) printAllSuchThat('x', 2, 5);

findCrosses(1,11, 9,13); } code Jeff's remove length except 8 or 9 code //Sorry it's where it should be, the wiki let me put it anywhere else when I tried


 * 1) include 
 * 2) include 
 * 3) include 

char *words[100000];   // This pair is a de-facto structure int wordCount=0;         // An OrderedCollection would be the data structure of choice

main{ FILE *inFile; char bufr[30]; int sz, ix;

inFile=fopen("/usr/share/dict/american-english","r"); printf("My file = %i\n",inFile);

while (fgets(bufr,30,inFile)!=0) { if(!isupper (bufr[0])) { bufr[(sz = strlen(bufr)) -1] = '\0'; words[wordCount] = malloc(sz); if (strlen(bufr) == 8 || strlen(bufr)== 9) strcpy(words[wordCount], bufr); wordCount++; printWord(wordCount-1); }       }        ix++; }   printf("Read %i words\n", wordCount); printWord(0); printWord(1); printWord(500); printWord(wordCount-1); }

printWord(int ix) { printf("word[%i]=\"%s\"\n", ix, words[ix]); }

// start modestly... // find word pairs where the 7th of 9 letters in the vertical word is the // same as the 5th of 8 letters of the horizontal word // use /usr/share/dict/american-english // // step-wise improvements... // parameterize: letter mix of mlen = nix of nlen // // + Omit proper nouns (done: omitted capitalized words) // Omit possessives: A zircon's shape is a cube. // last few entries which do not look like regular word? Keep those: letters w/ accents as in etude // // migrate common code out of loops // // make it fast: //     use data structures to prepare for answering MANY such questions code placeholder ... this wiki sometimes makes it tricky to insert a line among existing lines or after a code section. placeholder ... this wiki sometimes makes it tricky to insert a line among existing lines or after a code section.