Programming and Mathematical Thinking

[Pages:260]Programming and Mathematical Thinking

A Gentle Introduction to Discrete Math Featuring Python

Allan M. Stavely

The New Mexico Tech Press

Socorro, New Mexico, USA

Programming and Mathematical Thinking

A Gentle Introduction to Discrete Math Featuring Python

Allan M. Stavely

Copyright ? 2014 Allan M. Stavely First Edition

Content of this book available under the Creative Commons Attribution-Noncommercial-ShareAlike License. See for details.

Publisher's Cataloguing-in-Publication Data

Stavely, Allan M Programming and mathematical thinking: a gentle introduction to discrete math featuring Python / Allan M. Stavely. xii, 246 p.: ill. ; 28 cm ISBN 978-1-938159-00-8 (pbk.) -- 978-1-938159-01-5 (ebook) 1. Computer science -- Mathematics. 2. Mathematics -- Discrete Mathematics. 3. Python (Computer program language).

QA 76.9 .M35 .S79 2014 004-dc22

OCLC Number: 863653804 Published by The New Mexico Tech Press, a New Mexico nonprofit corporation

The New Mexico Tech Press Socorro, New Mexico, USA

Once more, to my parents, Earl and Ann i

ii

Table of Contents

Preface ........................................................................................................ vii 1. Introduction ............................................................................................. 1

1.1. Programs, data, and mathematical objects ..................................... 1 1.2. A first look at Python .................................................................... 3 1.3. A little mathematical terminology ................................................ 10 2. An overview of Python ........................................................................... 17 2.1. Introduction ................................................................................. 17 2.2. Values, types, and names ............................................................. 18 2.3. Integers ........................................................................................ 19 2.4. Floating-point numbers ................................................................ 23 2.5. Strings .......................................................................................... 25 3. Python programs .................................................................................... 29 3.1. Statements ................................................................................... 29 3.2. Conditionals ................................................................................ 31 3.3. Iterations ..................................................................................... 35 4. Python functions ..................................................................................... 41 4.1. Function definitions ..................................................................... 41 4.2. Recursive functions ...................................................................... 43 4.3. Functions as values ...................................................................... 45 4.4. Lambda expressions ..................................................................... 48 5. Tuples ..................................................................................................... 51 5.1. Ordered pairs and n-tuples .......................................................... 51 5.2. Tuples in Python .......................................................................... 52 5.3. Files and databases ...................................................................... 54 6. Sequences ............................................................................................... 57 6.1. Properties of sequences ................................................................ 57 6.2. Monoids ...................................................................................... 59 6.3. Sequences in Python ..................................................................... 64 6.4. Higher-order sequence functions .................................................. 67 6.5. Comprehensions .......................................................................... 73 6.6. Parallel processing ....................................................................... 74 7. Streams ................................................................................................... 83 7.1. Dynamically-generated sequences ................................................ 83 7.2. Generator functions ..................................................................... 85

iii

Programming and Mathematical Thinking

7.3. Endless streams ............................................................................ 90 7.4. Concatenation of streams ............................................................ 92 7.5. Programming with streams .......................................................... 95 7.6. Distributed processing ............................................................... 103 8. Sets ....................................................................................................... 107 8.1. Mathematical sets ...................................................................... 107 8.2. Sets in Python ............................................................................ 110 8.3. A case study: finding students for jobs ....................................... 114 8.4. Flat files, sets, and tuples ........................................................... 118 8.5. Other representations of sets ...................................................... 123 9. Mappings ............................................................................................. 127 9.1. Mathematical mappings ............................................................. 127 9.2. Python dictionaries .................................................................... 131 9.3. A case study: finding given words in a file of text ...................... 135 9.4. Dictionary or function? .............................................................. 140 9.5. Multisets .................................................................................... 145 10. Relations ............................................................................................ 153 10.1. Mathematical terminology and notation .................................. 153 10.2. Representations in programs .................................................... 156 10.3. Graphs ..................................................................................... 159 10.4. Paths and transitive closure ...................................................... 164 10.5. Relational database operations ................................................ 167 11. Objects ............................................................................................... 175 11.1. Objects in programs ................................................................. 175 11.2. Defining classes ........................................................................ 177 11.3. Inheritance and the hierarchy of classes ................................... 180 11.4. Object-oriented programming .................................................. 184 11.5. A case study: moving averages ................................................. 185 11.6. Recursively-defined objects: trees ............................................. 194 11.7. State machines ......................................................................... 201 12. Larger programs ................................................................................. 213 12.1. Sharing tune lists ...................................................................... 213 12.2. Biological surveys .................................................................... 218 12.3. Note cards for writers .............................................................. 227 Afterword ................................................................................................. 233 Solutions to selected exercises ................................................................... 235 Index ........................................................................................................ 241

iv

List of Examples

1.1. Finding a name ...................................................................................... 4 1.2. Finding an email address ....................................................................... 7 1.3. Average of a collection of observations .................................................. 8 6.1. Finding a name again, in functional style ............................................. 71 6.2. Average of observations again, in functional style ................................ 72 7.1. Combinations using a generator function ............................................ 89 8.1. Finding job candidates using set operations ....................................... 117 8.2. Job candidates again, with different input files .................................. 122 9.1. Finding given words in a document ................................................... 139 9.2. A memoized function: the nth Fibonacci number ................................ 144 9.3. Number of students in each major field ............................................. 149 10.1. Distances using symmetry and reflexivity ......................................... 159 11.1. The MovingAverage class .................................................................. 191 11.2. The MovingAverage class, version 2 ................................................. 193 11.3. The Pushbutton class ....................................................................... 204 11.4. A state machine for finding fields in a string .................................... 206 11.5. Code that uses a FieldsStateMachine ............................................. 207 11.6. A state machine for finding fields, version 2 .................................... 209

v

vi

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download