Question
Hello! My program is not passing runTestCases but my answers seem to be the same as correct answers. At first, I thought the problem was
Hello! My program is not passing runTestCases but my answers seem to be the same as correct answers. At first, I thought the problem was white spacing but runTestCases has a case for it. I'm not sure what is the problem.
=====================
My program:
/* This program indexes a web page, printing out the counts of words on that page */ #include
//memory leak /* run: valgrind --leak-check=full -v ./indexPage */
typedef struct trie { int countWord; int countNode; //count the number node char letter; struct trie ** node; //arr nodes }trie;
trie* indexPage(const char* url); int addWordOccurrence(const char* word, const int wordLength, trie* root); char* printTrieContents(trie root, char* buffer); int freeTrieMemory(trie* root); int getText(const char* srcAddr, char* buffer, const int bufSize);
int main(int argc, char** argv){ trie* root; root = indexPage(argv[1]); //error here char* buffer = malloc(10); buffer[0] = '\0'; buffer = printTrieContents(*root, buffer); //free 'em all; I keep having memo leaks; make sure no problems this time free(buffer); freeTrieMemory(root); //exit program return 0; }
trie* indexPage(const char* url){ trie * root; //p root = malloc(sizeof(trie)); //malloc here since memo problems root->countWord = 0; root->countNode = 0; root->letter = ' '; root->node = NULL; char resultBuffer[300000]; //webpage contents getText(url, resultBuffer, 300000); printf("%s ", url); //error here int i = 0; //resultBuffer while(i
int addWordOccurrence(const char* word, const int wordLength, trie* root){ int i = 0; int result = 0; int nodeI; //specific node //++, return 1 if ((*word == root->letter) && (wordLength == 1)){ root->countWord++; return 1; } if (root->letter == ' ' && root->countNode == 0){ //skip; does nothing } //check nodes to current letter word, pass current word and length; match else if (root->letter == ' '){ for(nodeI = 0; nodeI countNode; nodeI++){ if(*word == (root->node[nodeI])->letter && wordLength > 0){ result = addWordOccurrence((word), (wordLength), root->node[nodeI]); if (result == 1){ return 1; } } } } else{ //if current match, check next letter node, pass the word less current letter and length-1 match for(nodeI = 0; nodeI countNode; nodeI++){ if(*(word+1) == (root->node[nodeI])->letter && wordLength > 0 && result == 0){ result = addWordOccurrence((word+1), (wordLength-1), root->node[nodeI]); if (result == 1){ return 1; } } } } //can i just make one new struct trie instead of making one in if and else if (root->letter == ' '){ struct trie* newNode; root->countNode++; root->node = realloc(root->node, sizeof(trie) * root->countNode); // memo problems newNode = malloc(sizeof(trie)); // memo problems newNode->countWord = 0; newNode->countNode = 0; newNode->node = NULL; newNode->letter = word[0]; root->node[root->countNode-1] = newNode; result = addWordOccurrence((word),(wordLength), newNode); if (result == 1){ return 1; } } else{ struct trie* newNode; root->countNode++; root->node = realloc(root->node, sizeof(trie) * root->countNode); newNode = malloc(sizeof(trie)); newNode->countWord = 0; newNode->countNode = 0; newNode->node = NULL; newNode->letter = word[1]; root->node[root->countNode-1] = newNode; result = addWordOccurrence((word+1), (wordLength-1), newNode); if (result == 1){ return 1; } } return result; }
char* printTrieContents(trie root, char* buffer){ int length; if (root.letter != ' ' && root.letter != '\0'){ length = strlen(buffer) + 2; buffer = realloc(buffer,sizeof(char) * length); buffer[length-2] = root.letter; buffer[length-1] = '\0'; //word count > 0 if (root.countWord > 0 && strlen(buffer) > 0){ printf("%s: %d ", buffer, root.countWord); } } if (root.countNode > 0){ int i; trie* temp = NULL; //sort for(i = 0; i letter > root.node[j]->letter){ temp = root.node[i]; root.node[i] = root.node[j]; root.node[j] = temp; } } } //loop node in node array for (i = 0; i
int freeTrieMemory(trie* root){ while (root->countNode > 0){ if (freeTrieMemory((root->node[root->countNode-1])) == 1){ root->countNode--; } else{ printf("this should not happen but if it does... congradz you have error"); } } char letter = root->letter; //free 'em all' free(root->node); free(root); return 1; }
int getText(const char* srcAddr, char* buffer, const int bufSize){ FILE *pipe; int bytesRead;
snprintf(buffer, bufSize, "curl -s \"%s\" | python getText.py", srcAddr);
pipe = popen(buffer, "r"); if(pipe == NULL){ fprintf(stderr, "ERROR: could not open the pipe for command %s ", buffer); return 0; }
bytesRead = fread(buffer, sizeof(char), bufSize-1, pipe); buffer[bytesRead] = '\0';
pclose(pipe);
return bytesRead; }
===========================
runTestCases.sh.dat
#!/bin/bash
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
do
if [[ ${i} -lt 10 ]]
then
url="http://users.ipfw.edu/chenz/testweb/page_00000${i}.html"
else
url="http://users.ipfw.edu/chenz/testweb/page_0000${i}.html"
fi
./indexPage ${url} > test${i}Output.txt
./solution ${url} > test${i}CorrectOutput.txt
diff -q test${i}CorrectOutput.txt test${i}Output.txt > /devull
if [[ $? == 0 ]]
then
echo Test ${i} was passed successfully.
rm test${i}Output.txt test${i}CorrectOutput.txt
else
diff -q -b test${i}CorrectOutput.txt test${i}Output.txt > /devull
if [[ $? == 0 ]]
then
echo Test ${i} differed in its whitespace.
else
echo Test ${i} failed.
fi
echo "Here is a side-by-side comparison for test ${i}, with the correct output on the left, showing only the mismatched lines:"
diff -y --suppress-common-lines test${i}CorrectOutput.txt test${i}Output.txt
fi
done
:/workspace/PROJECT 3 $./runTestCases.sh.dat Test 0 failed Here is a side-by-side comparison for test 0, with the correct output on the left, showing only the mismatched lines: Sport sports sports sports sports Sport sports highlights sports sports l page l sport l sports l sports l sports l sports l sport l sports I highlights l sports sports Test 1 failed Here is a side-by-side comparison for test 1, with the correct output on the left, showing only the mismatched lines: page other sport link to other l sport | link sport link to l page l sport | link l page link to l page link to l page other other link to page link :/workspace/PROJECT 3 $./runTestCases.sh.dat Test 0 failed Here is a side-by-side comparison for test 0, with the correct output on the left, showing only the mismatched lines: Sport sports sports sports sports Sport sports highlights sports sports l page l sport l sports l sports l sports l sports l sport l sports I highlights l sports sports Test 1 failed Here is a side-by-side comparison for test 1, with the correct output on the left, showing only the mismatched lines: page other sport link to other l sport | link sport link to l page l sport | link l page link to l page link to l page other other link to page linkStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started