Dictionary Review

7 Pages

Computer Science
Course Code
Kathy Fellowes

This preview shows pages 1 and half of page 2. Sign up to view the full 7 pages of the document.
CSCA08Introduction to ProgrammingDictionary ReviewRecall the code for inverting a dictionarydef inverttable Given a dict table that contains keyvalue pairs returns anew dict that has tables values as keys and the values forthe new dict are the keys from table in a listinvertednew dictfor key in table traverse the dict tables valuetablekey if not invertedhaskeyvalue check if already a key invertedvalueadd it to inverted dictinvertedvalueappendkey add value to inverted dict return invertedreturn the new dict Now consider the following function to add to the programdef countwordsr Given an open file reader return a dict that has the words from the file r as keys and the number of occurrences of the corresponding words as valuesworddictfor line in rfor item in linestripsplitif item in worddictworddictitem1elseworddictitem1return worddictFinally lets write a main method that open the file prideandprejudicetxt for reading and creates a dict of the word count in the file and also creates another dict which will be the inverted dict of the word count dictBe sure to close the fileif namemainCSCA08Fall 2010Dictionaries ClassesPage 1 of 7
More Less
Unlock Document

Only pages 1 and half of page 2 are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.