Index 245 element, 83, 96 embedded reference, 136, 181, 190, 207 encapsulate, 72 encapsulation, 67, 134, 191, 196 encode, 160, 167 encrypt, 160 equality, 132 error compile-time, 221 runtime, 5, 45, 221 semantic, 5, 221, 227 syntax, 4, 221 error checking, 58 error handling, 216 error messages, 221 escape sequence, 66, 72 Euclid, 234 eureka traversal, 78 except statement, 124, 126 exception, 5, 9, 124, 126, 221, 225 executable, 9 execution flow, 225 expression, 17, 21, 194 big and hairy, 228 boolean, 37, 47 expression tree, 209, 212 fac<strong>to</strong>rial function, 55, 58 Fibonacci function, 58, 111 FIFO, 199, 206 file, 117, 126 text, 119 float, 11 floating-point, 21, 129 flow of execution, 29, 35, 225 for loop, 74, 86 formal language, 6, 9 format opera<strong>to</strong>r, 120, 126, 205, 225 format string, 120, 126 frabjuous, 55 fraction, 231 addition, 234 comparison, 235 multiplication, 232 frame, 32, 35, 44, 112 function, 27, 35, 71, 139, 148 argument, 30 boolean, 54, 167 composition, 26, 53 fac<strong>to</strong>rial, 55 helper, 187 math, 25 parameter, 30 recursive, 44 tuple as return value, 99 wrapper, 187 function call, 23, 35 function definition, 27, 35 function frame, 32, 35, 44, 112 function type modifier, 141 pure, 140 functional programming style, 142, 145 fundamental ambiguity theorem, 190 game animal, 216 gamma function, 59 generalization, 67, 134, 144 generalize, 72 generic data structure, 192, 193 geometric sequence, 65 Golfer, 205 greatest common divisor, 234, 237 guardian, 60 handle exception, 124, 126 handling errors, 216 hanging, 223 hello world, 8 helper function, 187 helper method, 190 high-level language, 2, 9 hint, 111, 115 his<strong>to</strong>gram, 104, 105, 113
246 Index Holmes, Sherlock, 5 identity, 132 immutable, 97 immutable string, 77 immutable type, 105 implementation Queue, 199 improved queue, 201 in opera<strong>to</strong>r, 86, 166 increment, 81 incremental development, 51, 60, 145 incremental program development, 222 index, 74, 81, 96, 107, 225 negative, 74 IndexError, 226 infinite list, 185 infinite loop, 63, 72, 223, 224 infinite recursion, 45, 47, 59, 223, 224 infix, 194, 197, 209 inheritance, 169, 179 initialization method, 152, 158, 163 inorder, 210, 219 instance, 131, 134, 137 object, 130, 148, 162 instantiate, 137 instantiation, 130 int, 11 integer long, 113 integer division, 17, 20, 21, 24 Intel, 64 interface, 192, 206 interpret, 2, 9 invariant, 189, 190 invoke, 115 invoking method, 109 irrational, 236 iteration, 61, 62, 72 join function, 95 key, 107, 115 key-value pair, 107, 115 KeyError, 226 keyword, 13, 15, 21 knowledge base, 216 language, 131 complete, 55 formal, 6 high-level, 2 low-level, 2 natural, 6 programming, 1 safe, 5 leaf node, 207, 219 leap of faith, 57, 184 length, 85 level, 207, 219 linear time, 201, 206 link, 190 linked list, 181, 190 linked queue, 200, 206 LinkedList, 188 Linux, 6 list, 83, 96, 181 as argument, 183 as parameter, 93 cloning, 92 element, 84 for loop, 86 infinite, 185 length, 85 linked, 181, 190 loop, 185 membership, 86 modifying, 186 mutable, 88 nested, 83, 94, 110 of objects, 163 printing, 183 printing backwards, 184 slice, 88 traversal, 85, 183 traverse recursively, 184
- 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 276 and 277: Index 247 well-formed, 189 list del
- Page 278 and 279: Index 249 redundancy, 7 reference,