12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

112 Chapter 11. DictionariesInthefirstcase the resulthas typeint;inthe second case itislong.Exercise 11.7 Exponentiation of large integers is the basis of common algorithms for public-keyencryption. Read the Wikipedia page on the RSA algorithm 3 and write functions to encode anddecode messages.11.8 DebuggingAs you work with bigger datasets it can become unwieldy to debug by printing and checking databy hand. Herearesome suggestions fordebugging large datasets:Scale down theinput: Ifpossible,reducethesizeofthedataset. Forexampleiftheprogramreadsatextfile,startwithjustthefirst10lines,orwiththesmallestexampleyoucanfind. Youcaneithereditthefilesthemselves,or(better)modifytheprogramsoitreadsonlythefirstnlines.If there is an error, you can reduce n to the smallest value that manifests the error, and thenincrease it gradually as you find and correct errors.Check summaries and types: Insteadofprintingandcheckingtheentiredataset,considerprintingsummaries of the data: for example, the number of items in a dictionary or the total of a listof numbers.A common cause of runtime errors is a value that is not the right type. For debugging thiskind of error,itisoften enough toprint the type ofavalue.Writeself-checks: Sometimes you can write code to check for errors automatically. For example,if you are computing the average of a list of numbers, you could check that the result is notgreater than the largest element in the list or less than the smallest. This is called a “sanitycheck” because itdetects resultsthat are “insane.”Another kind of check compares the results of two different computations to see if they areconsistent. This iscalled a“consistency check.”Pretty print the output: Formatting debugging output can make it easier to spot an error. We sawan example in Section 6.9. The pprint module provides a pprint function that displaysbuilt-intypes inamorehuman-readable format.Again, timeyou spend building scaffolding can reduce thetimeyou spend debugging.11.9 Glossarydictionary: A mapping fromaset of keys totheir corresponding values.key-value pair: The representation of themapping from akey toavalue.item: Another name forakey-value pair.key: An object that appears inadictionary as thefirstpart ofakey-value pair.value: An object that appears in a dictionary as the second part of a key-value pair. This is morespecific than our previous use of theword “value.”3 wikipedia.org/wiki/RSA.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!