CS 50 Walkthrough 6 Problem Set 6: Mispellings Marta Bralic Slides courtesy of: Keito Uchiyama
Problem Set 6: Mispellings • Topics: – More data structures, more pointers – More File I/O
• You implement: – A dic:onary for a very fast spell checker
The Distribution Code • • • • • •
texts – a symlink speller.c – a spellchecker dic:onary.h – the header file dic:onary.c – a dic:onary implementa:on Makefile ques:ons.txt
What to implement • • • •
load() – loads a dic:onary into memory size() – gets the size of the dic:onary unload() – unloads a dic:onary from memory check() – checks if a given word is in the dic:onary
Your options • Slow but simple: Linear search every :me – don’t do this!
• Hash tables • Tries
Hash Tables
Image courtesy User:Davidgothberg and User:Helix84, Wikimedia Commons
Hash Tables - Operations • • • •
Ini:alizing our hash table Adding dic:onary words Checking words Unloading words
Ini:alizing //Here's how our node is defined typedef struct node { char word[LENGTH + 1]; struct node *next; } node; //We have our main directory of node //pointers node *myarray[ARRAYSIZE]; // for each element i in myarray: // myarray[i] = NULL
Hash Tables – a Hash Function function myHashFunction(string): int hashresult foreach character in string: hashresult += character ‐ 97 return hashresult % ARRAYSIZE
Loading Dic:onary Words • fopen(dict) same as in pset5 • while !feof(dict) – create nodes for them – put these nodes in the hash table
Crea:ng Nodes • malloc space for new node (node *newnodeptr) – store each leYer i of the word in that node • fgetc(dptr) is that leYer • newnodeptr‐>word[i] is where leYer should be stored • un:l you reach ‘\n’ – newnodeptr‐>word[j] = ‘\0’ at this point
Put Node in Hash Table • hash(newnodeptr‐>word) – go to that place in array (array[hashresult]) • if nothing is there (NULL) – put a pointer to your node that you just malloced there – set newnodeptr‐>next to NULL
• else – set newnodeptr‐>next to the pointer currently there – put your pointer there
• when while loop exits, fclose(dptr)
Size Really easy if you’ve kept a counter that you increment every :me you load a word.
Check • convert each leYer of word tolower • hash word and go to that place in array – temporarily store the ptr you find there – using that ptr, traverse the linked list looking for the word in ptr‐>word
Unload • Iterate through each node, like in check – free the node – free the spot in the array that starts the linked list – return true
• run valgrind to ensure no leaks!
Tries
Tries – A struct //Here's an example of what each node //in our struct will look like typedef struct node { bool is_word; struct node *children[27]; } node; //We have our root node node *root;
Tries - Operations • • • •
Ini:alizing our root node Adding words Checking words Unloading words
Tips • Start with a small dic:onary and small text file (speller [dict] file) • Mapping out data structures on paper and on screen • Using gdb
Topics: â More data structures, more pointers. â More File I/O. ⢠You implement: ... convert each leâºer of word tolower. ⢠hash word and go to that place in array.
create nodes for them. â put these nodes ... store each leâºer i of the word in that node. ⢠fgetc(dptr) is that ... put a pointer to your node that you just malloced there.
Slow but simple: Linear search every fme. â don't do this! ⢠Hash tables. ⢠Tries ... easy if you've kept a counter that you increment every fme you load a word.
Data structures, hexadecimal, and pointers. ⢠Programs: â whodunit. â resize. â recover ... Image recovery! ... Go through each block in the disk image and: 1.
Image recovery - Steps. ⢠Steps: Go through each block in the disk image and: 1. If we find a JPEG signature, start wriûng the bytes out to another file. 2. If we find a new JPEG signature, close that old file and go back to 2. 3. If we find the E
This old man, he played one. He played knick-knack on my thumb. Knick-knack paddywhack, give your dog a bone. This old man came rolling home. This old man, he played two. He played knick-knack on my shoe. Knick-knack paddywhack, give your dog a bone.
This old man, he played one. He played knick-knack on my thumb. Knick-knack paddywhack, give your dog a bone. This old man came rolling home. This old ...
index.html â homepage. â« buildings.js â buildings in the game. â« houses.js â Harvard houses + locations. â« math3d.js â movement math. â« passengers.js â all the people in the game. â« service.css â appearance of the homepage. â«
Agenda. â« Distribution Code. â« HTML + CSS. â« Javascript. â« API's: Google Earth and Google Maps. â« Pickup. â« Dropoff. â« Choice of feature ...
Page 1 of 1. File: Form cs l mvr 50 pdf. Download now. Click here if your download doesn't start automatically. Page 1 of 1. form cs l mvr 50 pdf. form cs l mvr 50 ...
To Do. â« distribution code. â« ncurses. â« move cursor. â« allow changing user-added ... Allows you to change colors, ... g.board[g.y][g.x] is spot on board where.
function, takes one argument ch (ascii). â« if ch is 0, . , KEY_BACKSPACE, KEY_DC. â« set that spot in the board to 0. â« if ch is numerical between '1' and '9'.
distribution code. â« ncurses. â« move cursor. â« allow changing user-added numbers, but not original ones. â« allow replacement of blank with number. â« invalid move? â« won? ... Moving the cursor. â« Switch statements! switch (test). { case
d) none of these. Page 3 of 287. Main menu. Displaying West Bengal University B.Tech CS APM Sem 6 2013 Production & Operations Management.pdf. Page 1 ...
Go to middle if k < value at middle search for k between first and the one before the middle if k > value at middle search for k between one after the middle and ...
Feb 8, 2011 - or jQTouch) are not necessary, but you can certainly use more than one page if you'd like to. Page 6. Mobile Local. Walkthrough. Tommy. MacWilliam. Setup. JSONP. YQL. Tips and. Tricks. HTML Setup. â· HTML5 Doctype: . â· jQuery: .
Videos on website. â« Purpose. â« To guide you through the week's assignment ... poor/fair/good/better/best ... Building Blocks. â« printf. â« GetInt(). â« âthinkingâ.
University of Colorado at Boulder. Department of Computer Science ... Alto, California designed and completed a study to compare a variety of usability ..... performed; where each "best way" corresponds to one sequence of user actions, eg. a ...