Here comes the all nighter!Ok I have 15 hours to write a program in C (conforming to C89) that iterates over a list of words up to 1 million entries long, counting the number of times each word occurs.then print the words that occur at least a certain number of times.Also no memory leaks and has to execute in less than 10 seconds so no n^2 checking.If Input is:10 1abracadabraxylophonefnurdleabracadabraoxcartxylophoneaaazzzfnurdleoxcartOutput should be:aaa: 1abracadabra: 2fnurdle: 2oxcart: 2xylophone: 2zzz: 1I've already started, and I'm not feeling too bad about it, should be able to crack it out by 9am.Watch this thead for the hilarity that is sleep deprivation!Girlfriend bringing dinner soon (keeper)
Oh bro. I think I had that one. Not so hard, except I didn't have all the extra (time) constraints on it.PS. C89 is the business. Anything younger is full of shit.Pretty much 2 simple rules:Declare your variables before any logic.No inline comments (//)Though to be fair though I probably couldn't do it these days, as I've become far too pussywhipped by interpreted languages that have loose type checking, so most of that shit is inbuilt.Build a hash tree manually and you'll be fine (what most modern languages do automagically).PPS. Such a keeper.PPPS. Especially if she clears up afterwards too...
/* Questions 4 and 5 for Assignment Quiz 4 * * Takes a list of words from standard input and prints the number of times each word * appears above the given threshold. * * 20/8/2012 */#include <stdio.h>#include <string.h>#include <malloc.h>#define MAX_LINE_LENGTH 22typedef struct word Word;struct word { char *wrd; int count; Word *left; Word *right;};/*As in Lab 6, with StructExample4 the char array needs to be free'd seperately */void freeWord(Word *wrd){ free(wrd->wrd); free(wrd);}/* newWord is effectively the node, housing the count and the pointers to the left * and right * * Returns a word */Word *newWord(const char *wrd){ size_t wrdLength = strlen(wrd); Word *word = malloc(sizeof(Word)); if (word != NULL && (word->wrd = malloc(wrdLength + 1)) != NULL){ strncpy(word->wrd, wrd, wrdLength + 1); word->left = NULL; word->right = NULL; word->count = 1; /* If you're calling it then it exists */ } return word;}/* insert tests whether to insert the node, or to 1 up it's existing value * * returns the top of the binary tree */Word *insert(Word *wrd, Word *tree){ return tree;}/* Reads one line, finds the newline character and clobbers it * * returns a single word */Word *readWord(){ Word *sp = NULL; int wordLength = 0; char buff[MAX_LINE_LENGTH] = {'\0'}; char *cp = fgets(buff, MAX_LINE_LENGTH, stdin); if(cp != NULL){ wordLength = strcspn(buff, "\n"); buff[wordLength] = '\0'; sp = newWord(buff); } return sp;}/* Sets the first word in the binary tree * * returns the first word */Word *readWords(){ Word *first = NULL; Word *sp = readWord(); while(sp != NULL){ if(first == NULL){ first = sp; } else{ first = insert(sp, first); } sp = readWord(); } return first;}/* Recursion method of printing each node, also note, half the memory is free'd up here*/void printWords(Word *wrd, int threshold){ }}/* Gathers information and makes required calls */void processInput(){ int wordNum = 0; int threshold = 0; scanf("%d %d\n", &wordNum, &threshold); /* WordNum not used */ Word *wordList = readWords(); printWords(wordList, threshold);}int main() { int allocatedBlocks = 0; int leakedBlocks = 0; allocatedBlocks = mallinfo().uordblks + mallinfo().hblkhd; processInput(); leakedBlocks = mallinfo().uordblks + mallinfo().hblkhd - allocatedBlocks; if (leakedBlocks != 0) { printf("Memory leak of %d bytes detected!\n", leakedBlocks); } return 0;}
Took a pre-workout supp. last night.Shit was awesome.Except now I feel like a zombie. 3hrs sleep.Braiiiinnnnnnnzzzzzzz.
i had kinda hope speakman had died, what a pity
I finish my current job this week, get a whole week off before I start my new job.I'll probably kill myself from boredom next week