0% found this document useful (0 votes)
20 views57 pages

Fundamentals of Python From First Programs Through Data Structures 1st Edition Kenneth A. Lambert

The document promotes the ebook 'Fundamentals of Python: From First Programs through Data Structures' by Kenneth A. Lambert, providing a link for download and additional resources. It includes recommendations for other related ebooks and a brief profile of Amina Elgouacem, a computer science graduate. The content also outlines various chapters and topics covered in the Python textbook.

Uploaded by

rusdeesiabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views57 pages

Fundamentals of Python From First Programs Through Data Structures 1st Edition Kenneth A. Lambert

The document promotes the ebook 'Fundamentals of Python: From First Programs through Data Structures' by Kenneth A. Lambert, providing a link for download and additional resources. It includes recommendations for other related ebooks and a brief profile of Amina Elgouacem, a computer science graduate. The content also outlines various chapters and topics covered in the Python textbook.

Uploaded by

rusdeesiabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Visit ebookfinal.

com to download the full version and


explore more ebooks or textbooks

Fundamentals of Python From First Programs through


Data Structures 1st Edition Kenneth A. Lambert

_____ Click the link below to download _____


https://ebookfinal.com/download/fundamentals-of-python-from-
first-programs-through-data-structures-1st-edition-kenneth-
a-lambert/

Explore and download more ebooks or textbook at ebookfinal.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Fundamentals of Data Structures in C 2nd Edition Ellis


Horowitz

https://ebookfinal.com/download/fundamentals-of-data-structures-
in-c-2nd-edition-ellis-horowitz/

Problem Solving in Data Structures Algorithms Using Python


Programming Interview Guide 1st Edition Hemant Jain

https://ebookfinal.com/download/problem-solving-in-data-structures-
algorithms-using-python-programming-interview-guide-1st-edition-
hemant-jain/

Governing China From Revolution Through Reform 2nd Edition


Kenneth Lieberthal

https://ebookfinal.com/download/governing-china-from-revolution-
through-reform-2nd-edition-kenneth-lieberthal/

Classic Data Structures Samanta

https://ebookfinal.com/download/classic-data-structures-samanta/
A Common Sense Guide to Data Structures and Algorithms in
Python Volume 1 Level Up Your Core Programming Skills Jay
Wengrow
https://ebookfinal.com/download/a-common-sense-guide-to-data-
structures-and-algorithms-in-python-volume-1-level-up-your-core-
programming-skills-jay-wengrow/

Web Scraping with Python Collecting Data from the Modern


Web 1st Edition Ryan Mitchell

https://ebookfinal.com/download/web-scraping-with-python-collecting-
data-from-the-modern-web-1st-edition-ryan-mitchell/

Data Structures in Java A Laboratory Course 1st Edition


Sandra Andersen

https://ebookfinal.com/download/data-structures-in-java-a-laboratory-
course-1st-edition-sandra-andersen/

Starting Out with C From Control Structures through


Objects 7th Edition Edition Tony Gaddis

https://ebookfinal.com/download/starting-out-with-c-from-control-
structures-through-objects-7th-edition-edition-tony-gaddis/

Data Structures Demystified 1st Edition James Edward Keogh

https://ebookfinal.com/download/data-structures-demystified-1st-
edition-james-edward-keogh/
Fundamentals of Python From First Programs through
Data Structures 1st Edition Kenneth A. Lambert Digital
Instant Download
Author(s): Kenneth A. Lambert
ISBN(s): 9781423902188, 1423902181
Edition: 1
File Details: PDF, 11.50 MB
Year: 2009
Language: english
5 minutes with...
Amina Elgouacem
Amina Elgouacem graduated from Washington
and Lee University in 2003 with a BS in Computer
Science and a double major in French and is now
working at Primescape Solutions, a government
contractor company, as a Senior Web Developer/
Senior Consultant.

One piece of advice for first year students:


The Computer Science major can be challenging and intimidating at first, but never
give up. Take advantage of the summer internships which will give you hands-on
experience. That way you will have a better idea of what you would like to do in the
future (networking, Web development, research, teaching). Also, take advantage
of on-campus work opportunities at the Help Desk or a multimedia center, creating
applications for departments and student organizations, or even doing research for a
professor.

What’s the most interesting project you’ve worked on as a professional?


I have worked on several interesting projects for different government agencies. One
of the most recent of these was AEIS (Academic Exchange Information System) at the
Bureau of Educational and Cultural Affairs at the Department of State, Washington
DC. AEIS is Web-based and gathers data received from exchange program agencies
and institutions. It also provides the means for capturing and modifying as well as
reporting on program data. I have worked on all aspects of the software development
life cycle, but the rewarding part at the end of the day is to see the system live and
working, and to see users happy with it. I have learned to take simple, basic con-
cepts that I learned from my computer science courses and use them in learning new
programming languages, in daily research at work, and in analyzing and problem
solving.

Where do you see yourself in ten years?


I see myself a leader in technology, carrying a Master’s degree and contributing my
abilities in an innovative, challenging, and rewarding environment. I also see myself
teaching and mentoring new graduates, showing them the path to advancement and
success.

1423902181_ifc_se.indd 1 11/19/08 8:52:19 AM


Fundamentals of
Python:
From First Programs
Through Data Structures

Kenneth A. Lambert
Martin Osborne, Contributing Author

Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
Fundamentals of Python: From First © 2010 Course Technology, Cengage Learning
Programs Through Data Structures ALL RIGHTS RESERVED. No part of this work covered by the copyright
Kenneth A. Lambert herein may be reproduced, transmitted, stored or used in any form or by
any means graphic, electronic, or mechanical, including but not limited to
Executive Editor: Marie Lee
photocopying, recording, scanning, digitizing, taping, Web distribution,
Acquisitions Editor: Amy Jollymore information networks, or information storage and retrieval systems, except
Senior Product Manager: Alyssa Pratt as permitted under Section 107 or 108 of the 1976 United States Copyright
Development Editor: Ann Shaffer Act, without the prior written permission of the publisher.

Editorial Assistant: Julia Leroux-Lindsey


For product information and technology assistance, contact us at
Marketing Manager: Bryant Chrzan
Cengage Learning Customer & Sales Support, 1-800-354-9706
Content Project Manager: Matt Hutchinson For permission to use material from this text or product, submit all
Art Director: Marissa Falco requests online at www.cengage.com/permissions
Further permissions questions can be emailed to
Compositor: Gex Publishing Services
[email protected]

ISBN-13: 978-1-4239-0218-8
ISBN-10: 1-4239-0218-1

Course Technology
25 Thomson Place
Boston, Massachusetts 02210
USA

Cengage Learning is a leading provider of customized learning solutions


with office locations around the globe, including Singapore, the United
Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at:
international.cengage.com/region

Cengage Learning products are represented in Canada by Nelson


Education, Ltd.

For your lifelong learning solutions, visit course.cengage.com.

Purchase any of our products at your local college store or at our preferred
online store www.ichapters.com.

Some of the product names and company names used in this book have
been used for identification purposes only and may be trademarks or regis-
tered trademarks of their respective manufacturers and sellers.

Any fictional data related to persons or companies or URLs used through-


out this book is intended for instructional purposes only. At the time this
book was printed, any such data was fictional and not belonging to any real
persons or companies.

Course Technology, a part of Cengage Learning, reserves the right to revise this
publication and make changes from time to time in its content without notice.

The programs in this book are for instructional purposes only.


They have been tested with care, but are not guaranteed for any particular
intent beyond educational purposes. The author and the publisher do not
offer any warranties or representations, nor do they accept any liabilities
with respect to the programs.

Printed in Canada
1 2 3 4 5 6 7 12 11 10 09 08
Table of Contents
[CHAPTER] 1 INTRODUCTION
1.1 Two Fundamental Ideas of Computer Science: Algorithms and Information
Processing .................................................................................................................2
1.1.1 Algorithms ................................................................................................2
1.1.2 Information Processing............................................................................4
1.1 Exercises....................................................................................................................5
1.2 The Structure of a Modern Computer System .......................................................6
1.2.1 Computer Hardware ................................................................................6
1.2.2 Computer Software..................................................................................8
1.2 Exercises..................................................................................................................10
1.3 A Not-So-Brief History of Computing Systems...................................................10
1.3.1 Before Electronic Digital Computers ...................................................11
1.3.2 The First Electronic Digital Computers (1940–1950) .........................15
1.3.3 The First Programming Languages (1950–1965).................................16
1.3.4 Integrated Circuits, Interaction, and Timesharing (1965–1975) .........18
1.3.5 Personal Computing and Networks (1975–1990) ................................19
1.3.6 Consultation, Communication, and Ubiquitous Computing
(1990–Present)........................................................................................21
1.4 Getting Started with Python Programming..........................................................23
1.4.1 Running Code in the Interactive Shell .................................................23
1.4.2 Input, Processing, and Output...............................................................25
1.4.3 Editing, Saving, and Running a Script ..................................................27
1.4.4 Behind the Scenes: How Python Works ...............................................29
1.4 Exercises..................................................................................................................30
1.5 Detecting and Correcting Syntax Errors...............................................................30
1.5 Exercises..................................................................................................................32
Suggestions for Further Reading ...........................................................................32
Summary .................................................................................................................32
Review Questions ...................................................................................................35
Projects....................................................................................................................37

[CHAPTER] 2 SOFTWARE DEVELOPMENT, DATA TYPES, AND EXPRESSIONS 39


2.1 The Software Development Process .....................................................................40
2.1 Exercises..................................................................................................................43
2.2 Case Study: Income Tax Calculator.......................................................................43
2.2.1 Request ...................................................................................................43
2.2.2 Analysis ...................................................................................................44
2.2.3 Design.....................................................................................................44
2.2.4 Implementation (Coding) ......................................................................45
2.2.5 Testing ....................................................................................................46
2.3 Strings, Assignment, and Comments.....................................................................47
2.3.1 Data Types..............................................................................................47
2.3.2 String Literals.........................................................................................48
2.3.3 Escape Sequences ...................................................................................50
2.3.4 String Concatenation .............................................................................50
2.3.5 Variables and the Assignment Statement ..............................................51
2.3.6 Program Comments and Docstrings.....................................................52
2.3 Exercises..................................................................................................................53
2.4 Numeric Data Types and Character Sets ..............................................................54
2.4.1 Integers and Long Integers....................................................................54
2.4.2 Floating-Point Numbers........................................................................55
2.4.3 Character Sets ........................................................................................56
2.4 Exercises..................................................................................................................57
2.5 Expressions .............................................................................................................58
2.5.1 Arithmetic Expressions ..........................................................................58
2.5.2 Mixed-Mode Arithmetic and Type Conversions ..................................60
2.5 Exercises..................................................................................................................63
2.6 Using Functions and Modules ...............................................................................63
2.6.1 Calling Functions: Arguments and Return Values................................64
2.6.2 The math Module .................................................................................65
2.6.3 The Main Module..................................................................................66
2.6.4 Program Format and Structure .............................................................67
2.6.5 Running a Script from a Terminal Command Prompt ........................68
2.6 Exercises..................................................................................................................70
Summary .................................................................................................................70
Review Questions ...................................................................................................72
Projects....................................................................................................................73

[CHAPTER] 3 CONTROL STATEMENTS 75


3.1 Definite Iteration: The for Loop.........................................................................76
3.1.1 Executing a Statement a Given Number of Times ..............................76
3.1.2 Count-Controlled Loops .......................................................................77
3.1.3 Augmented Assignment .........................................................................79
3.1.4 Loop Errors: Off-by-One Error............................................................80
3.1.5 Traversing the Contents of a Data Sequence........................................80
3.1.6 Specifying the Steps in the Range .........................................................81
3.1.7 Loops That Count Down ......................................................................82
3.1 Exercises..................................................................................................................83
3.2 Formatting Text for Output ...................................................................................83
3.2 Exercises..................................................................................................................86
3.3 Case Study: An Investment Report........................................................................87
3.3.1 Request ...................................................................................................87
3.3.2 Analysis ...................................................................................................87
3.3.3 Design.....................................................................................................88
3.3.4 Implementation (Coding) ......................................................................88
3.3.5 Testing ....................................................................................................90
3.4 Selection: if and if-else Statements ...............................................................91
3.4.1 The Boolean Type, Comparisons, and Boolean Expressions ...............91
3.4.2 if-else Statements .............................................................................92
3.4.3 One-Way Selection Statements.............................................................94
3.4.4 Multi-way if Statements ......................................................................95
3.4.5 Logical Operators and Compound Boolean Expressions.....................97
3.4.6 Short-Circuit Evaluation .......................................................................99
3.4.7 Testing Selection Statements ...............................................................100
3.4 Exercises................................................................................................................101
3.5 Conditional Iteration: The while Loop ............................................................102
3.5.1 The Structure and Behavior of a while Loop ..................................102
3.5.2 Count Control with a while Loop....................................................104
3.5.3 The while True Loop and the break Statement ..........................105
3.5.4 Random Numbers................................................................................107
3.5.5 Loop Logic, Errors, and Testing .........................................................109
3.5 Exercises................................................................................................................109
3.6 Case Study: Approximating Square Roots...........................................................110
3.6.1 Request .................................................................................................110
3.6.2 Analysis .................................................................................................110
3.6.3 Design...................................................................................................110
3.6.4 Implementation (Coding) ....................................................................112
3.6.5 Testing ..................................................................................................113
Summary ...............................................................................................................113
Review Questions .................................................................................................116
Projects..................................................................................................................118

[CHAPTER] 4 STRINGS AND TEXT FILES 121


4.1 Accessing Characters and Substrings in Strings..................................................122
4.1.1 The Structure of Strings......................................................................122
4.1.2 The Subscript Operator.......................................................................123
4.1.3 Slicing for Substrings ...........................................................................124
4.1.4 Testing for a Substring with the in Operator ....................................125
4.1 Exercises................................................................................................................126
4.2 Data Encryption ...................................................................................................126
4.2 Exercises................................................................................................................129
4.3 Strings and Number Systems...............................................................................129
4.3.1 The Positional System for Representing Numbers............................130
4.3.2 Converting Binary to Decimal ............................................................131
4.3.3 Converting Decimal to Binary ............................................................132
4.3.4 Conversion Shortcuts...........................................................................133
4.3.5 Octal and Hexadecimal Numbers .......................................................134
4.3 Exercises................................................................................................................136
4.4 String Methods .....................................................................................................136
4.4 Exercises................................................................................................................140
4.5 Text Files...............................................................................................................141
4.5.1 Text Files and Their Format................................................................141
4.5.2 Writing Text to a File ..........................................................................142
4.5.3 Writing Numbers to a File ..................................................................142
4.5.4 Reading Text from a File .....................................................................143
4.5.5 Reading Numbers from a File .............................................................145
4.5.6 Accessing and Manipulating Files and Directories on Disk...............146
4.5 Exercises................................................................................................................148
4.6 Case Study: Text Analysis.....................................................................................148
4.6.1 Request .................................................................................................149
4.6.2 Analysis .................................................................................................149
4.6.3 Design...................................................................................................150
4.6.4 Implementation (Coding) ....................................................................151
4.6.5 Testing ..................................................................................................152
Summary ...............................................................................................................153
Review Questions .................................................................................................154
Projects..................................................................................................................156

[CHAPTER] 5 LISTS AND DICTIONARIES 159


5.1 Lists .......................................................................................................................160
5.1.1 List Literals and Basic Operators ........................................................160
5.1.2 Replacing an Element in a List ...........................................................163
5.1.3 List Methods for Inserting and Removing Elements .........................165
5.1.4 Searching a List....................................................................................167
5.1.5 Sorting a List........................................................................................168
5.1.6 Mutator Methods and the Value None ...............................................168
5.1.7 Aliasing and Side Effects......................................................................169
5.1.8 Equality: Object Identity and Structural Equivalence........................171
5.1.9 Example: Using a List to Find the Median of a Set of Numbers ......172
5.1.10 Tuples ...................................................................................................173
5.1 Exercises................................................................................................................174
5.2 Defining Simple Functions ..................................................................................175
5.2.1 The Syntax of Simple Function Definitions .......................................175
5.2.2 Parameters and Arguments..................................................................176
5.2.3 The return Statement.......................................................................177
5.2.4 Boolean Functions................................................................................177
5.2.5 Defining a main Function...................................................................178
5.2 Exercises................................................................................................................179
5.3 Case Study: Generating Sentences ......................................................................179
5.3.1 Request .................................................................................................179
5.3.2 Analysis .................................................................................................179
5.3.3 Design...................................................................................................180
5.3.4 Implementation (Coding) ....................................................................182
5.3.5 Testing ..................................................................................................183
5.4 Dictionaries...........................................................................................................183
5.4.1 Dictionary Literals ...............................................................................183
5.4.2 Adding Keys and Replacing Values .....................................................184
5.4.3 Accessing Values...................................................................................185
5.4.4 Removing Keys ....................................................................................186
5.4.5 Traversing a Dictionary .......................................................................186
5.4.6 Example: The Hexadecimal System Revisited....................................188
5.4.7 Example: Finding the Mode of a List of Values .................................189
5.4 Exercises................................................................................................................190
5.5 Case Study: Nondirective Psychotherapy ...........................................................191
5.5.1 Request .................................................................................................191
5.5.2 Analysis .................................................................................................191
5.5.3 Design...................................................................................................192
5.5.4 Implementation (Coding) ....................................................................193
5.5.5 Testing ..................................................................................................195
Summary ...............................................................................................................195
Review Questions .................................................................................................196
Projects..................................................................................................................198

[CHAPTER] 6 DESIGN WITH FUNCTIONS 201


6.1 Functions as Abstraction Mechanisms.................................................................202
6.1.1 Functions Eliminate Redundancy........................................................202
6.1.2 Functions Hide Complexity ................................................................203
6.1.3 Functions Support General Methods with Systematic Variations .....204
6.1.4 Functions Support the Division of Labor ...........................................205
6.1 Exercises................................................................................................................205
6.2 Problem Solving with Top-Down Design ...........................................................206
6.2.1 The Design of the Text-Analysis Program .........................................206
6.2.2 The Design of the Sentence-Generator Program ..............................207
6.2.3 The Design of the Doctor Program ...................................................209
6.2 Exercises................................................................................................................210
6.3 Design with Recursive Functions ........................................................................211
6.3.1 Defining a Recursive Function ............................................................211
6.3.2 Tracing a Recursive Function ..............................................................213
6.3.3 Using Recursive Definitions to Construct Recursive Functions .......214
6.3.4 Recursion in Sentence Structure .........................................................214
6.3.5 Infinite Recursion.................................................................................215
6.3.6 The Costs and Benefits of Recursion..................................................216
6.3 Exercises................................................................................................................218
6.4 Case Study: Gathering Information from a File System ....................................219
6.4.1 Request .................................................................................................219
6.4.2 Analysis .................................................................................................220
6.4.3 Design...................................................................................................222
6.4.4 Implementation (Coding) ....................................................................224
6.5 Managing a Program’s Namespace ......................................................................227
6.5.1 Module Variables, Parameters, and Temporary Variables ..................227
6.5.2 Scope.....................................................................................................228
6.5.3 Lifetime ................................................................................................229
6.5.4 Default (Keyword) Arguments ............................................................230
6.5 Exercises................................................................................................................232
6.6 Higher-Order Functions (Advanced Topic) ........................................................233
6.6.1 Functions as First-Class Data Objects ................................................233
6.6.2 Mapping................................................................................................234
6.6.3 Filtering ................................................................................................236
6.6.4 Reducing...............................................................................................237
6.6.5 Using lambda to Create Anonymous Functions...............................237
6.6.6 Creating Jump Tables ..........................................................................238
6.6 Exercises................................................................................................................239
Summary ...............................................................................................................240
Review Questions .................................................................................................242
Projects..................................................................................................................244

[CHAPTER] 7 SIMPLE GRAPHICS AND IMAGE PROCESSING 247


7.1 Simple Graphics ...................................................................................................248
7.1.1 Overview of Turtle Graphics ...............................................................248
7.1.2 Turtle Operations.................................................................................249
7.1.3 Object Instantiation and the turtlegraphics Module ................251
7.1.4 Drawing Two-Dimensional Shapes .....................................................254
7.1.5 Taking a Random Walk........................................................................255
7.1.6 Colors and the RGB System................................................................256
7.1.7 Example: Drawing with Random Colors ............................................257
7.1.8 Using the str Function with Objects ................................................259
7.1 Exercises................................................................................................................260
7.2 Case Study: Recursive Patterns in Fractals..........................................................261
7.2.1 Request .................................................................................................262
7.2.2 Analysis .................................................................................................262
7.2.3 Design...................................................................................................263
7.2.4 Implementation (Coding) ....................................................................265
7.3 Image Processing .................................................................................................266
7.3.1 Analog and Digital Information .........................................................266
7.3.2 Sampling and Digitizing Images .........................................................267
7.3.3 Image File Formats ..............................................................................267
7.3.4 Image-Manipulation Operations .........................................................268
7.3.5 The Properties of Images ....................................................................269
7.3.6 The images Module ..........................................................................269
7.3.7 A Loop Pattern for Traversing a Grid ................................................273
7.3.8 A Word on Tuples................................................................................274
7.3.9 Converting an Image to Black and White ..........................................275
7.3.10 Converting an Image to Grayscale......................................................277
7.3.11 Copying an Image ................................................................................278
7.3.12 Blurring an Image ................................................................................279
7.3.13 Edge Detection ....................................................................................280
7.3.14 Reducing the Image Size .....................................................................281
7.3 Exercises................................................................................................................283
Summary ...............................................................................................................284
Review Questions .................................................................................................285
Projects..................................................................................................................287

[CHAPTER] 8 DESIGN WITH CLASSES 291


8.1 Getting Inside Objects and Classes .....................................................................292
8.1.1 A First Example: The Student Class................................................293
8.1.2 Docstrings ............................................................................................296
8.1.3 Method Definitions..............................................................................296
8.1.4 The __init__ Method and Instance Variables................................297
8.1.5 The __str__ Method........................................................................298
8.1.6 Accessors and Mutators .......................................................................298
8.1.7 The Lifetime of Objects ......................................................................299
8.1.8 Rules of Thumb for Defining a Simple Class.....................................300
8.1 Exercises................................................................................................................301
8.2 Case Study: Playing the Game of Craps .............................................................301
8.2.1 Request .................................................................................................301
8.2.2 Analysis .................................................................................................301
8.2.3 Design...................................................................................................302
8.2.4 Implementation (Coding) ....................................................................304
8.3 Data-Modeling Examples.....................................................................................307
8.3.1 Rational Numbers ................................................................................307
8.3.2 Rational Number Arithmetic and Operator Overloading..................309
8.3.3 Comparisons and the __cmp__ Method............................................310
8.3.4 Equality and the __eq__ Method ......................................................311
8.3.5 Savings Accounts and Class Variables .................................................312
8.3.6 Putting the Accounts into a Bank........................................................315
8.3.7 Using cPickle for Permanent Storage of Objects ..........................317
8.3.8 Input of Objects and the try-except Statement............................318
8.3.9 Playing Cards .......................................................................................319
8.3 Exercises................................................................................................................323
8.4 Case Study: An ATM............................................................................................323
8.4.1 Request .................................................................................................323
8.4.2 Analysis .................................................................................................323
8.4.3 Design...................................................................................................325
8.4.4 Implementation (Coding) ....................................................................327
8.5 Structuring Classes with Inheritance and Polymorphism...................................329
8.5.1 Inheritance Hierarchies and Modeling ...............................................330
8.5.2 Example: A Restricted Savings Account..............................................331
8.5.3 Example: The Dealer and a Player in the Game of Blackjack ...........333
8.5.4 Polymorphic Methods..........................................................................338
8.5.5 Abstract Classes ...................................................................................338
8.5.6 The Costs and Benefits of Object-Oriented Programming...............339
8.5 Exercises................................................................................................................341
Summary ...............................................................................................................341
Review Questions .................................................................................................343
Projects..................................................................................................................344

[CHAPTER] 9 GRAPHICAL USER INTERFACES 347


9.1 The Behavior of Terminal-Based Programs and GUI-Based Programs............348
9.1.1 The Terminal-Based Version ...............................................................348
9.1.2 The GUI-Based Version......................................................................349
9.1.3 Event-Driven Programming................................................................351
9.1 Exercises................................................................................................................353
9.2 Coding Simple GUI-Based Programs .................................................................353
9.2.1 Windows and Labels............................................................................354
9.2.2 Displaying Images ................................................................................355
9.2.3 Command Buttons and Responding to Events...................................356
9.2.4 Viewing the Images of Playing Cards .................................................358
9.2.5 Entry Fields for the Input and Output of Text ...................................361
9.2.6 Using Pop-up Dialog Boxes ................................................................363
9.2 Exercises................................................................................................................364
9.3 Case Study: A GUI-Based ATM..........................................................................365
9.3.1 Request .................................................................................................365
9.3.2 Analysis .................................................................................................365
9.3.3 Design...................................................................................................366
9.3.4 Implementation (Coding) ....................................................................367
9.4 Other Useful GUI Resources ..............................................................................370
9.4.1 Colors ...................................................................................................371
9.4.2 Text Attributes......................................................................................371
9.4.3 Sizing and Justifying an Entry .............................................................372
9.4.4 Sizing the Main Window.....................................................................373
9.4.5 Grid Attributes .....................................................................................374
9.4.6 Using Nested Frames to Organize Components................................378
9.4.7 Multi-Line Text Widgets .....................................................................379
9.4.8 Scrolling List Boxes .............................................................................382
9.4.9 Mouse Events .......................................................................................385
9.4.10 Keyboard Events ..................................................................................386
9.4 Exercises................................................................................................................387
Summary ...............................................................................................................388
Review Questions .................................................................................................389
Projects..................................................................................................................390

[CHAPTER] 10 MULTITHREADING, NETWORKS, AND CLIENT/SERVER


PROGRAMMING 393
10.1 Threads and Processes .........................................................................................394
10.1.1 Threads.................................................................................................395
10.1.2 Sleeping Threads..................................................................................398
10.1.3 Producer, Consumer, and Synchronization ........................................400
10.1 Exercises................................................................................................................407
10.2 Networks, Clients, and Servers............................................................................407
10.2.1 IP Addresses .........................................................................................407
10.2.2 Ports, Servers, and Clients...................................................................409
10.2.3 Sockets and a Day/Time Client Script................................................410
10.2.4 A Day/Time Server Script ...................................................................412
10.2.5 A Two-Way Chat Script.......................................................................414
10.2.6 Handling Multiple Clients Concurrently ...........................................416
10.2.7 Setting Up Conversations for Others .................................................418
10.2 Exercises................................................................................................................420
10.3 Case Study: A Multi-Client Chat Room .............................................................421
10.3.1 Request ................................................................................................421
10.3.2 Analysis ................................................................................................421
10.3.3 Design...................................................................................................422
10.3.4 Implementation (Coding) ....................................................................423
Summary ...............................................................................................................425
Review Questions .................................................................................................426
Projects..................................................................................................................428
[CHAPTER] 11 SEARCHING, SORTING, AND COMPLEXITY ANALYSIS 431
11.1 Measuring the Efficiency of Algorithms..............................................................432
11.1.1 Measuring the Run Time of an Algorithm ........................................432
11.1.2 Counting Instructions ..........................................................................435
11.1.3 Measuring the Memory Used by an Algorithm..................................438
11.1 Exercises................................................................................................................439
11.2 Complexity Analysis .............................................................................................439
11.2.1 Orders of Complexity ..........................................................................439
11.2.2 Big-O Notation ....................................................................................441
11.2.3 The Role of the Constant of Proportionality .....................................442
11.2 Exercises................................................................................................................443
11.3 Search Algorithms ................................................................................................443
11.3.1 Search for a Minimum ........................................................................444
11.3.2 Linear Search of a List ........................................................................444
11.3.3 Best-Case, Worst-Case, and Average-Case Performance...................445
11.3.4 Binary Search of a List.........................................................................446
11.3.5 Comparing Data Items and the cmp Function ...................................448
11.3 Exercises................................................................................................................449
11.4 Sort Algorithms ....................................................................................................450
11.4.1 Selection Sort .......................................................................................450
11.4.2 Bubble Sort...........................................................................................452
11.4.3 Insertion Sort .......................................................................................453
11.4.4 Best-Case, Worst-Case, and Average-Case Performance Revisited...455
11.4 Exercises................................................................................................................456
11.5 An Exponential Algorithm: Recursive Fibonacci ................................................457
11.6 Converting Fibonacci to a Linear Algorithm......................................................458
11.7 Case Study: An Algorithm Profiler......................................................................460
11.7.1 Request .................................................................................................460
11.7.2 Analysis .................................................................................................460
11.7.3 Design...................................................................................................462
11.7.4 Implementation (Coding) ....................................................................463
Summary ...............................................................................................................466
Review Questions .................................................................................................467
Projects..................................................................................................................468

[CHAPTER] 12 TOOLS FOR DESIGN, DOCUMENTATION, AND TESTING 471


12.1 Software Design with UML.................................................................................472
12.1.1 UML and Modeling.............................................................................473
12.1.2 Use Case Diagrams ..............................................................................473
12.1.3 Class Diagrams.....................................................................................476
12.1.4 Collaboration Diagrams.......................................................................479
12.1.5 From Collaboration Diagram to Code ...............................................481
12.1.6 Inheritance............................................................................................482
12.1 Exercises................................................................................................................483
12.2 Documentation .....................................................................................................484
12.2.1 Writing APIs ........................................................................................484
12.2.2 Revisiting the Student Class ............................................................485
12.2.3 Preconditions and Postconditions ......................................................487
12.2.4 Enforcing Preconditions with Exceptions...........................................488
12.2.5 Web-Based Documentation with pydoc............................................490
12.2 Exercises................................................................................................................493
12.3 Testing...................................................................................................................493
12.3.1 What to Test.........................................................................................494
12.3.2 Three Approaches to Choosing Test Data..........................................494
12.3.2.1 Haphazard Testing ..........................................................495
12.3.2.2 Black-Box Testing ...........................................................495
12.3.2.3 White-Box Testing..........................................................495
12.3.3 When to Test........................................................................................496
12.3.3.1 Unit Testing ....................................................................496
12.3.3.2 Integration Testing..........................................................496
12.3.3.3 Acceptance Testing .........................................................496
12.3.3.4 Regression Testing ..........................................................497
12.3.4 Proofs of Program Correctness ...........................................................497
12.3.5 Unit Testing in Python ........................................................................498
12.3 Exercises................................................................................................................502
Suggestions for Further Reading .........................................................................502
Summary ...............................................................................................................503
Review Questions .................................................................................................504
Projects..................................................................................................................505

[CHAPTER] 13 COLLECTIONS, ARRAYS, AND LINKED STRUCTURES 507


13.1 Overview of Collections .......................................................................................508
13.1.1 Linear Collections................................................................................508
13.1.2 Hierarchical Collections ......................................................................508
13.1.3 Graph Collections ................................................................................509
13.1.4 Unordered Collections ........................................................................510
13.1.5 Operations on Collections ...................................................................510
13.1.6 Abstraction and Abstract Data Types ..................................................512
13.1 Exercises................................................................................................................513
13.2 Data Structures for Implementing Collections: Arrays ......................................513
13.2.1 The Array Data Structure....................................................................513
13.2.2 Random Access and Contiguous Memory ..........................................516
13.2.3 Static Memory and Dynamic Memory................................................517
13.2.4 Physical Size and Logical Size.............................................................518
13.2 Exercises................................................................................................................519
13.3 Operations on Arrays ...........................................................................................519
13.3.1 Increasing the Size of an Array............................................................520
13.3.2 Decreasing the Size of an Array ..........................................................521
13.3.3 Inserting an Item into an Array That Grows......................................521
13.3.4 Removing an Item from an Array .......................................................523
13.3.5 Complexity Trade-Off: Time, Space, and Arrays ...............................524
13.3 Exercises................................................................................................................525
13.4 Two-Dimensional Arrays (Grids).........................................................................525
13.4.1 Processing a Grid .................................................................................526
13.4.2 Creating and Initializing a Grid ..........................................................526
13.4.3 Defining a Grid Class ..........................................................................527
13.4.4 Ragged Grids and Multidimensional Arrays.......................................528
13.4 Exercises................................................................................................................528
13.5 Linked Structures .................................................................................................529
13.5.1 Singly Linked Structures and Doubly Linked Structures ..................530
13.5.2 Noncontiguous Memory and Nodes...................................................531
13.5.3 Defining a Singly Linked Node Class.................................................533
13.5.4 Using the Singly Linked Node Class ..................................................534
13.5 Exercises................................................................................................................536
13.6 Operations on Singly Linked Structures .............................................................536
13.6.1 Traversal ...............................................................................................536
13.6.2 Searching ..............................................................................................538
13.6.3 Replacement .........................................................................................539
13.6.4 Inserting at the Beginning ...................................................................539
13.6.5 Inserting at the End .............................................................................540
13.6.6 Removing at the Beginning .................................................................542
13.6.7 Removing at the End ...........................................................................543
13.6.8 Inserting at Any Position .....................................................................544
13.6.9 Removing at Any Position ...................................................................547
13.6.10 Complexity Trade-Off: Time, Space, and Singly Linked Structures.549
13.6 Exercises................................................................................................................550
13.7 Variations on a Link .............................................................................................550
13.7.1 A Circular Linked Structure with a Dummy Header Node ..............550
13.7.2 Doubly Linked Structures ...................................................................552
13.7 Exercises................................................................................................................555
Summary ..............................................................................................................555
Review Questions .................................................................................................556
Projects ..............................................................................................................557

[CHAPTER] 14 LINEAR COLLECTIONS: STACKS 561


14.1 Overview of Stacks ...............................................................................................562
14.2 Using a Stack ........................................................................................................563
14.2.1 The Stack Interface..............................................................................564
14.2.2 Instantiating a Stack .............................................................................565
14.2.3 Example Application: Matching Parentheses......................................566
14.2 Exercises................................................................................................................568
14.3 Three Applications of Stacks ...............................................................................569
14.3.1 Evaluating Arithmetic Expressions......................................................569
14.3.2 Evaluating Postfix Expressions ............................................................570
14.3.2 Exercises................................................................................................................572
14.3.3 Converting Infix to Postfix ..................................................................572
14.3.3 Exercises................................................................................................................575
14.3.4 Backtracking .........................................................................................575
14.3.5 Memory Management..........................................................................577
14.4 Implementations of Stacks ...................................................................................580
14.4.1 Test Driver............................................................................................580
14.4.2 Array Implementation..........................................................................581
14.4.3 Linked Implementation .......................................................................584
14.4.4 Time and Space Analysis of the Two Implementations......................587
14.4 Exercises................................................................................................................588
14.5 Case Study: Evaluating Postfix Expressions ........................................................589
14.5.1 Request .................................................................................................589
14.5.2 Analysis .................................................................................................589
14.5.3 Design...................................................................................................592
14.5.3.1 Instance Variables and Methods for Class
PFEvaluatorView ......................................................593
14.5.3.2 Instance Variables and Methods for Class
PFEvaluatorModel ....................................................594
14.5.3.3 Instance Variables and Methods for Class
PFEvaluator................................................................594
14.5.3.4 Instance Variables and Methods for Class Scanner ....595
14.5.3.5 Instance and Class Variables and Methods for Class
Token .............................................................................595
14.5.4 Implementation ....................................................................................596
Summary ...............................................................................................................599
Review Questions .................................................................................................600
Projects..................................................................................................................601

[CHAPTER] 15 LINEAR COLLECTIONS: QUEUES 603


15.1 Overview of Queues .............................................................................................604
15.2 The Queue Interface and Its Use ........................................................................605
15.2 Exercises................................................................................................................608
15.3 Two Applications of Queues ................................................................................609
15.3.1 Simulations ...........................................................................................609
15.3.2 Round-Robin CPU Scheduling...........................................................611
15.3 Exercises................................................................................................................612
15.4 Implementations of Queues .................................................................................612
15.4.1 A Linked Implementation....................................................................612
15.4.2 An Array Implementation ....................................................................614
15.4.2.1 A First Attempt ...............................................................615
15.4.2.2 A Second Attempt...........................................................615
15.4.2.3 A Third Attempt .............................................................616
15.4.3 Time and Space Analysis for the Two Implementations ....................617
15.4 Exercises................................................................................................................618
15.5 Case Study: Simulating a Supermarket Checkout Line......................................618
15.5.1 Request .................................................................................................618
15.5.2 Analysis .................................................................................................618
15.5.3 The Interface........................................................................................619
15.5.4 Classes and Responsibilities.................................................................620
15.6 Priority Queues ....................................................................................................627
15.6 Exercise .................................................................................................................632
15.7 Case Study: An Emergency Room Scheduler .....................................................633
15.7.1 Request .................................................................................................633
15.7.2 Analysis .................................................................................................633
15.7.3 Classes...................................................................................................635
15.7.4 Design and Implementation ................................................................635
Summary ...............................................................................................................638
Review Questions .................................................................................................638
Projects..................................................................................................................640
[CHAPTER] 16 LINEAR COLLECTIONS: LISTS 643
16.1 Overview of Lists..................................................................................................644
16.2 Using Lists ............................................................................................................645
16.2.1 Index-Based Operations.......................................................................646
16.2.2 Content-Based Operations ..................................................................646
16.2.3 Position-Based Operations ..................................................................647
16.2.4 Interfaces for Lists ...............................................................................652
16.2 Exercises................................................................................................................654
16.3 Applications of Lists .............................................................................................654
16.3.1 Heap-Storage Management.................................................................654
16.3.2 Organization of Files on a Disk...........................................................656
16.3.3 Implementation of Other ADTs..........................................................657
16.4 Indexed List Implementations .............................................................................658
16.4.1 An Array-Based Implementation of an Indexed List ..........................658
16.4.2 A Linked Implementation of an Indexed List.....................................660
16.4.3 Time and Space Analysis for the Two Implementations ....................663
16.4 Exercises................................................................................................................665
16.5 Implementing Positional Lists .............................................................................665
16.5.1 The Data Structures for a Linked Positional List ..............................665
16.5.2 Methods Used to Navigate from Beginning to End ..........................667
16.5.3 Methods Used to Navigate from End to Beginning ..........................670
16.5.4 Insertions into a Positional List...........................................................670
16.5.4 Removals from a Positional List..........................................................671
16.5.5 Time and Space Analysis of Positional List Implementations ...........672
16.5 Exercises................................................................................................................672
16.6 Iterators.................................................................................................................673
16.6.1 Using an Iterator in Python ................................................................674
16.6.2 Implementing an Iterator ....................................................................676
16.6 Exercises................................................................................................................677
16.7 Case Study: Developing a Sorted List .................................................................678
16.7.1 Request .................................................................................................678
16.7.2 Analysis .................................................................................................678
16.7.3 Design...................................................................................................679
16.7.4 Implementation (Coding) ....................................................................680
Summary ...............................................................................................................681
Review Questions .................................................................................................682
Projects..................................................................................................................683

[CHAPTER] 17 RECURSION 685


17.1 n log n Sorting ......................................................................................................686
17.1.1 Overview of Quicksort.........................................................................686
17.1.2 Partitioning...........................................................................................687
17.1.3 Complexity Analysis of Quicksort .......................................................689
17.1.4 Implementation of Quicksort ..............................................................690
17.1.5 Merge Sort ...........................................................................................692
17.1.6 Complexity Analysis for Merge Sort ...................................................695
17.1 Exercises................................................................................................................696
17.2 Recursive List Processing.....................................................................................696
17.2.1 Basic Operations on a Lisp-Like List..................................................696
17.2.2 Recursive Traversals of a Lisp-Like List .............................................698
17.2.3 Building a Lisp-Like List.....................................................................700
17.2.4 The Internal Structure of a Lisp-Like List.........................................702
17.2.5 Lists and Functional Programming.....................................................703
17.2 Exercises................................................................................................................704
17.3 Recursion and Backtracking .................................................................................705
17.3.1 A General Recursive Strategy..............................................................705
17.3.2 The Maze Problem Revisited ..............................................................706
17.3.3 The Eight Queens Problem ................................................................709
17.4 Recursive Descent and Programming Languages...............................................714
17.4.1 Introduction to Grammars ..................................................................714
17.4.2 Recognizing, Parsing, and Interpreting Sentences in a Language .......717
17.4.3 Lexical Analysis and the Scanner.........................................................717
17.4.4 Parsing Strategies .................................................................................718
17.5 Case Study: A Recursive Descent Parser.............................................................719
17.5.1 Request .................................................................................................719
17.5.2 Analysis .................................................................................................719
17.5.3 Classes...................................................................................................720
17.5.4 Implementation (Coding) ....................................................................720
17.6 The Costs and Benefits of Recursion ..................................................................722
17.6.1 No, Maybe, and Yes .............................................................................723
17.6.2 Getting Rid of Recursion.....................................................................723
17.6.3 Tail Recursion ......................................................................................724
Summary ...............................................................................................................725
Review Questions .................................................................................................726
Projects..................................................................................................................727

[CHAPTER] 18 HIERARCHICAL COLLECTIONS: TREES 733


18.1 An Overview of Trees...........................................................................................734
18.1.1 Tree Terminology.................................................................................734
18.1.2 General Trees and Binary Trees ..........................................................736
18.1.3 Recursive Definitions of Trees ............................................................736
18.1 Exercise .................................................................................................................737
18.2 Why Use a Tree? ..................................................................................................737
18.3 The Shape of Binary Trees...................................................................................740
18.3 Exercises................................................................................................................742
18.4 Three Common Applications of Binary Trees ....................................................743
18.4.1 Heaps ....................................................................................................743
18.4.2 Binary Search Trees .............................................................................744
18.4.3 Expression Trees ..................................................................................745
18.4 Exercises................................................................................................................747
18.5 Binary Tree Traversals..........................................................................................747
18.5 Exercise .................................................................................................................749
18.6 A Binary Tree ADT..............................................................................................749
18.6.1 The Interface for a Binary Tree ADT.................................................750
18.6.2 Processing a Binary Tree .....................................................................752
18.6.3 Implementing a Binary Tree................................................................753
18.6.4 The String Representation of a Tree ..................................................756
18.6 Exercises................................................................................................................757
18.7 Developing a Binary Search Tree ........................................................................757
18.7.1 The Binary Search Tree Interface .......................................................757
18.7.2 Data Structures for the Implementation of BST ................................758
18.7.3 Searching a Binary Search Tree...........................................................759
18.7.4 Inserting an Item into a Binary Search Tree.......................................760
18.7.5 Removing an Item from a Binary Search Tree ...................................762
18.7.6 Complexity Analysis of Binary Search Trees ......................................763
18.7 Exercises................................................................................................................763
18.8 Case Study: Parsing and Expression Trees ..........................................................764
18.8.1 Request .................................................................................................764
18.8.2 Analysis .................................................................................................764
18.8.3 Design and Implementation of the Node Classes ..............................765
18.8.4 Design and Implementation of the Parser Class ............................767
18.9 An Array Implementation of Binary Trees ..........................................................769
18.9 Exercises................................................................................................................770
18.10 Implementing Heaps ............................................................................................771
18.10 Exercises................................................................................................................774
18.11 Using a Heap to Implement a Priority Queue....................................................774
Summary ...............................................................................................................775
Review Questions .................................................................................................776
Projects..................................................................................................................777

[CHAPTER] 19 UNORDERED COLLECTIONS: SETS AND DICTIONARIES 779


19.1 Using Sets .............................................................................................................780
19.1.1 The Python set Class.........................................................................781
19.1.2 A Sample Session with Sets .................................................................782
19.1.3 Applications of Sets ..............................................................................783
19.1.4 Implementations of Sets ......................................................................783
19.1.5 Relationship Between Sets and Dictionaries.......................................783
19.1 Exercises................................................................................................................784
19.2 List Implementations of Sets and Dictionaries ...................................................784
19.2.1 Sets........................................................................................................784
19.2.2 Dictionaries ..........................................................................................785
19.2.3 Complexity Analysis of the List Implementations of Sets and
Dictionaries...........................................................................................................788
19.2 Exercises................................................................................................................789
19.3 Hashing Strategies................................................................................................789
19.3.1 The Relationship of Collisions to Density .........................................790
19.3.2 Hashing with Non-Numeric Keys ......................................................792
19.3.3 Linear Probing .....................................................................................794
19.3.4 Quadratic Probing................................................................................796
19.3.5 Chaining ...............................................................................................797
19.3.6 Complexity Analysis.............................................................................798
19.3 Exercises................................................................................................................800
19.4 Case Study: Profiling Hashing Strategies............................................................800
19.4.1 Request .................................................................................................800
19.4.2 Analysis .................................................................................................800
19.4.3 Design...................................................................................................803
19.4.4 Implementation ....................................................................................803
19.5 Hashing Implementation of Dictionaries ............................................................806
19.5 Exercises................................................................................................................810
19.6 Hashing Implementation of Sets .........................................................................811
19.6 Exercises................................................................................................................812
19.7 Sorted Sets and Dictionaries ................................................................................813
Summary ...............................................................................................................814
Review Questions .................................................................................................815
Projects..................................................................................................................816

[CHAPTER] 20 GRAPHS 819


20.1 Graph Terminology ..............................................................................................820
20.1 Exercises................................................................................................................824
20.2 Why Use Graphs? ................................................................................................824
20.3 Representations of Graphs ..................................................................................825
20.3.1 Adjacency Matrix..................................................................................825
20.3.2 Adjacency List ......................................................................................826
20.3.3 Analysis of the Two Representations...................................................828
20.3.4 Further Run-Time Considerations .....................................................829
20.3 Exercises................................................................................................................829
20.4 Graph Traversals...................................................................................................830
20.4.1 A Generic Traversal Algorithm ...........................................................830
20.4.2 Breadth-First and Depth-First Traversals...........................................831
20.4.3 Graph Components .............................................................................834
20.4 Exercises................................................................................................................834
20.5 Trees Within Graphs............................................................................................835
20.5.1 Spanning Trees and Forests.................................................................835
20.5.2 Minimum Spanning Tree.....................................................................835
20.5.3 Algorithms for Minimum Spanning Trees..........................................836
20.6 Topological Sort ...................................................................................................838
20.7 The Shortest-Path Problem ................................................................................840
20.7.1 Dijkstra’s Algorithm .............................................................................840
20.7.2 The Initialization Step .........................................................................841
20.7.3 The Computation Step ........................................................................842
20.7.4 Analysis .................................................................................................843
20.7 Exercises................................................................................................................843
20.8 Developing a Graph ADT ...................................................................................844
20.8.1 Example Use of the Graph ADT ........................................................844
20.8.2 The Class LinkedDirectedGraph ................................................846
20.8.3 The Class LinkedVertex.................................................................850
20.8.4 The Class LinkedEdge .....................................................................852
Discovering Diverse Content Through
Random Scribd Documents
“What I want is a second mate.”
(p. 18)
But I was already heated to the boiling point by the tidings I had
heard, and, blazing with indignation that he should dare ask me, a
loyal colonist, such a question, I blurted out:
“I’ll see His Majesty hung first! Do you think I’ll fight against my
native country?”
“A rebel! A rebel! Right here among us! Seize him, comrades! Don’t
let him escape! His Majesty knows what to do with such fellows.
Seize him!” and he sprang towards me.
“Here he is, sir!” cried a stout teamster by my side, and he reached
out his hand to hold me. But I eluded his grasp, and, turning, darted
back down the street, with the whole crowd at my heels.
CHAPTER VI
IN WHICH I HAVE MY FIRST TASTE OF A
BRITISH PRISON
Around the first corner and down to Watling Street I ran, taking the
nearest course to the river, though I had no intention of returning to
my ship. The startling news I had heard about the state of affairs in
the homeland had fired me with a patriotism before which all
thought of allegiance to the King vanished. I was inflamed with the
desire to cross the ocean at once and throw in my lot with my
struggling countrymen. For the present I would endeavor to escape
my pursuers; later I would find some way to return to my native
land.
I came to this decision in the brief time it took me to reach St. Paul’s
church. Turning there, I crossed Carter Lane and Queen Street, and
came out upon the Thames near St. Paul’s pier. Here a glance
behind me showed that I had distanced my pursuers. Noting this
fact with much satisfaction, I sped out upon the wharf and darted
through the open door of the nearest warehouse. No one appeared
to dispute my entrance or to check my advance, and swiftly I glided
between the barrels and boxes to the farthest side of the room. Here
I found another door. It was closed but unfastened, and I had time
to open it before the angry mob that was following me appeared.
Passing quickly into the next apartment, I shut the door and rolled a
huge cask of rum against it, effectually barring it. Confident now
that it would be some time before my pursuers discovered my
whereabouts, I proceeded leisurely through the semi-darkness of the
room to a place where great bales of cotton were piled nearly to the
ceiling. Among these I at length found an open space which allowed
me to crawl back of the outer tier, where I lay down and waited.
So still was the immense building I could plainly hear the pursuing
men enter the other apartment in search of me. The persistence
with which they kept at their task told of their eagerness to find me.
At length two of them tried the door of the room in which I was
hiding, and, on discovering it was barred, one of them exclaimed
confidently:
“He cannot have gone in there!”
“Where is he then?” asked the other doubtingly. “I certainly saw him
enter here. Where can he have gone?”
“Out of that open scuttle in the roof,” answered the first.
“But it is more than four and twenty feet to the ground,” objected
the second.
“What of that?” retorted his companion scornfully. “He’s a sailor and
can climb up or down these walls as easily as he would a mast. It’s
just as I told you, while we have been looking for him among these
boxes and barrels, he has made good his escape.”
The objector seemed to be convinced, for without another word he
followed his comrade down the room. The sound of their footsteps
grew fainter and fainter, and finally ceased altogether. Evidently the
search for me had been abandoned. Still I did not stir. I was safely
hidden, and would remain where I was until the hour for closing the
warehouse had come.
Opposite the opening through which I had crawled was an outside
door, one edge of which was warped enough to allow a few rays of
light to enter. I watched these, knowing that when they disappeared
it would be time for me to make a move.
How slowly the minutes passed! What a tumult of thoughts crowded
through my brain! The events since I had left the colony came
trooping in rapid succession. The life on board the frigate was lived
over again. With these bygone experiences came plans for the
future. I knew there were several vessels in the river hailing from
American ports. Once let their captains know of the battle at Bunker
Hill and they would hasten to sail for home. Doubtless on one of
these crafts I could find a berth. I resolved therefore, to visit them in
turn under the cover of the night until I had secured a place.
At length the light through the doorway grew so faint as to be
scarcely perceptible, and I crawled out of my hole, and went over to
the entrance. First gently, and then more vigorously I tried to open
the door. My efforts were useless, however, for it was fastened on
the outside. So I retraced my steps to the place where I had
entered, rolled the cask away from the door, and opened it. Stepping
through into the next room, I turned to close the entrance after me,
when a voice startled me.
“So you were there after all, my young bantam,” it exclaimed. “Tim
Waters thought so. You didn’t fool him, if you did the others; and it
has been worth while to wait for you too, for now the five pounds
offered for your capture is mine.”
A glance showed me that the speaker was a burly fellow, evidently
the porter of the warehouse. Doubtless he had known that the door
between the apartments of the building was not fastened, and
finding it secured, had quickly divined that I was within. So,
stimulated by the reward offered for my apprehension, he had
patiently awaited my coming.
Scarcely had I surmised this fact when he sprang forward to seize
me. But quick as he was, I was quicker, and, eluding his grasp,
dodged by him. So confident was he that he was going to grab me,
he had put his whole force into his forward spring, and now, missing
me, he also lost his balance, and plunged headlong against the door.
Startled by his call, I had but partly closed it, and, swinging back as
he came against it, he was precipitated into the other room. The
cask which I had used as a barricade was only a few feet away, and
striking upon this with his head and shoulders on one side and his
legs and feet upon the other, he set it to rolling. I could scarcely
refrain from a shout of laughter as I saw him struggling to regain his
feet, and by his very efforts sending his unwieldy steed farther and
farther down the room. Not until the barrel fetched up against the
pile of cotton bales did it stop, and even then it was a moment or
two before he could regain an upright position. I only waited long
enough to notice he was not seriously injured, and then shutting the
door, I fastened it on my side by passing a piece of a box cover
through the door handle.
In another minute I was in the open air, and finding the way clear, I
hastened through the fast falling darkness to the street. Keeping in
the shadows as much as possible, I went down the river bank to a
point nearly opposite the first American vessel. Here I undertook to
reach the end of an adjacent wharf, hoping there to find a boat in
which I could visit the brig. But I had hardly got a dozen feet down
the planking before a watchman confronted me, saying gruffly as he
tried to look me over in the darkness:
“Who are you? What do you want here? Are you that rebel
midshipman I have been told to look out for? Faith, I believe you
are!” and he seized me by the collar before I could do a thing to
prevent him.
Then began a struggle which lasted for some minutes. We were
about equally matched in strength, but he had me at a disadvantage
and I am quite sure would have at length mastered me but for an
accident. Coming to a place where the flooring suddenly raised itself
a few inches, he stumbled and fell full length upon his back. I was
pulled down upon him, but he lost his hold upon me, and before he
could recover it, I regained my feet and was away.
I had no trouble in eluding him, or those he aroused by his cries, but
as I made off under the friendly cover of the night I must confess
my heart was filled with apprehension. It was very evident that the
whole water front had been guarded against every attempt on my
part to board a ship from the colonies. The offer of a reward for my
arrest had, moreover, put all the watchmen on the alert. If I
escaped, therefore, two things were clear: I must change my naval
garb for one less conspicuous, and I must make off across the
country to some other port. Having come to this conclusion, I left
the river, and started towards the rear end of the town. After going a
mile or more the lights of a second-hand clothing shop attracted me.
Crossing the street, I glanced in at the window. The store was
evidently kept by an old Jew who was alone, and I ventured in.
“Have you a second-hand rig you would exchange for this I have
on?” I briefly asked.
He glanced curiously at me, and then with a shrewd look in his eyes
remarked:
“Running away from your ship, are you?”
“Yes,” I admitted promptly, “but I am willing to give you a good
bargain, so what does that matter to you?”
He shook his head. “It might get me into trouble with the naval
authorities,” he replied. “It’s risky business.”
“I don’t see how,” I retorted. “You can tell them I claimed to have
surrendered my commission, or anything else you choose. They
cannot blame you for making a good trade when you had the
chance.”
He came slowly around his counter and looked my uniform carefully
over. It was nearly new, and in excellent condition, and as he noted
these facts the look in his eyes changed to one of greed.
“It’s risky, risky,” he replied, “and I can’t allow you much for the
garments. But here is something I will give you for it,” and he led
the way to the other side of the shop. From a shelf he took what
had evidently been the suit of a farmer lad. It was of coarse material
and well worn, yet neat and clean.
“I took this a week ago from a youngster who ran away to sea,” he
explained; “now I’ll exchange with you to help you run away from
the sea,” and he laughed at his attempt at facetiousness.
The clothes were not worth half those I was wearing, but I did not
hesitate.
“I will do it,” I said. “Can I go into your back room and make the
change?”
He assented, and led me into the rear room, leaving me alone, as
another customer came in just then. I took the opportunity, while
changing my clothing, to look over the state of my finances, finding I
had five shillings and a sixpence. There was little likelihood of my
earning any more and this sum, therefore, must last me until I could
find a ship for home. So it was clear that whatever port I decided to
go to, I must walk, in order to husband my little store.
My first anxiety, however, was to put the city behind me, and with
this end in view, upon leaving the shop I struck off uptown at a brisk
rate. An hour later, in the outskirts, I stopped at an inn long enough
to get supper, and then resumed my tramp. All night long I kept it
up, but as dawn came on, finding myself near a small village, which
I afterwards learned to be Watford, I entered and made my way to
its one tavern. There I secured a room, to which I at once retired for
a much needed rest. Some hours later I was awakened by the inn-
keeper, whom I found sitting down on the side of my bed gazing
long and fixedly at me.
“There is a squad of soldiers down stairs who are looking for a lad
about your size and build, I should judge.”
Though greatly alarmed by the tidings, there was something in the
man’s gaze which reassured me, and I waited for him to go on:
“They say he is a young rebel from the colonies, who has cursed the
King.”
Still I was silent.
“Tell me all about it,” he continued. “You have a friend in me.”
The man’s dealing with me proved that, so I frankly told my story.
“If that is all, I will protect you,” he declared. “I have a brother over
there, and my sympathies are with the colonies. I hope they will
win,” and he abruptly left the room.
Listening at the door, I heard him descend the stairs, and say to the
officer in charge of the troopers:
“I have no one here dressed as you say that young rebel was; but
I’ll tell you what I’ll do. If he comes this way, I’ll take care of him,” a
promise he literally kept. For he not only boarded me a week, long
enough, as he believed, to end all search for me, but on my
departure put money enough into my hands to pay my fare by stage
to Liverpool, where he advised me to go.
“You may regard all I have done for you as a loan,” were his parting
words, “and repay me when I come to America,” a thing I am glad
to say I was able to do.
I left Watford with little fear of detection, and enjoyed to the full my
ride across the country to St. Helen’s, a small town a few miles out
of Liverpool. The stage reached there just at dark, and, as I had
done a half dozen times before, I descended from its top and
entered the tavern to order supper and a room for the night. Two
feet over the threshold a hand was laid upon my shoulder and a
voice I instantly recognized said:
“Master Dunn, you are my prisoner.” It was the recruiting sergeant
whom I had heard in Cheapside, London, telling of the war with the
colonies.
Resistance was useless, for behind the officer stood four soldiers
with their muskets ready for instant use, so I submitted to my arrest
with the best grace I could muster.
In a few minutes they mounted me upon a horse, surrounded me
with a squad of troopers, and hurried me off towards Liverpool.
Reaching the city, they hastened along its narrow streets to a huge
stone building on the river’s edge before which they stopped.
Tumbling me unceremoniously from the beast I rode, they led me
through the heavy portals, and along the dark corridors to a room in
the rear, into which they thrust me and swung to its stout door with
a clang that still echoes in my ears. I did not need to be told where I
was—the structure, its form, its appearance, its interior
arrangements, spoke louder than human words. It said in a silent
but unmistakable language, “You are in a British prison.”
CHAPTER VII
I MEET A NEW FRIEND
I will not attempt to describe my feelings as I stood there in the
darkness, with those stout walls shutting me from the liberty I
craved—craved that I might give myself to the service of my
struggling country. I am quite sure I could not at that time separate
the mingled feelings of chagrin, regret, and hopelessness that
oppressed me. One word alone can express the condition of my
mind and heart just then—despair. I could have shed tears if it
would have been of any use; it may be a sob did escape me, but if
so it was speedily checked, for a heavy voice spoke from the farthest
corner of the room, demanding:
“Who are you?”
I had supposed I was alone in the cell. Surprised now to find I was
sharing it with another, I nevertheless was able to answer promptly:
“An unfortunate prisoner like yourself.”
“Yes,” was the response, “but I mean are you British or American?”
and I thought I detected an eagerness, almost a suspense in the
speaker’s voice as he waited for an answer.
“American,” I replied.
“Thank God for that!” was the immediate ejaculation. Then I heard a
sound as though one was rising from a bed, and the next instant the
man came over where I was with rapid strides.
“I am Samuel Tucker, a sea captain from Marblehead, in the colony
of Massachusetts,” he announced much to my astonishment. “Now
tell me who are you?”
“Captain Tucker!” I cried, ignoring his question in my surprise and
joy. “Captain Samuel Tucker of Marblehead, and I came across the
ocean to find you! Who would have thought we should meet here—
in a cell of the Liverpool jail!”
“‘Came across the ocean to find me,’” he repeated, and I could not
help noticing the wonderment in his tones. “Pray tell me then who
are you?”
“I am Arthur Dunn, the son of Captain Dunn, with whom you once
sailed,” I replied, and then rapidly, yet in fullest detail, I told my
story, beginning with my mother’s dying request, and ending with
my arrest an hour or two before.
“So you are in jail for the very same reason I am,” he remarked with
a slight laugh when I was done. Then he spun his own yarn.
He had made a quick voyage to Lisbon, discharged his cargo, taken
another, and returned to the colony. Then he had sailed for
Marseilles, France. There an agent of the English government had
come to him, desiring to charter his brig to go over to Gibraltar and
convey a company of soldiers to Liverpool.
“The rascal never told me why the regiment was hurrying home,” the
Captain explained, “or I would not have let him have my craft at any
price. But the pay was good, and I fell into the same trap that a half
dozen other Yankee skippers did. We went to Gibraltar and brought
in here a regiment which sailed the very next day after its arrival for
the colonies to fight our countrymen. When I learned the truth my
blood boiled within me, and I cursed the King and his government to
the agent’s face when he came to pay me the charter money. I
might have known what would follow; the confiscation of my vessel
and my incarceration here. But the fellow knew just what I thought
of him and the government,” he concluded with a chuckle.
“How long have you been in here?” I now asked.
“Two weeks,” he responded; “but come over here,” he added in a
low whisper, and, taking me by the arm, he led me down the cell to
its one narrow window, through which so little light came that I had
not noticed it until then.
Once at the aperture he raised my hands with his own to the
window frame, and then I became aware that he was removing it.
Placing it noiselessly on the floor, he carried my hand up to the iron
grating, which I soon found he was taking down bar by bar.
“There,” he said at length in a tone so low I could scarcely hear him,
“you see the way is clear for our escape. This is why I was so
anxious to find out if you were an American. I wanted no one here
to thwart my plans. We shall go at midnight, so you will not be long
within these walls, Master Dunn,” and again he chuckled.
“But is not the river outside this wall?” I questioned, “and some feet
below? How are we to get down to it?”
“With this,” he replied, and carrying my hand up to his bosom I felt
hidden inside of his shirt a coil of stout rope. “Oh! the preparations
are all made, and cannot fail,” he continued confidently. “A boat from
a Yankee ship in the river will come under this window at midnight,
and lowering ourselves into that, we shall be taken off to the vessel
which will sail before daylight. The only change in the plan is she will
carry two passengers instead of one. You see, you came just in time
to go home with me, Master Dunn.”
The confidence in his own tones inspired me. A half-hour before I
had been on the verge of despair; now I was nearly wild in my
exuberance of joy. I could scarcely wait for the hour to come when
we should leave our cell. Then I fell to wondering how Captain
Tucker had been able to arrange so completely his plan of escape
and as though he divined my thought, the captain told me, while we
waited the coming of the rescuing yawl.
“Anchored in the harbor, near my own brig, at the time of my arrest,”
he began, “was the ship Rebecca Morris from Philadelphia. Her
captain is an old friend of mine, and I knew if I could manage to
communicate with him he would do all in his power to help me
escape. Under the pretext of sending a message to my family in the
colonies I asked the turnkey who came to my cell daily to take a
letter off to Captain Allen. At first the fellow, demurred, but when I
offered him my watch, a valuable one, in return for the favor, and let
him see the note I had written, he yielded. Unbeknown to him,
however, I substituted a second note for the first one, in which I
described the situation of my cell, and suggested a way in which my
friend could aid me. That night a boat came under my window,
bringing the things I had asked for—a file and a stout rope.
Meanwhile I removed the sash with my pocket knife, and unraveled
one of my stockings to obtain the string I needed. With the latter I
pulled up a stouter cord, and then the file and the rope from the
yawl. I knew it would take me several days to cut through the bars,
and so sent down a note requesting the boat to return here for me
tonight. When the cord came back, there was a line from Captain
Allen himself assuring me he would be here without fail.”
As the moments passed I could not help growing anxious lest for
some reason the friendly captain should fail us. On the other hand,
Captain Tucker was as cool and undisturbed as it was possible for a
man to be.
“I know Christopher Allen,” he declared again and again, “and he will
be here as sure as the sun will rise tomorrow morning. All the
guards on the river cannot stop him. He’ll find a way to outwit them
and rescue us.”
At length there came the yowl of a cat from somewhere on the river.
Captain Tucker leaped to his feet (for we had been sitting on the
edge of the bed while we waited), exclaiming in a suppressed
whisper:
“What did I tell you, Master Dunn? He is almost here.”
Again the yowl sounded nearer, and as though the animal was
floating on the tide down by the jail. Then it came the third time
directly under our window-sill.
My companion had already drawn the coil of rope from his bosom,
and was making one end fast to a piece of the iron grating. Placing
this across the narrow aperture, he threw out the cord, and turned
to me, saying:
“All is ready, Master Dunn, and you shall go first. Tell Captain Allen,
however, that I’m right behind you.”
“Nay, sir,” I remonstrated, “you should go first, as the plan is yours
and—” but I did not finish.
“I am in command here,” he interrupted with an authority I could
not dispute, “and you are to go now.”
With the words he lifted me in his brawny arms and thrust me feet
foremost through the opening. I caught the rope in my hands and in
another moment was gliding swiftly down it. The distance was not
so great as I had expected, and it seemed but an instant before
stout hands seized me and set me gently down in the boat.
“You are not Captain Tucker,” a voice then whispered in my ear.
“No,” I admitted in the same low tone, “I am Arthur Dunn, a fellow
prisoner whom Captain Tucker insisted should come down first. He is
right behind me.”
The swaying of the rope confirmed my words, and, pushing me to
one side, the same brawny arms caught the captain as he came
down. The next minute the light craft darted off down the river as
swiftly as four oars could pull it.
I had been given a place in the bow, while Captain Tucker sat with
Captain Allen in the stern of the boat. What passed between them
on our way to the ship I never knew, but when we were in her cabin
her skipper turned to me, and putting his hand in mine, said:
“I have had a double pleasure tonight, Master Dunn. Instead of
plucking one patriot out of the hands of the enemy, I have taken
two. I knew your father. I believe he has in you a son worthy of him.
You are welcome to a place among us.”
I thanked him as best I could, and went to the berth assigned me
with a heart full of gratitude to the overruling Providence that had so
wonderfully cared for me and given me such kind friends.
But I was destined to find a more powerful friend before that voyage
was finished. This was Master Robert Morris, the owner of the
vessel, and a member of the Continental Congress, who was on
board as a passenger. Either Captain Tucker or Captain Allen must
have told him who I was and how I came to be on board the ship,
for when I went on deck the next morning, he came up to me, and
having congratulated me on my escape from the British prison, said
with a smile:
“We ought to hear good things from you and Captain Tucker. You
both have early shown that you are loyal to the colonies.”
Having no desire to be a drone on the vessel, I went to the captain
after breakfast and offered my services in any place he could use
me.
“It is commendable in you, Master Dunn, to make this offer,” he
replied, “and I will say I rather expected it of you. It is your father all
over again. But there is no place I can put you except with the
sailors. Captain Tucker has already made a similar offer, and I can
hardly put the best skipper that ever sailed out of a New England
port out of the cabin, so I have created the berth of a third mate in
order to use him. You can hardly expect me to arrange for a fourth
mate much as I should like to keep you with us. But if you care to
go to the forecastle, I will enter you on the ship’s roster as an able
seaman.”
“I had rather go there than be idle,” I answered promptly, and,
having been assigned my watch and station, I went forward. Though
I did not know it then, my act greatly pleased Master Morris, who
was already furtively watching both Captain Tucker and myself for
proofs of our seamanship.
Before the voyage was over he had the opportunity to see us in
positions which tested to the full our qualities as sailors, for ere a
week had passed we encountered one of the severest gales I ever
experienced. For three days it raged, carrying away our shrouds and
yards as if they were tow, racking the ship until her seams opened,
and she was in great jeopardy. What was even worse, the Captain
was stricken down by a falling spar, and both of his limbs were
broken.
In this emergency Captain Tucker stepped forward and offered to
take charge of the vessel. To this Master Morris and the mates
readily assented. The new commander’s first act was to assign me to
his own position as third mate, and his second to assure the men he
would save the vessel, if they would only promptly obey his orders.
Then he went to the wheel, and, taking the helm into his own
hands, guided the ship all that night through the storm.
His orders, issued from the wheel, were promptly executed by the
sailors who had taken on new courage and when dawn came and
the tempest abated, there was not a single soul on board but what
was ready to own that it was to his skill and knowledge the ship and
the crew owed their preservation.
This deed was the link in the chain that secured the fast friendship
of Master Morris. Grateful for the saving of his vessel, upon our
arrival in port, he introduced Captain Tucker to some of the other
members of the Continental Congress, and before he left
Philadelphia for home he had the promise of a captain’s commission
in the Continental Navy.
Nor was I forgotten. Master Morris spoke a kind word for me, and I
was assured that I should have a midshipman’s commission in return
for the one I had lost.
CHAPTER VIII
OUR FIRST PRIZE
Our arrival in Marblehead created quite a sensation. Tidings of
Captain Tucker’s imprisonment and the confiscation of his vessel had
already reached there, while his escape was unknown. His sudden
appearance in the streets of the village therefore brought him almost
an ovation, and men, women, and even the children crowded
around him, hoping to hear how he had escaped the enemy’s hands.
A few of the townspeople remembered my visit to the place several
months before in search of the Captain, and when it became known
that I had also been thrown into jail for defying the King, I came in
for my own share of public attention; so for some days we both
knew what it was to be famous.
Then the excitement subsided, and we were permitted to move
among the people in a commonplace sort of way, and unmolested to
attend to the daily tasks that fell to us. At the Captain’s request I
made my home with him while we awaited our commissions, and,
save for a brief visit to my native village early in January, I did not
leave him.
It was January 20, (1776) when I returned from my visit. As I had
only a small bundle with me, I left the stage at the door of the
tavern, and started to walk to the house I now called my home. I
had gone but a few yards when I heard the clatter of a horse’s hoofs
behind me, and, turning, I saw a man in martial costume and
adorned with the trappings of rank riding in hot haste towards me.
His coming had already aroused much curiosity on the part of the
villagers, for they were gathering at the windows and on the streets
and wharves, to gaze at the trooper as he galloped along. He
dashed by me, and took the nearest road to Captain Tucker’s
residence on Rowland Hill. Suspecting he was a messenger from the
Congressional Committee, I quickened my steps and came in sight of
the house in time to witness an amusing scene.
Captain Tucker was out in the yard chopping wood. The gaily decked
officer rode up and dismounted. Seeing a person dressed in ordinary
garb—a tarpaulin hat slouching over his face, a pea jacket and red
waistcoat covering his body, brown breeches on his legs, and a
flaming bandanna waving about his neck—he, naturally perhaps,
thought he must have come to the wrong place, and so called out
roughly:
“I say, fellow, I wish you would tell me if the Honorable Samuel
Tucker lives hereabouts!”
“Honorable? Honorable?” questioned the Captain with a shrewd look
at the stranger. “There is not any man of that name in Marblehead.
He must be one of the family of Tuckers in Salem. I am the only
Samuel Tucker here.”
The trooper took his packet from his pocket, looked at it again and
again. “Lives in a house, two stories, gable-end, standing by itself on
a hill, not far from the bay shore, a piece of woods near it,” he read
out slowly. “Surely this must be the place,” he commented, looking
sharply around him; and then eying the chopper from head to foot,
he continued:
“Captain Glover at Cambridge told me that he knew Master Tucker
well, and that he lived in Marblehead, and described his house for
me—‘gable-end, on the sea-side, none near it.’ Faith, this looks like
the very place.”
The parley, however, soon came to an end, for the messenger was
not slow to notice the gallant look and noble appearance of the man
before him, and knew he could not be mistaken.
“You yourself must be Captain Tucker,” he declared, handing the
packet to the man before him, “and here is your commission as a
Captain in the Continental Navy. I also have a midshipman’s
commission for Arthur Dunn. Can you tell me where I may find
him?”
“He’s right behind you, I reckon,” remarked the captain, taking his
packet and waiting for me to take mine. Then he invited the stranger
to come into the house and take a rest and refreshments before he
returned to Cambridge—an invitation which was gratefully accepted.
With the commissions were our assignments to the frigate Franklin,
carrying sixteen guns, and stationed at Beverly. The Captain was
also directed to get her in readiness at once for a cruise.
Busy days followed. The stores were shipped, the ammunition was
placed on board, and in a week we would have been ready for sea
had our outfit of small arms arrived. After three days delay Captain
Tucker grew impatient, and, going over to Salem, he purchased the
weapons with his own money and had them sent on board. As he
saw them arranged in their racks, he turned to me, who had been
directing the work, and remarked with satisfaction:
“There! we’ll get off tomorrow morning, Master Dunn.”
He went over to Marblehead that evening and when he returned he
carried a huge bundle in his arms. To his executive officer,
Lieutenant Fettyplace, he explained:
“It’s a banner my wife has been making for us. Tomorrow, before we
sail, we’ll break it out from the masthead.”
Naturally curious to see it, Master Fettyplace, Lieutenant Salter, the
second officer, and myself, who stood near, waited for him to show
the flag to us. But he did not do it. It was not until my own hands
pulled the cord the next morning which unfurled the banner from
the frigate’s peak that we saw the beautiful piece which Mistress
Tucker had wrought.
There it floated on the gentle breeze: a white field, a green union,
made in the form of a pine tree, with the motto beneath it: “An
appeal to heaven.” And under that flag we fought until Congress had
adopted the stars and stripes.
Ten minutes later with every foot of the ship’s canvas stretched to
the north-west wind, we were standing out to sea. Once out of the
harbor, our bow was turned towards Cape Cod, and a man was sent
to the cross-trees to be on the lookout for prizes. We found not one,
but two, much sooner than we expected. The circumstances as near
as I can recall were these:
Just before dark, hearing a loud cannonading on our left, and
apparently some distance away, we directed our course thither.
Before the night fairly shut down, we came near enough to see four
vessels engaged in conflict. Two of these, a ship and a brig, were
flying the British flag, while the other two were schooners, and
clearly American privateers.
Not wishing to take the English vessels from those who had first
discovered them, and who had the first right to them as prizes if
they could capture them, we refrained from entering into the
engagement. But when the enemy beat off our friends, and sailed
away towards Boston, we immediately gave chase. The Britishers
and our own ship were evidently faster sailers than the privateers,
and soon we had left them behind. At nine o’clock they were out of
sight, and the chase was all our own. Then the wind nearly failed,
and for an hour or two we drifted along a mile behind our prey.
About this time our lookout reported another sail some distance
away off our starboard. It was too dark to make her out, and
Captain Tucker immediately ordered out a boat, and putting me in
charge, directed me to go over and reconnoitre the strange vessel.
“Take a dark lantern with you, Master Dunn,” he said, “and, if she
prove to be an English cruiser, suspend your light near the edge of
the water as a signal of your immediate return. We’ll be on the
lookout for you. If, however, she be an American vessel, then elevate
your light in the air, and we’ll come down, pick you up, and speak
with her.”
“Aye, aye, sir!” I responded, and descending to the yawl, I gave the
order to pull away.
When near enough for the sound of our oars to be detected, we
muffled them, and cautiously advanced, seeking to get under the
stern of the craft without being discovered. At length we were near
enough to discern through the darkness that she was a trim
schooner carrying ten guns, and that her course showed she was
trying to overhaul the British vessels. This fact made me a little
suspicious of her character, for it seemed to me a daring attempt for
her to be following up two vessels, each larger than herself, with an
idea of attacking them. I called the attention of our boatswain,
Joseph Lewis, to this circumstance, asking in a low tone:
“Does it not seem to you, Master Lewis, that she must be a Britisher,
a consort of the ship and brig, and is endeavoring to overhaul
them?”
“Two things are agin that, sir,” he replied in the same cautious way.
“Fust, thar’s her build. She came from Yankee stocks as sure as my
name is Joe Lewis. Then thar’s the flag she carries. I can’t jest make
it out, sir, but it ain’t the English colors. As for her followin’ two
vessels larger than herself, that doesn’t signify. We’ve got Capt’ns
who’d do it with half her guns, hopin’ to find a way to cut out one or
t’other of them. I’m sure, sir, we’ll find she’s a friend. I’m ready to
risk runnin’ right up to her.”
“I hardly think we’ll do that,” I answered. “But we’ll work up under
her stern, and make sure who she is.”
“I beg yer pardon, sir,” he continued, “but if ye’ll order all the men to
lie low in the boat, an’ git down yerself, an’ let me have an oar, I’ll
scull her up under the schooner in no time without their ’specting
we’re thar.”
I gave the necessary command, and, stooping as low as he possibly
could and yet work an oar, he sent our yawl noiselessly forward in
the wake of the stranger. Five minutes later he touched my arm. I
looked up and he made a quick upward gesture. I understood, and
opened my dark lantern enough to send a single ray of light on the
stern of the schooner which was now just above our heads. With a
thrill of delight I read her name—Katy—and her port—Providence in
the Rhode Island Plantations.
Dropping behind her far enough to make it safe to signal our frigate,
we raised our light high in the air with an oar, and waited. It was not
long, light as the breeze was, before the ship was alongside of us,
and, picking us up, she went on after the schooner. In ten minutes
we spoke with her, and her captain, Abraham Whipple, came on
board.
Captain Tucker and he speedily came to an agreement to join forces
and attack the English vessels, and since Captain Tucker held his
commission from the Continental Congress, it was arranged that
both of the Yankee crafts should be under his command. These
preliminaries completed, we hastened on as fast as the light wind
would permit us after the enemy. It was clear by their course that
they were striving to make Boston harbor; and equally clear after an
hour or two of watching that all four vessels were about equally
matched in speed.
As we drew near Long Island clouds overcast the dim stars, shutting
out what light we had, and we were uncertain whether the ships we
were pursuing took the east or west channel. So Captain Tucker
signalled the Katy to go up the east side, while he went up the west
side of the island. It was our good fortune to speedily overtake the
transports which, running too close to the flats, got aground.
Clearing away our guns, we ran abreast the Britishers and poured in
a broadside. They both responded from their starboard batteries, the
only ones they could use. Our shots were low and did great havoc
among the troops and crew of the enemy. On the other hand, their
guns were aimed too high and the balls passed above our heads,
riddling our sails and doing much damage to our spars and rigging;
then they swept on, endangering the Rhode Island schooner, which
lay on the other side of the island, becalmed, in range of the English
cannon, and unable to change her position or to render us any
assistance.
For a half-hour the battle raged until the commander of the British
ship was slain, when she struck her colors, and her consort quickly
followed. I was with Lieutenant Fettyplace when he boarded the
larger craft, finding her to be the George, from Glasgow, Scotland.
Lieutenant Salter went off to the brig, and reported her to be the
Annabella from the same Scottish port. Each vessel carried a large
cargo of ammunition, clothing and stores for the British army, while
on the latter were two hundred and fifty Highland soldiers, under
command of Colonel Archibald Campbell, and belonging to General
Frazer’s corps.
In the conflict we had not lost a man, nor had any of the ship’s
company been seriously wounded. In short, our only damage was to
our sails and rigging. But the enemy had suffered greatly. More than
two score men were more or less injured, while thirty-six, including
the captain of the George, lay dead.
One of the saddest sights I remember in all my naval experience
was that of the next day, when we buried the dead Scotchmen on
the island. It was heartrending to see the women, who had
accompanied the troops, weeping with loud lamentations, and to
hear the bag-pipes play the funeral dirge.
The hours before the sad burial had been spent in floating the
prizes, and transferring our prisoners to the Franklin. A stiff breeze
from the south-west had enabled the Katy to rejoin us. So the
funeral once over, prize crews were placed on the captured vessels,
and we sailed for Lynn.
There we turned them over to the Continental agent, and had the
satisfaction of knowing that their cargoes—save one item—went to
Washington’s army at Cambridge. The exception was the store of
canvas we found among the prize goods. This was used to furnish us
with a new set of sails, and we were soon off to sea again.
CHAPTER IX
AN ASTONISHING OFFER
Our first voyage had been of three days duration, and we had
captured two prizes of considerable value. Standing near Lieutenant
Salter, as we left the harbor for our second cruise, I remarked:
“A short and successful trip was ours before, Lieutenant Salter. What
think you our fortune will be this time, sir?”
He smiled. “Who can tell, Master Dunn? This search for prizes is
much like a lottery. We may quickly make a fine draw, as we did
before, or we may get a blank. It’s even possible that we may cruise
around for weeks, yes, this entire voyage, and yet return to port
empty-handed.”
“It’s not Captain Tucker’s luck to do that,” I protested stoutly. “He’s
always made successful voyages, and I believe this will be no
exception. We shall not go many days before we capture a craft.
Remember what I tell you.”
“I presume you would like to see them coming so fast the Captain
would find it hard work to make up prize crews for them,” he
responded banteringly. Then he continued more earnestly: “I agree
with you. I believe Captain Tucker sails under a lucky star, and have
no idea our trip is to be a fruitless one.”
He proved a true prophet. The very next day we took a prize—a
sloop from Halifax, laden with dried fish, and bound for Boston.
Though not of great value, it was in no sense to be despised, and to
my own mind it was not unimportant. It meant so much less food for
the enemy, and so much more for our brave troops. With this
thought predominant I raised a cheer as Boatswain Lewis, with a
crew of four men, started for port in charge of her—a cheer which
our entire ship’s company joined in heartily.
Then the prizes followed in rapid succession until we had nine to our
credit, and our number had been reduced three score to furnish
crews for them. Among these were several of great value.
It was just two weeks since we left port when we captured our tenth
vessel, the brig White Swan, from the Western Islands for Boston,
with a cargo of fruit and wine. I speak more particularly of this, for
my own personal fortunes were more intimately connected with it.
We sighted the craft at early dawn, and immediately gave chase—
and it was a long one. Heavy laden as she was, and with less than
two-thirds of our spread of canvas, yet she was well nigh a match
for us in sailing. We overtook her by inches. The sun was about to
set when we sent a shot across her bow, and it had gone down
when I returned from boarding her and reported her sailing port, her
destination, her cargo, and the additional facts that she had a crew
of twelve, and carried one passenger—a Master George Rollins.
Captain Tucker kept her by the frigate all night. At daylight he
transferred to her a score of prisoners that we had taken from other
vessels, and, placing me in command with a crew of ten, he directed
me to take her into Lynn. Master Rollins at his own request remained
upon her, and with her captain shared the cabin with myself and
Midshipman Thomas Blinn, who was my executive officer.
We cleared from the Franklin about nine o’clock and started on our
voyage of two hundred miles with high hopes. The brig was a
staunch one, and, as I have already intimated, a fast sailer. Her
cargo was valuable, and I felt what perhaps may be termed a
natural elation to be given the task of taking her into port. It is true I
had a large number of prisoners between the decks, and two men
under parole in the cabin, but I anticipated no trouble from them,
and felt myself competent to fulfill the task assigned me faithfully.
The day was clear and cold. The wind was a favorable one, and we
went ahead at a pace which would have delighted any sailor’s heart.
In ten hours I estimated that we had covered nearly one-half the
distance to port. Let the breeze only keep up and we should reach
our haven before noon on the morrow. With much satisfaction I
turned the watch over to Midshipman Blinn and went below.
Inadvertently the room I occupied was between that of Captain
Williamson and Master Rollins, while Midshipman Blinn had one on
the other side of the cabin. I had fallen asleep, but was awakened
by someone trying to open my door. Fortunately, as it proved, I had
fastened it on entering, and discovering this fact the intruder passed
on and gave a light tap at the door of Master Rollins. The next
instant, as though the newcomer was expected, the door opened
gently, and the man quietly entered.
I suddenly remembered that when I stored my kit under my berth I
had noticed a crack in the partition between the staterooms, and
stepping noiselessly out upon the floor I stooped down, pushed my
bag to one side, and crawled in beside it. Placing my ear to the crack
I was able to distinguish much that passed between Master Rollins
and his visitor, though they conversed in whispers.
He was Captain Williamson, as I had suspected, and had evidently
announced his failure to enter my room, for his companion was
saying:
“It is too bad, Captain; so the first part of your plan fails.”
“Yes, and now I’ll try the second,” was the response.
“You’ll need to proceed with great caution, for if discovered they’ll be
likely to put you down with the other prisoners,” Master Rollins now
warned him.
“Yes,” the captain admitted, “but it’s worth the risk. If I can only
crawl along to the hatch and open it, the vessel is ours again. What
we lack in weapons we shall make up in numbers, and can easily
overpower the half dozen men on deck. Then Master Dunn down
here and the men in the forecastle can be taken care of at our
leisure.”
“I shall stand by my agreement if you succeed, Captain Williamson,
and add a hundred pounds to the one I am to give you when you
land me on the cape.”
“I have no fear about that, Master Rollins. You have done always
just as you promised, but I think this time I shall have earned it,” the
officer commented, and turned to go.
I waited until I had heard him pass my door, and ascend the ladder
to the deck; then I slipped out of my room and hurried after him.
But the moment my head was above the hatchway I knew that his
second plan had failed, for Midshipman Blinn had been on the alert,
and detected his presence on deck.
“What does this mean, Captain Williamson?” he was demanding.
“You know as well as I do that you were not to come on deck during
the night hours. I must insist that you return to the cabin
immediately, or I shall have to put you in the brig with your men.”
For answer the captain did the most foolish thing he could have
done under the circumstances. He sprang upon the speaker,
doubtless with the intention of throwing him down, and passing over
him to the middle hatch. Perhaps he thought that with a quick rush
he could reach it and let out the prisoners before the watch could
prevent him.
But he had underestimated the quickness and strength of Master
Blinn. Leaping to one side, the Midshipman avoided his antagonist,
and himself became the attacker instead of the attacked. Grappling
the Englishman by the waist, he called loudly for his men to come to
his aid. I sprang forward to assist him, but two of the watch were
before me, and when I reached his side, the burly Captain was
subdued. Two minutes later he was in the hold with the other
prisoners.
I quickly acquainted my executive officer with what I had seen and
heard in the cabin, and so explained how I came to be on the deck
so promptly. Then I said warmly—forgetting all titles in my
appreciation of Master Blinn’s good work:
“But I hardly was needed here, Tom. You were too alert for the
Captain. No one could have done better. I shall make a full report of
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookfinal.com

You might also like