132 Chapter 13. Casestudy: datastructure selectionOk, the last one is the easy; the only mapping type we have seen is a dictionary, so it is the naturalchoice.For the prefixes, the most obvious options are string, list of strings, or tuple of strings. For thesuffixes, one option isalist;another isahistogram (dictionary).How should you choose? The first step is to think about the operations you will need to implementforeachdatastructure. Fortheprefixes,weneedtobeabletoremovewordsfromthebeginningandadd to the end. For example, if the current prefix is “Half a,” and the next word is “bee,” you needtobe able toformthe next prefix, “a bee.”Your first choice might be a list, since it is easy to add and remove elements, but we also need to beable to use the prefixes as keys in a dictionary, so that rules out lists. With tuples, you can’t appendor remove, but you can usetheaddition operator toformanew tuple:def shift(prefix, word):return prefix[1:] + (word,)shift takes a tuple of words, prefix, and a string, word, and forms a new tuple that has all thewords inprefixexcept the first,andwordadded totheend.For the collection of suffixes, the operations we need to perform include adding a new suffix (orincreasing the frequency of an existing one), and choosing a random suffix.Addinganewsuffixisequallyeasyforthelistimplementationorthehistogram. Choosingarandomelementfromalistiseasy;choosingfromahistogramishardertodoefficiently(seeExercise13.7).So far we have been talking mostly about ease of implementation, but there are other factors toconsider in choosing data structures. One is run time. Sometimes there is a theoretical reason toexpect one data structure to be faster than other; for example, I mentioned that the in operator isfasterfor dictionaries than for lists,at leastwhen thenumber ofelements islarge.But often you don’t know ahead of time which implementation will be faster. One option is toimplementbothofthemandseewhichisbetter. Thisapproachiscalledbenchmarking. Apracticalalternativeistochoosethedatastructurethatiseasiesttoimplement,andthenseeifitisfastenoughfortheintendedapplication. Ifso,thereisnoneedtogoon. Ifnot,therearetools,liketheprofilemodule, that can identifythe places inaprogram that take themost time.The other factor to consider is storage space. For example, using a histogram for the collection ofsuffixes might take less space because you only have to store each word once, no matter how manytimes it appears in the text. In some cases, saving space can also make your program run faster,and in the extreme, your program might not run at all if you run out of memory. But for manyapplications, space isasecondary consideration after runtime.One final thought: in this discussion, I have implied that we should use one data structure for bothanalysis and generation. But since these are separate phases, it would also be possible to use onestructure for analysis and then convert to another structure for generation. This would be a net winifthe timesaved during generation exceeded the timespent inconversion.13.10 DebuggingWhenyouaredebuggingaprogram,andespeciallyifyouareworkingonahardbug,therearefourthings totry:
13.11. Glossary 133reading: Examineyourcode,readitbacktoyourself,andcheckthatitsayswhatyoumeanttosay.running: Experiment by making changes and running different versions. Often if you display theright thing at the right place in the program, the problem becomes obvious, but sometimesyou have tospend sometimetobuild scaffolding.ruminating: Take some time to think! What kind of error is it: syntax, runtime, semantic? Whatinformation can you get from the error messages, or from the output of the program? Whatkind of error could cause the problem you’re seeing? What did you change last, before theproblem appeared?retreating: At some point, the best thing to do is back off, undoing recent changes, until you getback toaprogram that works and that you understand. Then you can startrebuilding.Beginning programmers sometimes get stuck on one of these activities and forget the others. Eachactivity comes withitsown failuremode.For example, reading your code might help if the problem is a typographical error, but not if theproblem is a conceptual misunderstanding. If you don’t understand what your program does, youcan read it100 times and never seethe error,because theerror isinyour head.Running experiments can help, especially if you run small, simple tests. But if you run experimentswithout thinking or reading your code, you might fall into a pattern I call “random walkprogramming,” which is the process of making random changes until the program does the rightthing. Needless tosay, random walk programming can take along time.You have totake time tothink. Debugging islike an experimental science. You should have at leastonehypothesisaboutwhattheproblemis. Iftherearetwoormorepossibilities,trytothinkofatestthat would eliminate one of them.Takingabreakhelpswiththethinking. Sodoestalking. Ifyouexplaintheproblemtosomeoneelse(oreven yourself),you willsometimes find the answer before you finish asking thequestion.But even the best debugging techniques will fail if there are too many errors, or if the code you aretrying to fix is too big and complicated. Sometimes the best option is to retreat, simplifying theprogram until you get tosomething that works and that you understand.Beginningprogrammersareoftenreluctanttoretreatbecausetheycan’tstandtodeletealineofcode(even if it’s wrong). If it makes you feel better, copy your program into another file before you startstrippingitdown. Then you can paste thepieces back inalittlebitat atime.Findingahardbugrequiresreading,running,ruminating,andsometimesretreating. Ifyougetstuckon one of theseactivities, trytheothers.13.11 Glossarydeterministic: Pertaining to a program that does the same thing each time it runs, given the sameinputs.pseudorandom: Pertaining to a sequence of numbers that appear to be random, but are generatedby adeterministic program.default value: The value given toan optional parameter ifno argument isprovided.
- Page 1:
Think PythonHowto Think LikeaComput
- Page 4 and 5:
Copyright©2008 AllenDowney.Printin
- Page 6 and 7:
viChapter 0. PrefaceIn 2003 I start
- Page 8 and 9:
viiiChapter 0. Preface• Keith Ver
- Page 10 and 11:
xChapter 0. Preface• Patryk Wolow
- Page 12 and 13:
xiiContents3 Functions 173.1 Functi
- Page 14 and 15:
xivContents7 Iteration 637.1 Multip
- Page 16 and 17:
xviContents12 Tuples 11512.1 Tuples
- Page 18 and 19:
xviiiContents17 Classes and methods
- Page 20 and 21:
xxContents
- Page 22:
2 Chapter 1. The way of theprogramp
- Page 25 and 26:
1.4. Formal and natural languages 5
- Page 27 and 28:
1.7. Glossary 7Your job is to be a
- Page 29 and 30:
Chapter 2Variables,expressions ands
- Page 31 and 32:
2.3. Variable names and keywords 11
- Page 33 and 34:
2.6. Expressions 132.6 ExpressionsA
- Page 35 and 36:
2.10. Debugging 152.10 DebuggingAtt
- Page 37 and 38:
Chapter 3Functions3.1 Functioncalls
- Page 39 and 40:
3.4. Composition 19>>> math.sqrt(2)
- Page 41 and 42:
3.7. Flow of execution 21def repeat
- Page 43 and 44:
3.10. Stack diagrams 23Thisfunction
- Page 45 and 46:
3.12. Why functions? 253.12 Whyfunc
- Page 47 and 48:
3.15. Exercises 27Here’s an examp
- Page 49 and 50:
Chapter 4Case study: interfacedesig
- Page 51 and 52:
4.3. Exercises 31The syntax of a fo
- Page 53 and 54:
4.6. Interface design 33This draws
- Page 55 and 56:
4.8. A development plan 354.8 Adeve
- Page 57 and 58:
4.12. Exercises 37Exercise 4.2 Writ
- Page 59 and 60:
Chapter 5Conditionals and recursion
- Page 61 and 62:
5.5. Alternative execution 41if sta
- Page 63 and 64:
5.9. Stack diagrams for recursive f
- Page 65 and 66:
5.11. Keyboard input 455.11 Keyboar
- Page 67 and 68:
5.14. Exercises 47relational operat
- Page 69 and 70:
5.14. Exercises 492. Write a functi
- Page 71 and 72:
Chapter 6Fruitfulfunctions6.1 Retur
- Page 73 and 74:
6.2. Incremental development 53>>>
- Page 75 and 76:
6.5. More recursion 55It is common
- Page 77 and 78:
6.6. Leap of faith 57__main__factor
- Page 79 and 80:
6.9. Debugging 59The first base cas
- Page 81 and 82:
6.11. Exercises 61sum = x + y + zpo
- Page 83 and 84:
Chapter 7Iteration7.1 Multipleassig
- Page 85 and 86:
7.4. break 652. Iftheconditionisfal
- Page 87 and 88:
7.6. Algorithms 672.00001024003>>>
- Page 89 and 90:
7.9. Exercises 69decrement: An upda
- Page 91 and 92:
Chapter 8Strings8.1 Astringis a seq
- Page 93 and 94:
8.4. String slices 73Thefollowingex
- Page 95 and 96:
8.7. Looping and counting 75This is
- Page 97 and 98:
8.10. String comparison 778.10 Stri
- Page 99 and 100:
8.12. Glossary 798.12 Glossaryobjec
- Page 101 and 102: Chapter 9Case study: word play9.1 R
- Page 103 and 104: 9.3. Search 83Exercise 9.6 Write a
- Page 105 and 106: 9.5. Debugging 85Here is a version
- Page 107 and 108: 9.7. Exercises 87Write a Python pro
- Page 109 and 110: Chapter 10Lists10.1 Alist isasequen
- Page 111 and 112: 10.3. Traversing alist 9110.3 Trave
- Page 113 and 114: 10.7. Map, filter and reduce 93List
- Page 115 and 116: 10.9. Listsand strings 95>>> t = ['
- Page 117 and 118: 10.12. Listarguments 97The statedia
- Page 119 and 120: 10.14. Glossary 99word = word.strip
- Page 121 and 122: 10.15. Exercises 101Exercise 10.6 W
- Page 123 and 124: Chapter 11DictionariesAdictionaryis
- Page 125 and 126: 11.1. Dictionary as asetof counters
- Page 127 and 128: 11.4. Dictionaries and lists 107Thi
- Page 129 and 130: 11.5. Memos 109corresponding locati
- Page 131 and 132: 11.7. Long integers 111count = 0def
- Page 133 and 134: 11.10. Exercises 113implementation:
- Page 135 and 136: Chapter 12Tuples12.1 Tuples areimmu
- Page 137 and 138: 12.3. Tuples as return values 117>>
- Page 139 and 140: 12.6. Dictionaries and tuples 119If
- Page 141 and 142: 12.7. Comparing tuples 12112.7 Comp
- Page 143 and 144: 12.10. Glossary 123>>> t3 = [1, 2,
- Page 145 and 146: Chapter 13Case study: data structur
- Page 147 and 148: 13.3. Word histogram 12713.3 Word h
- Page 149 and 150: 13.6. Dictionary subtraction 129num
- Page 151: 13.9. Datastructures 131In this tex
- Page 155 and 156: Chapter 14Files14.1 PersistenceMost
- Page 157 and 158: 14.4. Filenames and paths 137>>> '%
- Page 159 and 160: 14.6. Databases 139Python starts by
- Page 161 and 162: 14.9. Writingmodules 141The argumen
- Page 163 and 164: 14.11. Glossary 14314.11 Glossarype
- Page 165 and 166: Chapter 15Classes and objects15.1 U
- Page 167 and 168: 15.3. Rectangles 147>>> print '(%g,
- Page 169 and 170: 15.6. Copying 149>>> print box.widt
- Page 171 and 172: 15.8. Glossary 151>>> hasattr(p, 'x
- Page 173 and 174: Chapter 16Classes and functions16.1
- Page 175 and 176: 16.3. Modifiers 155if sum.second >=
- Page 177 and 178: 16.5. Debugging 157But if we have t
- Page 179 and 180: Chapter 17Classes and methods17.1 O
- Page 181 and 182: 17.3. Another example 161Inside the
- Page 183 and 184: 17.6. The str method 163>>> time =
- Page 185 and 186: 17.9. Polymorphism 165Unfortunately
- Page 187 and 188: 17.12. Exercises 167object-oriented
- Page 189 and 190: Chapter 18InheritanceIn this chapte
- Page 191 and 192: 18.3. Comparing cards 171Thefirstel
- Page 193 and 194: 18.6. Add, remove, shuffle and sort
- Page 195 and 196: 18.8. Classdiagrams 175So when you
- Page 197 and 198: 18.10. Glossary 177might get the on
- Page 199 and 200: 18.11. Exercises 1796. Print a tabl
- Page 201 and 202: Chapter 19Case study: Tkinter19.1 G
- Page 203 and 204:
19.3. Canvas widgets 183Exercise 19
- Page 205 and 206:
19.6. Packing widgets 185>>> text.i
- Page 207 and 208:
19.6. Packing widgets 187self.bu(te
- Page 209 and 210:
19.8. Binding 18919.8 BindingAbindi
- Page 211 and 212:
19.9. Debugging 19119.9 DebuggingOn
- Page 213 and 214:
19.11. Exercises 193image = PIL.ope
- Page 215 and 216:
Appendix ADebuggingDifferentkindsof
- Page 217 and 218:
A.2. Runtime errors 197A.2 Runtime
- Page 219 and 220:
A.3. Semantic errors 199• You are
- Page 221 and 222:
A.3. Semantic errors 201The explici
- Page 223 and 224:
Indexabecedarian, 72, 83abs functio
- Page 225 and 226:
Index 205experimental, 4superstitio
- Page 227 and 228:
Index 207raw input, 45recursive, 43
- Page 229 and 230:
Index 209map pattern, 93, 100map to
- Page 231 and 232:
Index 211poker, 169, 178polygon fun
- Page 233 and 234:
Index 213knowing the answer, 53leap