Table Of ContentComputer Science/Computing
Chapman & Hall/CRC
TEXTBOOKS IN COMPUTING
E
EXPLORATIONS IN COMPUTING
X EXPLORATIONS
An Introduction to Computer Science P
L
and Python Programming IN COMPUTING
O
R
A
Explorations in Computing: An Introduction to Computer Science and Py- An Introduction to Computer Science
T
thon Programming teaches you how to use programming skills to explore
I and Python Programming
fundamental concepts and computational approaches to solving problems. It
O
gives you an introduction to computer science concepts and computer pro-
N
gramming.
S
The text uses Python as the lab software so that you can seamlessly transition
from introductory projects to more advanced studies in later courses. It also I
N
introduces Python programming, providing you with sufficient programming
skills so you can implement your own programs.
C
The interactive lab projects in each chapter allow you to examine important
O
ideas in computer science, particularly how algorithms offer computational
M
solutions to problems. You can type expressions, view results, and run experi-
ments that help you understand the concepts in a hands-on way. The Python P
software modules for each lab project are available on the author’s website. U
Features T
• Emphasizes computational thinking and important concepts in computing I
N
• Gives you sufficient Python programming skills
• Includes tutorial projects that encourage you to explore a particular G
problem and solve it computationally
• Contains standard exercises that test your understanding of the topics
covered
• Provides the software and other ancillaries on the author’s website
Conery
John S. Conery
K16600
K16600_cover.indd 1 7/30/14 12:26 PM
EXPLORATIONS
IN COMPUTING
An Introduction to Computer Science
and Python Programming
K16600_FM.indd 1 8/6/14 11:07 AM
CHAPMAN & HALL/CRC
TEXTBOOKS IN COMPUTING
Series Editors
John Impagliazzo Andrew McGettrick
Professor Emeritus, Hofstra University Department of Computer
and Information Sciences
University of Strathclyde
Aims and Scope
This series covers traditional areas of computing, as well as related technical areas, such as
software engineering, artificial intelligence, computer engineering, information systems, and
information technology. The series will accommodate textbooks for undergraduate and gradu-
ate students, generally adhering to worldwide curriculum standards from professional societ-
ies. The editors wish to encourage new and imaginative ideas and proposals, and are keen to
help and encourage new authors. The editors welcome proposals that: provide groundbreaking
and imaginative perspectives on aspects of computing; present topics in a new and exciting
context; open up opportunities for emerging areas, such as multi-media, security, and mobile
systems; capture new developments and applications in emerging fields of computing; and
address topics that provide support for computing, such as mathematics, statistics, life and
physical sciences, and business.
Published Titles
Paul Anderson, Web 2.0 and Beyond: Principles and Technologies
Henrik Bærbak Christensen, Flexible, Reliable Software: Using Patterns and Agile
Development
John S. Conery, Explorations in Computing: An Introduction to Computer Science
John S. Conery, Explorations in Computing: An Introduction to Computer Science and
Python Programming
Ted Herman, A Functional Start to Computing with Python
Pascal Hitzler, Markus Krötzsch, and Sebastian Rudolph, Foundations of Semantic Web
Technologies
Mark J. Johnson, A Concise Introduction to Data Structures using Java
Mark J. Johnson, A Concise Introduction to Programming in Python
Lisa C. Kaczmarczyk, Computers and Society: Computing for Good
Mark C. Lewis, Introduction to the Art of Programming Using Scala
Bill Manaris and Andrew R. Brown, Making Music with Computers: Creative Programming
in Python
Uvais Qidwai and C.H. Chen, Digital Image Processing: An Algorithmic Approach with
MATLAB®
David D. Riley and Kenny A. Hunt, Computational Thinking for the Modern Problem Solver
Henry M. Walker, The Tao of Computing, Second Edition
K16600_FM.indd 2 8/6/14 11:07 AM
Chapman & Hall/CRC
TEXTBOOKS IN COMPUTING
EXPLORATIONS
IN COMPUTING
An Introduction to Computer Science
and Python Programming
John S. Conery
K16600_FM.indd 3 8/6/14 11:07 AM
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2015 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 20140630
International Standard Book Number-13: 978-1-4665-7245-4 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to
publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials
or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material repro-
duced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any
copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any
form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming,
and recording, or in any information storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copy-
right.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400.
CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been
granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identifica-
tion and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Contents
Preface ix
1 Introduction 1
1.1 Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 TheLimitsofComputation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 ALaboratoryforComputationalExperiments . . . . . . . . . . . . . . . . . . 12
1.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2 ThePythonWorkbench 17
IntroducingPythonandanenvironmentforinteractiveexperiments
2.1 InteractingwithPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2 Numbers,Functions,andtheMathModule. . . . . . . . . . . . . . . . . . . . 24
2.3 Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.4 DefiningNewFunctions: defStatements . . . . . . . . . . . . . . . . . . . . . 31
2.5 ConditionalExecution: ifStatements . . . . . . . . . . . . . . . . . . . . . . 36
2.6 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.7 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3 TheSieveofEratosthenes 55
Analgorithmforfindingprimenumbers
3.1 TheAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.3 IteratingOverLists: forStatements . . . . . . . . . . . . . . . . . . . . . . . 62
3.4 ListIndexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.5 TheAnimatedSieve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.6 AHelperFunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.7 The sieve Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.8 (cid:169) RunningaProgramfromtheCommandLine . . . . . . . . . . . . . . . . . 81
3.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
v
vi
4 AJourneyofaThousandMiles 91
Iterationasastrategyforsolvingcomputationalproblems
4.1 TheLinearSearchAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.2 ImplementingLinearSearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
4.3 TheInsertionSortAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
4.4 AHelperFunctionforInsertionSort . . . . . . . . . . . . . . . . . . . . . . . 101
4.5 ImplementingInsertionSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4.6 Scalability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
5 DivideandConquer 117
Anewstrategy: Breakinglargeproblemsintosmallersubproblems
5.1 TheBinarySearchAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
5.2 ImplementingBinarySearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
5.3 BinarySearchExperiments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
5.4 TheMergeSortAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
5.5 ImplementingMergeSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.6 MergeSortExperiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.7 (cid:169) RecursiveFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
5.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
6 Spam,Spam,Spam,Mail,andSpam 147
Amachinelearningapproachtofilteringjunkmail
6.1 ACloserLookatStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
6.2 ReadingTextfromaFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
6.3 CountingWordsinaTextFile . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
6.4 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.5 WordFrequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
6.6 Spamicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
6.7 AnAlgorithmforIdentifyingJunkMail . . . . . . . . . . . . . . . . . . . . . . 176
6.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
7 NowforSomethingCompletelyDifferent 191
Analgorithmforgeneratingrandomnumbers
7.1 PseudorandomNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
7.2 NumbersonDemand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
7.3 ModulesandEncapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
7.4 GameswithRandomNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . 208
7.5 RandomShuffles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
7.6 TestsofRandomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
7.7 DefiningNewObjects: classStatements . . . . . . . . . . . . . . . . . . . . . 224
7.8 (cid:169) AdditionalFeaturesfortheCardClass . . . . . . . . . . . . . . . . . . . . . 234
7.8.1 (cid:169) ClassVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
7.8.2 (cid:169) Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
7.8.3 (cid:169) OptionalArguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
7.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
vii
8 BitbyBit 249
Binarycodesandalgorithmsfortextcompressionanderrordetection
8.1 BinaryCodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
8.2 CodesforCharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
8.3 ErrorDetection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
8.4 ParityBitsandtheExclusiveOR . . . . . . . . . . . . . . . . . . . . . . . . . . 265
8.5 HuffmanTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
8.6 HuffmanCodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
8.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
9 TheWaroftheWords 289
Computerarchitectureandmachinelevelprogramming
9.1 Hello,MARS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
9.2 TheTemperatureonMARS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
9.3 Corewar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
9.4 Self-ReferentialCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
9.5 (cid:169) Clones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
9.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
10 I’dLiketoHaveanArgument,Please 315
AprogramthatunderstandsEnglish(ordoesit?)
10.1 OverviewofELIZA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
10.2 SentencePatterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
10.3 BuildingResponsesfromPartsofSentences . . . . . . . . . . . . . . . . . . . 325
10.4 Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
10.5 AnAlgorithmforHavingaConversation . . . . . . . . . . . . . . . . . . . . . 333
10.6 (cid:169) WritingScriptsforELIZA . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
10.7 ELIZAandtheTuringTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
10.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
11 TheMusicoftheSpheres 349
ComputersimulationandtheN-bodyproblem
11.1 RunningAroundinCircles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
11.2 TheForceofGravity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
11.3 ForceVectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
11.4 N-BodySimulationoftheSolarSystem . . . . . . . . . . . . . . . . . . . . . . 369
11.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
12 TheTravelingSalesman 379
Ageneticalgorithmforacomputationallydemandingproblem
12.1 MapsandTours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
12.2 ExhaustiveSearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
12.3 RandomSearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
viii
12.4 PointMutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
12.5 TheGeneticAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
12.6 Crossovers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
12.7 (cid:169)TSPHelperFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
12.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Index 417