Index 247 well-formed, 189 list deletion, 89 list method, 114, 163 list operation, 87 list traversal, 96 literalness, 7 local variable, 31, 35, 69 logarithm, 64 logical opera<strong>to</strong>r, 37, 38 long integer, 113 loop, 63, 72 body, 63, 72 condition, 224 for loop, 74 in list, 185 infinite, 63, 224 nested, 163 traversal, 74 while, 62 loop variable, 72, 171, 183 low-level language, 2, 9 lowercase, 80 map <strong>to</strong>, 160 math function, 25 mathematical opera<strong>to</strong>r, 232 matrix, 94 sparse, 110 McCloskey, Robert, 75 mental model, 227 method, 109, 115, 139, 148, 158 dictionary, 109 initialization, 152, 163 invocation, 109 list, 114, 163 model mental, 227 modifier, 141, 145 modifying lists, 186 module, 25, 35, 79 copy, 135 string, 80 modulus opera<strong>to</strong>r, 37, 47, 171 multiple assignment, 61, 72 multiplication fraction, 232 mutable, 77, 81, 97 list, 88 object, 134 mutable type, 105 NameError, 225 natural language, 6, 9, 131 negation, 236 nested list, 94, 96, 110 nested structure, 159 nesting, 47 newline, 72 node, 181, 190, 207, 219 Node class, 181 None, 50, 60 number random, 99 numera<strong>to</strong>r, 231 object, 91, 96, 129, 137 list of, 163 mutable, 134 object code, 9 object instance, 130, 148, 162 object invariant, 189 object-oriented design, 169 object-oriented programming, 147, 169 object-oriented programming language, 147, 158 operand, 17, 21 operation dictionary, 108 list, 87 opera<strong>to</strong>r, 17, 21 binary, 209, 219 bracket, 73 conditional, 162 format, 120, 126, 205, 225 in, 86, 166 logical, 37, 38
248 Index modulus, 37, 171 overloading, 154, 232 opera<strong>to</strong>r overloading, 154, 158, 162, 205 order of evaluation, 228 order of operations, 17 ordering, 162 overload, 232 overloading, 158 opera<strong>to</strong>r, 205 override, 158, 162 parameter, 30, 35, 93, 131 list, 93 parent class, 169, 170, 172, 179 parent node, 207, 219 parse, 7, 9, 194, 197, 212 partial ordering, 162 pass statement, 39 path, 123 pattern, 78 pattern matching, 105 Pentium, 64 performance, 201 performance hazard, 206 pickle, 126 pickling, 123 planned development, 145 poetry, 7 Point class, 153 polymorphic, 158 polymorphism, 155 pop, 193 portability, 9 portable, 2 postfix, 194, 197, 209 pos<strong>to</strong>rder, 210, 219 precedence, 21, 228 precondition, 185, 190 prefix, 210, 219 preorder, 210, 219 print statement, 8, 9, 226 printing deck object, 163 hand of cards, 171 object, 131, 148 priority, 205 priority queue, 199, 206 ADT, 203 priority queueing, 199 problem-solving, 9 product, 214 program, 9 development, 72 program development encapsulation, 67 generalization, 67 programming language, 1 prompt, 45, 47 prose, 7 pro<strong>to</strong>type development, 143 provider, 192, 197 pseudocode, 234 pseudorandom, 105 pure function, 140, 145 push, 193 queue, 199, 206 improved implementation, 201 linked implementation, 200 List implementation, 199 Queue ADT, 199 queueing policy, 199, 206 raise exception, 124, 126 random, 165 random number, 99 randrange, 165 rank, 159 rational, 231 rectangle, 133 recursion, 42, 44, 47, 55, 57, 209, 210 base case, 44 infinite, 45, 59, 224 recursive data structure, 181, 190, 208 recursive definition, 214 reduce, 234, 237
- Page 2 and 3:
How to Think Like a Computer Scient
- Page 4 and 5:
How to Think Like a Computer Scient
- Page 6 and 7:
Foreword By David Beazley As an edu
- Page 8 and 9:
Preface By Jeff Elkner This book ow
- Page 10 and 11:
make instant changes whenever someo
- Page 12 and 13:
call (type) out its name.” Parame
- Page 14 and 15:
Contributor List To paraphrase the
- Page 16 and 17:
• Paul Sleigh found an error in C
- Page 18 and 19:
xvii • Roger Sperberg pointed out
- Page 20 and 21:
Contents Foreword v Preface vii Con
- Page 22 and 23:
Contents xxi 4.7 Nested conditional
- Page 24 and 25:
Contents xxiii 8.15 Matrices . . .
- Page 26 and 27:
Contents xxv 14.4 A more complicate
- Page 28 and 29:
Contents xxvii 19.4 Improved Linked
- Page 30 and 31:
Chapter 1 The way of the program Th
- Page 32 and 33:
1.2 What is a program? 3 $ python P
- Page 34 and 35:
1.3 What is debugging? 5 1.3.2 Runt
- Page 36 and 37:
1.4 Formal and natural languages 7
- Page 38 and 39:
1.6 Glossary 9 source code: A progr
- Page 40 and 41:
Chapter 2 Variables, expressions an
- Page 42 and 43:
2.3 Variable names and keywords 13
- Page 44 and 45:
2.4 Statements 15 >>> 76trombones =
- Page 46 and 47:
2.6 Operators and operands 17 2.6 O
- Page 48 and 49:
2.9 Composition 19 On one hand, thi
- Page 50 and 51:
2.11 Glossary 21 state diagram: A g
- Page 52 and 53:
Chapter 3 Functions 3.1 Function ca
- Page 54 and 55:
3.4 Math functions 25 >>> minute =
- Page 56 and 57:
3.6 Adding new functions 27 problem
- Page 58 and 59:
3.7 Definitions and use 29 3.7 Defi
- Page 60 and 61:
3.10 Variables and parameters are l
- Page 62 and 63:
3.12 Functions with results 33 The
- Page 64 and 65:
3.13 Glossary 35 local variable: A
- Page 66 and 67:
Chapter 4 Conditionals and recursio
- Page 68 and 69:
4.4 Conditional execution 39 In gen
- Page 70 and 71:
4.7 Nested conditionals 41 Each con
- Page 72 and 73:
4.9 Recursion 43 countdown expects
- Page 74 and 75:
4.11 Infinite recursion 45 4.11 Inf
- Page 76 and 77:
4.13 Glossary 47 block: A group of
- Page 78 and 79:
Chapter 5 Fruitful functions 5.1 Re
- Page 80 and 81:
5.2 Program development 51 avoid lo
- Page 82 and 83:
5.3 Composition 53 3. Once the prog
- Page 84 and 85:
5.5 More recursion 55 But the extra
- Page 86 and 87:
5.6 Leap of faith 57 __main__ facto
- Page 88 and 89:
5.8 Checking types 59 >>> factorial
- Page 90 and 91:
Chapter 6 Iteration 6.1 Multiple as
- Page 92 and 93:
6.2 The while statement 63 1. Evalu
- Page 94 and 95:
6.3 Tables 65 1.0 0.0 2.0 0.6931471
- Page 96 and 97:
6.5 Encapsulation and generalizatio
- Page 98 and 99:
6.7 Local variables 69 6.7 Local va
- Page 100 and 101:
6.9 Functions 71 the function is ca
- Page 102 and 103:
Chapter 7 Strings 7.1 A compound da
- Page 104 and 105:
7.3 Traversal and the for loop 75 T
- Page 106 and 107:
7.6 Strings are immutable 77 Other
- Page 108 and 109:
7.9 The string module 79 As an exer
- Page 110 and 111:
7.11 Glossary 81 There are other us
- Page 112 and 113:
Chapter 8 Lists A list is an ordere
- Page 114 and 115:
8.3 List length 85 If you try to re
- Page 116 and 117:
8.6 List operations 87 for VARIABLE
- Page 118 and 119:
8.9 List deletion 89 >>> list = [
- Page 120 and 121:
8.10 Objects and values 91 8.10 Obj
- Page 122 and 123:
8.13 List parameters 93 >>> b[0] =
- Page 124 and 125:
8.16 Strings and lists 95 might be
- Page 126 and 127:
Chapter 9 Tuples 9.1 Mutability and
- Page 128 and 129:
9.3 Tuples as return values 99 9.3
- Page 130 and 131:
9.6 Counting 101 >>> randomList(8)
- Page 132 and 133:
9.7 Many buckets 103 bucketWidth =
- Page 134 and 135:
9.9 Glossary 105 9.9 Glossary Assig
- Page 136 and 137:
Chapter 10 Dictionaries The compoun
- Page 138 and 139:
10.2 Dictionary methods 109 10.2 Di
- Page 140 and 141:
10.5 Hints 111 An alternative is to
- Page 142 and 143:
10.6 Long integers 113 Using this v
- Page 144 and 145:
10.8 Glossary 115 overflow: A numer
- Page 146 and 147:
Chapter 11 Files and exceptions Whi
- Page 148 and 149:
11.1 Text files 119 The following f
- Page 150 and 151:
11.2 Writing variables 121 the str
- Page 152 and 153:
11.3 Directories 123 11.3 Directori
- Page 154 and 155:
11.5 Exceptions 125 Or trying to op
- Page 156 and 157:
11.6 Glossary 127 format sequence:
- Page 158 and 159:
Chapter 12 Classes and objects 12.1
- Page 160 and 161:
12.3 Instances as arguments 131 con
- Page 162 and 163:
12.5 Rectangles 133 >>> p1 = Point(
- Page 164 and 165:
12.8 Copying 135 The variables dwid
- Page 166 and 167:
12.9 Glossary 137 12.9 Glossary cla
- Page 168 and 169:
Chapter 13 Classes and functions 13
- Page 170 and 171:
13.3 Modifiers 141 The output of th
- Page 172 and 173:
13.5 Prototype development versus p
- Page 174 and 175:
13.8 Glossary 145 Similarly, the te
- Page 176 and 177:
Chapter 14 Classes and methods 14.1
- Page 178 and 179:
14.3 Another example 149 class Time
- Page 180 and 181:
14.5 Optional arguments 151 if done
- Page 182 and 183:
14.7 Points revisited 153 >>> curre
- Page 184 and 185:
14.9 Polymorphism 155 def __mul__(s
- Page 186 and 187:
14.10 Glossary 157 Of course, we in
- Page 188 and 189:
Chapter 15 Sets of objects 15.1 Com
- Page 190 and 191:
15.3 Class attributes and the str m
- Page 192 and 193:
15.5 Decks 163 def __cmp__(self, ot
- Page 194 and 195:
15.7 Shuffling the deck 165 >>> dec
- Page 196 and 197:
15.9 Glossary 167 class Deck: ... d
- Page 198 and 199:
Chapter 16 Inheritance 16.1 Inherit
- Page 200 and 201:
16.3 Dealing cards 171 16.3 Dealing
- Page 202 and 203:
16.6 OldMaidHand class 173 class Ca
- Page 204 and 205:
16.7 OldMaidGame class 175 Hand fra
- Page 206 and 207:
16.7 OldMaidGame class 177 class Ol
- Page 208 and 209:
16.8 Glossary 179 Hand Allen picked
- Page 210 and 211:
Chapter 17 Linked lists 17.1 Embedd
- Page 212 and 213:
17.3 Lists as collections 183 node1
- Page 214 and 215:
17.5 Infinite lists 185 We invoke t
- Page 216 and 217:
17.8 Wrappers and helpers 187 def r
- Page 218 and 219:
17.10 Invariants 189 tail = self.ne
- Page 220 and 221:
Chapter 18 Stacks 18.1 Abstract dat
- Page 222 and 223:
18.4 Pushing and popping 193 def po
- Page 224 and 225:
18.7 Evaluating postfix 195 Python
- Page 226 and 227: 18.9 Glossary 197 18.9 Glossary abs
- Page 228 and 229: Chapter 19 Queues This chapter pres
- Page 230 and 231: 19.3 Performance characteristics 20
- Page 232 and 233: 19.5 Priority queue 203 and it is m
- Page 234 and 235: 19.6 The Golfer class 205 19.6 The
- Page 236 and 237: Chapter 20 Trees Like linked lists,
- Page 238 and 239: 20.2 Traversing trees 209 20.2 Trav
- Page 240 and 241: 20.4 Tree traversal 211 def printTr
- Page 242 and 243: 20.5 Building an expression tree 21
- Page 244 and 245: 20.5 Building an expression tree 21
- Page 246 and 247: 20.7 The animal tree 217 Are you th
- Page 248 and 249: 20.8 Glossary 219 def yes(ques): fr
- Page 250 and 251: Appendix A Debugging Different kind
- Page 252 and 253: A.2 Runtime errors 223 A.1.1 I can
- Page 254 and 255: A.2 Runtime errors 225 If there is
- Page 256 and 257: A.3 Semantic errors 227 A.3 Semanti
- Page 258 and 259: A.3 Semantic errors 229 return self
- Page 260 and 261: Appendix B Creating a new data type
- Page 262 and 263: B.1 Fraction multiplication 233 >>>
- Page 264 and 265: B.4 Comparing fractions 235 This re
- Page 266 and 267: B.6 Glossary 237 reduce: To change
- Page 268 and 269: Appendix C Recommendations for furt
- Page 270 and 271: C.2 Recommended general computer sc
- Page 272 and 273: Index Make Way for Ducklings, 75 Py
- Page 274 and 275: Index 245 element, 83, 96 embedded
- Page 278 and 279: Index 249 redundancy, 7 reference,