100% found this document useful (3 votes)
26 views

Practical Programming 3rd Edition Paul Gries instant download

The document discusses the third edition of 'Practical Programming' by Paul Gries, which teaches introductory computer science using Python. It emphasizes the importance of programming skills across various fields and provides resources for learning and troubleshooting. The book includes acknowledgments, a preface, and introductory chapters on programming concepts and the Python language.

Uploaded by

paicekniaz0f
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
100% found this document useful (3 votes)
26 views

Practical Programming 3rd Edition Paul Gries instant download

The document discusses the third edition of 'Practical Programming' by Paul Gries, which teaches introductory computer science using Python. It emphasizes the importance of programming skills across various fields and provides resources for learning and troubleshooting. The book includes acknowledgments, a preface, and introductory chapters on programming concepts and the Python language.

Uploaded by

paicekniaz0f
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/ 53

Practical Programming 3rd Edition Paul Gries pdf

download

https://textbookfull.com/product/practical-programming-3rd-
edition-paul-gries/

Download more ebook from https://textbookfull.com


Acknowledgments
This book would be confusing and riddled with errors if it
weren’t for a bunch of awesome people who patiently and
carefully read our drafts.

We had a great team of people provide technical reviews for this


edition and previous editions: in no particular order, Frank
Ruiz, Stefan Turalski, Stephen Wolff, Peter W.A. Wood, Steve
Wolfman, Adam Foster, Owen Nelson, Arturo Martínez
Peguero, C. Keith Ray, Michael Szamosi, David Gries, Peter
Beens, Edward Branley, Paul Holbrook, Kristie Jolliffe, Mike
Riley, Sean Stickle, Tim Ottinger, Bill Dudney, Dan Zingaro, and
Justin Stanley. We also appreciate all the people who reported
errata: your feedback was invaluable.

Greg Wilson started us on this journey when he proposed that


we write a textbook, and he was our guide and mentor as we
worked together to create the first edition of this book.

Finally, we would like to thank our editor Tammy Coron, who


set up a workflow that made the tight timeline possible. Tammy,
your gentle nudges kept us on track (squirrel!) and helped us
complete this third edition in record time.

Copyright © 2018, The Pragmatic Bookshelf.


Preface
This book uses the Python programming language to teach
introductory computer science topics and a handful of useful
applications. You’ll certainly learn a fair amount of Python as
you work through this book, but along the way you’ll also learn
about issues that every programmer needs to know: ways to
approach a problem and break it down into parts, how and why
to document your code, how to test your code to help ensure
your program does what you want it to, and more.

We chose Python for several reasons:

It is free and well documented. In fact, Python is one of the largest


and best-organized open source projects going.

It runs everywhere. The reference implementation, written in C, is


used on everything from cell phones to supercomputers, and it’s
supported by professional-quality installers for Windows, macOS,
and Linux.

It has a clean syntax. Yes, every language makes this claim, but
during the several years that we have been using it at the University
of Toronto, we have found that students make noticeably fewer
“punctuation” mistakes with Python than with C-like languages.

It is relevant. Thousands of companies use it every day: it is one of


the languages used at Google, Industrial Light & Magic uses it
extensively, and large portions of the game EVE Online are written
in Python. It is also widely used by academic research groups.

It is well supported by tools. Legacy editors like vi and Emacs all


have Python editing modes, and several professional-quality IDEs
are available. (We use IDLE, the free development environment that
comes with a standard Python installation.)
Online Resources
All the source code, errata, discussion forums, installation
instructions, and exercise solutions are available at
http://pragprog.com/book/gwpy3/practical-programming.

Footnotes

[1]

See http://www.ccs.neu.edu/home/matthias/HtDP2e/index.html.

[2]

See http://learnyousomeerlang.com.

[3]

See http://learnyouahaskell.com.

Copyright © 2018, The Pragmatic Bookshelf.


Chapter 1
What’s Programming?

(Photo credit: NASA/Goddard Space Flight Center Scientific


Visualization Studio)

Take a look at the pictures above. The first one shows forest
cover in the Amazon basin in 1975. The second one shows the
same area twenty-six years later. Anyone can see that much of
the rainforest has been destroyed, but how much is “much”?

Now look at this:

(Photo credit: CDC)


Are these blood cells healthy? Do any of them show signs of
leukemia? It would take an expert doctor a few minutes to tell.
Multiply those minutes by the number of people who need to be
screened. There simply aren’t enough human doctors in the
world to check everyone.

This is where computers come in. Computer programs can


measure the differences between two pictures and count the
number of oddly shaped platelets in a blood sample. Geneticists
use programs to analyze gene sequences; statisticians, to
analyze the spread of diseases; geologists, to predict the effects
of earthquakes; economists, to analyze fluctuations in the stock
market; and climatologists, to study global warming. More and
more scientists are writing programs to help them do their
work. In turn, those programs are making entirely new kinds of
science possible.

Of course, computers are good for a lot more than just science.
We used computers to write this book. Your smartphone is a
pretty powerful computer; you’ve probably used one today to
chat with friends, check your lecture notes, or look for a
restaurant that serves pizza and Chinese food. Every day,
someone figures out how to make a computer do something that
has never been done before. Together, those “somethings” are
changing the world.

This book will teach you how to make computers do what you
want them to do. You may be planning to be a doctor, a linguist,
or a physicist rather than a full-time programmer, but whatever
you do, being able to program is as important as being able to
write a letter or do basic arithmetic.
We begin in this chapter by explaining what programs and
programming are. We then define a few terms and present some
useful bits of information for course instructors.
Programs and Programming
A program is a set of instructions. When you write down
directions to your house for a friend, you are writing a program.
Your friend “executes” that program by following each
instruction in turn.

Every program is written in terms of a few basic operations that


its reader already understands. For example, the set of
operations that your friend can understand might include the
following: “Turn left at Darwin Street,” “Go forward three
blocks,” and “If you get to the gas station, turn around—you’ve
gone too far.”

Computers are similar but have a different set of operations.


Some operations are mathematical, like “Take the square root of
a number,” whereas others include “Read a line from the file
named data.txt” and “Make a pixel blue.”

The most important difference between a computer and an old-


fashioned calculator is that you can “teach” a computer new
operations by defining them in terms of old ones. For example,
you can teach the computer that “Take the average” means “Add
up the numbers in a sequence and divide by the sequence’s
size.” You can then use the operations you have just defined to
create still more operations, each layered on top of the ones that
came before. It’s a lot like creating life by putting atoms
together to make proteins and then combining proteins to build
cells, combining cells to make organs, and combining organs to
make a creature.
Defining new operations and combining them to do useful
things is the heart and soul of programming. It is also a
tremendously powerful way to think about other kinds of
problems. As Professor Jeannette Wing wrote in Computational
Thinking [Win06], computational thinking is about the
following:

Conceptualizing, not programming. Computer science isn’t


computer programming. Thinking like a computer scientist means
more than being able to program a computer: it requires thinking at
multiple levels of abstraction.

A way that humans, not computers, think. Computational thinking


is a way humans solve problems; it isn’t trying to get humans to
think like computers. Computers are dull and boring; humans are
clever and imaginative. We humans make computers exciting.
Equipped with computing devices, we use our cleverness to tackle
problems we wouldn’t dare take on before the age of computing and
build systems with functionality limited only by our imaginations.

For everyone, everywhere. Computational thinking will be a reality


when it becomes so integral to human endeavors it disappears as an
explicit philosophy.

We hope that by the time you have finished reading this book,
you will see the world in a slightly different way.
What’s a Programming Language?
Directions to the nearest bus station can be given in English,
Portuguese, Mandarin, Hindi, and many other languages. As
long as the people you’re talking to understand the language,
they’ll get to the bus station.

In the same way, there are many programming languages, and


they all can add numbers, read information from files, and
make user interfaces with windows and buttons and scroll bars.
The instructions look different, but they accomplish the same
task. For example, in the Python programming language, here’s
how you add 3 and 4:

​3 + 4

But here’s how it’s done in the Scheme programming language:

​(+ 3 4)

They both express the same idea—they just look different.

Every programming language has a way to write mathematical


expressions, repeat a list of instructions a number of times,
choose which of two instructions to do based on the current
information you have, and much more. In this book, you’ll learn
how to do these things in the Python programming language.
Once you understand Python, learning the next programming
language will be much easier.
What’s a Bug?
Pretty much everyone has had a program crash. A standard
story is that you were typing in a paper when, all of a sudden,
your word processor crashed. You had forgotten to save, and
you had to start all over again. Old versions of Microsoft
Windows used to crash more often than they should have,
showing the dreaded “blue screen of death.” (Happily, they’ve
gotten a lot better in the past several years.) Usually, your
computer shows some kind of cryptic error message when a
program crashes.

What happened in each case is that the people who wrote the
program told the computer to do something it couldn’t do: open
a file that didn’t exist, perhaps, or keep track of more
information than the computer could handle, or maybe repeat a
task with no way of stopping other than by rebooting the
computer. (Programmers don’t mean to make these kinds of
mistakes, they are just part of the programming process.)

Worse, some bugs don’t cause a crash; instead, they give


incorrect information. (This is worse because at least with a
crash you’ll notice that there’s a problem.) As a real-life example
of this kind of bug, the calendar program that one of the authors
uses contains an entry for a friend who was born in 1978. That
friend, according to the calendar program, had his 5,875,542nd
birthday this past February. Bugs can be entertaining, but they
can also be tremendously frustrating.

Every piece of software that you can buy has bugs in it. Part of
your job as a programmer is to minimize the number of bugs
and to reduce their severity. In order to find a bug, you need to
track down where you gave the wrong instructions, then you
need to figure out the right instructions, and then you need to
update the program without introducing other bugs.

Every time you get a software update for a program, it is for one
of two reasons: new features were added to a program or bugs
were fixed. It’s always a game of economics for the software
company: are there few enough bugs, and are they minor
enough or infrequent enough in order for people to pay for the
software?

In this book, we’ll show you some fundamental techniques for


finding and fixing bugs and also show you how to prevent them
in the first place.
The Difference Between Brackets,
Braces, and Parentheses
One of the pieces of terminology that causes confusion is what
to call certain characters. Several dictionaries use these names,
so this book does too:

() Parentheses

[] Brackets

Braces (Some people call these curly brackets or curly braces,


{}
but we’ll stick to just braces.)
Installing Python
Installation instructions and use of the IDLE programming
environment are available on the book’s website:
http://pragprog.com/titles/gwpy3/practical-programming.

Copyright © 2018, The Pragmatic Bookshelf.


Chapter 2
Hello, Python
Programs are made up of commands that tell the computer
what to do. These commands are called statements, which the
computer executes. This chapter describes the simplest of
Python’s statements and shows how they can be used to do
arithmetic, which is one of the most common tasks for
computers and also a great place to start learning to program.
It’s also the basis of almost everything that follows.
How Does a Computer Run a Python
Program?
In order to understand what happens when you’re
programming, it helps to have have a mental model of how a
computer executes a program.

The computer is assembled from pieces of hardware, including


a processor that can execute instructions and do arithmetic, a
place to store data such as a hard drive, and various other
pieces, such as a screen, a keyboard, an Ethernet controller for
connecting to a network, and so on.

To deal with all these pieces, every computer runs some kind of
operating system, such as Microsoft Windows, Linux, or
macOS. An operating system, or OS, is a program; what makes
it special is that it’s the only program on the computer that’s
allowed direct access to the hardware. When any other
application (such as your browser, a spreadsheet program, or a
game) wants to draw on the screen, find out what key was just
pressed on the keyboard, or fetch data from storage, it sends a
request to the OS (see the top image).

This may seem like a roundabout way of doing things, but it


means that only the people writing the OS have to worry about
the differences between one graphics card and another and
whether the computer is connected to a network through
Ethernet or wireless. The rest of us—everyone analyzing
scientific data or creating 3D virtual chat rooms—only have to
learn our way around the OS, and our programs will then run
on thousands of different kinds of hardware.

Today, it’s common to add another layer between the


programmer and the computer’s hardware. When you write a
program in Python, Java, or Visual Basic, it doesn’t run directly
on top of the OS. Instead, another program, called an
interpreter or virtual machine, takes your program and runs it
for you, translating your commands into a language the OS
understands. It’s a lot easier, more secure, and more portable
across operating systems than writing programs directly on top
of the OS:

There are two ways to use the Python interpreter. One is to tell
it to execute a Python program that is saved in a file with a .py
extension. Another is to interact with it in a program called a
shell, where you type statements one at a time. The interpreter
will execute each statement when you type it, do what the
statement says to do, and show any output as text, all in one
window. We will explore Python in this chapter using a Python
shell.

Install Python Now (If You Haven't Already)


If you haven’t yet installed Python 3.6, please do so now. (Python 2 won’t do; there are
significant differences between Python 2 and Python 3, and this book uses Python 3.6.)
Locate installation instructions on the book’s website:
http://pragprog.com/titles/gwpy3/practical-programming.

Programming requires practice: you won’t learn how to program just by reading this book,
much like you wouldn’t learn how to play guitar just by reading a book on how to play guitar.

Python comes with a program called IDLE, which we use to write Python programs. IDLE
has a Python shell that communicates with the Python interpreter and also allows you to
write and run programs that are saved in a file.

We strongly recommend that you open IDLE and follow along with our examples. Typing in
the code in this book is the programming equivalent of repeating phrases back to an
instructor as you’re learning to speak a new language.
Expressions and Values: Arithmetic in
Python
You’re familiar with mathematical expressions like 3 + 4 (“three
plus four”) and 2 - 3 / 5 (“two minus three divided by five”);
each expression is built out of values like 2, 3, and 5 and
operators like + and -, which combine their operands in
different ways. In the expression 4 / 5, the operator is “/” and
the operands are 4 and 5.

Expressions don’t have to involve an operator: a number by


itself is an expression. For example, we consider 212 to be an
expression as well as a value.

Like any programming language, Python can evaluate basic


mathematical expressions. For example, the following
expression adds 4 and 13:

​​>>>​​​4​​+​13​
​17

The >>> symbol is called a prompt. When you opened IDLE, a


window should have opened with this symbol shown; you don’t
type it. It is prompting you to type something. Here we typed 4
+ 13, and then we pressed the Return (or Enter) key in order to
signal that we were done entering that expression. Python then
evaluated the expression.

When an expression is evaluated, it produces a single value. In


the previous expression, the evaluation of 4 + 13 produced the
value 17. When you type the expression in the shell, Python
shows the value that is produced.

Subtraction and multiplication are similarly unsurprising:

​​>>>​​​15​​​-​​3​
​12
​​>>>​​​4​​*​​7​
​28

The following expression divides 5 by 2:

​​>>>​​​5​​/​​2​
​2.5

The result has a decimal point. In fact, the result of division


always has a decimal point even if the result is a whole number:

​​>>>​​​4​​/​​2​
​2.0

TYPES
Every value in Python has a particular type, and the types of
values determine how they behave when they’re combined.
Values like 4 and 17 have type int (short for integer), and values
like 2.5 and 17.0 have type float. The word float is short for
floating point, which refers to the decimal point that moves
around between digits of the number.

An expression involving two floats produces a float:

​​>>>​​​17.0​​​-​​10.0​
​7.0

When an expression’s operands are an int and a float, Python


automatically converts the int to a float. This is why the
following two expressions both return the same answer:

​​>>>​​​17.0​​​-​​10​
​7.0
​​>>>​​​17​​​-​​10.0​
​7.0

If you want, you can omit the zero after the decimal point when
writing a floating-point number:

​​>>>​​​17​​​-​​10.​
​7.0
​​>>>​​​17.​​​- ​10​
​7.0

However, most people think this is bad style, since it makes


your programs harder to read: it’s very easy to miss a dot on the
screen and see 17 instead of 17..

INTEGER DIVISION, MODULO, AND


EXPONENTIATION
Every now and then, we want only the integer part of a division
result. For example, we might want to know how many 24-hour
days there are in 53 hours (which is two 24-hour days plus
another 5 hours). To calculate the number of days, we can use
integer division:

​​>>>​​​53​​​//​​​24​
​2

We can find out how many hours are left over using the modulo
operator, which gives the remainder of the division:

​​>>>​​​53​​​%​​24​
​5

Python doesn’t round the result of integer division. Instead, it


takes the floor of the result of the division, which means that it
rounds down to the nearest integer:

​​>>>​​​17​​​//​​​10​
​1

Be careful about using % and // with negative operands.


Because Python takes the floor of the result of an integer
division, the result is one smaller than you might expect if the
result is negative:

​​>>>​​​-17​​​//​​​10​
​-2

When using modulo, the sign of the result matches the sign of
the divisor (the second operand):

​​>>>​​​-17​​​%​​10​
​3
​​>>>​​​17​​​%​​-10​
​-3
For the mathematically inclined, the relationship between //
and % comes from this equation, for any two non-zero numbers
a and b:

​(b * (a // b) + a % b) is equal to a

For example, because -17 // 10 is -2, and -17 % 10 is 3; then 10 *


(-17 // 10) + -17 % 10 is the same as 10 * -2 + 3, which is -17.

Floating-point numbers can be operands for // and % as well.


With //, division is performed and the result is rounded down
to the nearest whole number, although the type is a floating-
point number:

​​>>>​​​3.3​​​//​​​1​
​3.0
​​>>>​​​3​​//​​1.0​
​3.0
​​>>>​​​3​​//​​1.1​
​2.0
​​>>>​​​3.5​​​//​​​1.1​
​3.0
​​>>>​​​3.5​​​//​​​1.3​
​2.0

The following expression calculates 3 raised to the 6th power:

​​>>>​​​3​​**​​​6​
​729

Operators that have two operands are called binary operators.


Negation is a unary operator because it applies to one operand:

​​>>>​​​-5​
​-5
​​>>>​​​--5
​5
​​>>>​​​---5​
​-5
What Is a Type?
We’ve now seen two types of numbers (integers and floating-
point numbers), so we ought to explain what we mean by a type.
In Python, a type consists of two things:

A set of values

A set of operations that can be applied to those values

For example, in type int, the values are …, -3, -2, -1, 0, 1, 2, 3, …
and we have seen that these operators can be applied to those
values: +, -, *, /, //, %, and **.

The values in type float are a subset of the real numbers, and it
happens that the same set of operations can be applied to float
values. We can see what happens when these are applied to
various values in Table 1, ​Arithmetic Operators​. If an operator
can be applied to more than one type of value, it is called an
overloaded operator.

Table 1. Arithmetic Operators

Symbol Operator Example Result

- Negation -5 -5

+ Addition 11 + 3.1 14.1

- Subtraction 5 - 19 -14
Symbol Operator Example Result

* Multiplication 8.5 * 4 34.0

/ Division 11 / 2 5.5

// Integer Division 11 // 2 5

% Remainder 8.5 % 3.5 1.5

** Exponentiation 2 ** 5 32

FINITE PRECISION
Floating-point numbers are not exactly the fractions you
learned in grade school. For example, look at Python’s version
of the fractions 2/3 and 5/3:

​​>>>​​​2​​/​​3​
​0.6666666666666666
​​>>>​​​5​​/​​3​
​1.6666666666666667

The first value ends with a 6, and the second with a 7. This is
fishy: both of them should have an infinite number of 6s after
the decimal point. The problem is that computers have a finite
amount of memory, and (to make calculations fast and memory
efficient) most programming languages limit how much
information can be stored for any single number. The number
0.6666666666666666 turns out to be the closest value to 2/3
that the computer can actually store in that limited amount of
memory, and 1.6666666666666667 is as close as we get to the
real value of 5/3.

More on Numeric Precision

Integers (values of type int) in Python can be as large or as small as you like. However, float
values are only approximations to real numbers. For example, 1/4 can be stored exactly, but
as we’ve already seen, 2/3 cannot. Using more memory won’t solve the problem, though it
will make the approximation closer to the real value, just as writing a larger number of 6s
after the 0 in 0.666… doesn’t make it exactly equal to 2/3.

The difference between 2/3 and 0.6666666666666666 may look tiny. But if we use
0.6666666666666666 in a calculation, then the error may get compounded. For example, if
we add 1 to 2/3, the resulting value ends in …6665, so in many programming languages, 1 +
2/3 is not equal to 5/3:

​​>>>​​​2​​/​​3​​+​​1​
​1.6666666666666665
​​>>>​​​5​​/​​3​
​1.6666666666666667

As we do more calculations, the rounding errors can get larger and larger, particularly if we’re
mixing very large and very small numbers. For example, suppose we add 10000000000 (10
billion) and 0.00000000001 (there are 10 zeros after the decimal point):

​​>>>​​​10000000000​​​+​​0.00000000001​
​10000000000.0

The result ought to have twenty zeros between the first and last significant digit, but that’s
too many for the computer to store, so the result is just 10000000000—it’s as if the addition
never took place. Adding lots of small numbers to a large one can therefore have no effect at
all, which is not what a bank wants when it totals up the values of its customers’ savings
accounts.

It’s important to be aware of the floating-point issue. There is no magic bullet to solve it,
because computers are limited in both memory and speed. Numerical analysis, the study of
algorithms to approximate continuous mathematics, is one of the largest subfields of
computer science and mathematics.

Here’s a tip: If you have to add up floating-point numbers, add them from smallest to largest
in order to minimize the error.
Discovering Diverse Content Through
Random Scribd Documents
“I’ve been told the same,” Dave nodded, quietly.

“What brought you in?”

“Shell hits, I think they were, though one dent might have been
made by a torpedo,” Darrin answered.

“Then you had a fight.”

“A short one.”

“And the German pest?”

“Went to the bottom. I know, for we saw her sink, and her conning
tower was so damaged that she couldn’t have kept the water out,
once she went under. Besides, we found the surface of the water
covered with oil.”

“I’ll wager you did,” agreed Chetwynd, heartily. “You Yankee sailors
have sunk dozens of the pests.”

“And hope to sink scores more,” Darrin assured him.

“Oh, you’ll do it,” came the confident answer. “But come on upstairs
with us. We’ve a private parlor and a piano, and plan a jolly hour or
two.”

From one end of the room, in a lull in the singing, an exasperated


English voice rose on the air.

“What I can’t understand,” the speaker cried, “is that the enemy
appear to have every facility for getting the latest gossip right out of
this port. And they know every time that a liner, a freighter or a
warship sails from this port. There is some spy service on shore that
communicates with the German submarine commanders.”

“I’d like to catch one of the rascally spies!” Dan uttered to a young
English officer.
“What would you do with him?” bantered the other.

“Cook him!” retorted Dan, vengefully. “I don’t know in just what


form; probably fricassee him.”

Little did Dalzell dream how soon the answer to the spy problem
would come to him.
CHAPTER II—THE MEETING WITH A PIRATE

Thirty-six hours’ work at the dry dock, with changing shifts, put the
“Logan” in shape to start seaward again.

Under another black sky, moving into thick weather, the “Logan”
swung off at slow speed, with little noise from engines or propellers.

“I feel as if something were going to happen to-night,” said Dalzell,


coming to the bridge at midnight after a two-hour nap. A little
shudder ran over his body.

“I hope something does,” agreed Darrin, warmly. “But remember—


no Jonah forebodings!”

“I—I think it will be something good!” hesitated Dalzell.

“Good or bad, have me called at six bells,” Dave instructed his


second in command. “Before that, of course, if anything turns up.”

He went slowly down and entered the chart-room, closing the


curtains after him. Taking off his sheepskin coat and hanging it up,
Dave dropped into a chair, pulling a pair of blankets over him. Inside
of thirty seconds he was sound asleep, dreaming, perhaps, of the
night before at the hotel, when he had enjoyed the luxury of
removing his clothing and sleeping between sheets.

At three o’clock to the minute a messenger entered and roused him.


How Darrin hated to get up! He was horribly sleepy, yet he was on
his feet in a twinkling, removing the service blouse that he had worn
while sleeping, and dashing cold water in his face. A hurried toilet
completed, he drew on and buttoned his blouse, next donned his
sheepskin coat and cap, and went out into the dark of the early
morning.

“All secure, sir!” reported Dalzell, from the bridge, meaning that
reports had come in from all departments of the craft that all was
well.

“You had better turn in, Mr. Dalzell,” Dave called, before he began to
pace the deck.

“I’m not sleepy, sir,” lied Dalzell, like the brave young gentleman that
he was in all critical times. Dan knew that from now until sun-up was
the tune that called for utmost vigilance.

Darrin busied himself, as he did frequently every day, by going about


the ship, on deck and below deck, on a tour of inspection. This
occupied him for nearly an hour. Then he climbed to the bridge.

“Better turn in and get a nap, Danny-boy,” he urged, in an


undertone.

“Say!” uttered Danny Grin. “You must know something big is coming
off, and you don’t want me to have a hand in it!”

Dave picked up his night glass and began to use it in an effort to


help out his subordinate, who stood near him. From time to time
Dan also used a glass. A freshening breeze blew in their faces as the
boat lounged indolently along on its way. It was drowsy work, yet
every officer and man needed to be constantly on the alert.

Despite his denials that he was sleepy, Danny Grin braced himself
against a stanchion of the bridge frame and closed his eyes briefly,
just before dawn. He wouldn’t have done it had he been the ranking
officer on the bridge, but he felt ghastly tired, and Darrin and Ensign
Tupper were there and very much awake.
With a start Dan presently came to himself, realizing that he had lost
consciousness for a few seconds.

“Oh, it’s all right,” Dan murmured to himself. “Neither Davy nor Tup
will know that I’m slipping in half a minute of doze.”

His eyes closing again, despite the roll of the craft, he was soon
sound enough asleep to dream fitfully.

And so he stood when the first streaks of dawn appeared astern. It


was still dark off over the waters, but the slow-moving destroyer
stood vaguely outlined against the eastern streaks in the sky.

Ensign Tupper was observing the compass under the screened


binnacle light, and Darrin, glass to his eyes, was peering off to
northward when the steady, quick tones of a man of the bow watch
reached the bridge:

“’Ware torpedo, coming two points off port bow!”

That seaman’s eyesight was excellent, for the torpedo was still far
enough away so that Dave had time to order a sharp swerve to port,
and to send a quick signal to the engine room. As the craft turned
she fairly jumped forward. The “Logan” was now facing the
torpedo’s course, and seemed a bare shade out of its path, but the
watchers held their breath during those fractions of a second.

Then it went by, clearing the destroyer amidships by barely two feet.
Nothing but the swiftness of Darrin’s orders and the marvelously
quick responses from helmsman and engineer had saved the
destroyer from being hit.

On Dave’s lips hovered the order to dash forward over the course by
which the torpedo had come, which is the usual procedure of
destroyer commanders when attacking a submarine.
Instead, as the idea flashed into his head, he ordered the ship
stopped.

Danny Grin had come out of his “forty winks” at the hail of the bow
watch. Now Dave spoke to him hurriedly. Dalzell fairly leaped down
from the bridge, hurrying amidships.

“All hands stand by to abandon ship!” rang the voice of Ensign


Tupper, taking his order from Darrin. The alarm to abandon ship was
sounded all through the ship.

There was a gasp of consternation, but Dalzell had already met and
spoken to three of the junior officers, and these quickly carried the
needed word.

The light was yet too faint, and would be for a few minutes, to find
such a tantalizingly tiny object as a submarine’s periscope at a
distance even of a few hundred yards. Lieutenant-Commander
Darrin, therefore, had hit upon a simple trick that he hoped would
prove effective. All depended upon the speed with which his ruse
could be carried out. Cold perspiration stood out over Darrin as he
realized the chances he was taking.

“Bow watch, there! Keep sharp lookout for torpedoes! Half a second
might save us!”

Tupper stood with hand on the engine-room telegraph. He already


had warned the engineer officer in charge to stand by for quick
work.

Dalzell and the officers to whom Darrin had spoken saw to it that
nearly all of the men turned out and rushed to the boats. Even the
engineer department off watch came tumbling up in their distinctive
clothing.

To an onlooker it would have appeared like a real stampede for the


boats. Tackle creaked, making a louder noise than usual, but
seeming to “stick” as an effort was made to lower loaded boats. The
men in boats and at davits were grinning, for their officers had
explained the trick.

Dawn’s light streaks had become somewhat more distinct as Dave


peered ahead. Mr. Beatty and three men crouched low behind one of
the forward guns.

The submarine commander must have rubbed his eyes, for, while he
had observed no signs of a hit, he saw the American craft drifting on
the water and the crew frantically trying to abandon ship.

Then the thing for which Darrin had hoped and prayed happened.
The enemy craft’s conning tower appeared above water four
hundred yards away.

“The best shot you ever made in your life, Mr. Beatty!” called Dave in
an anxious voice.

The officer behind the gun had been ready all the time. At the first
appearance of the conning tower he had drawn the finest sight
possible.

The three-inch gun spoke. It seemed ages ere the shell reached its
destination.

Then what a cheer ascended as the crew came piling on board from
the boats. The conning tower of the submarine had been fairly
struck and wrecked.

“Half speed ahead!” commanded Dave’s steady voice, while Dan


gave the helmsman his orders. As Tupper sent the signal below the
“Logan” gathered headway.

But Darrin had not finished, for on the heels of his first order came
the second:
“Open on her with every gun!”

After the wrecking of his conning tower the German commander


began to bring his craft to the surface. Perhaps it was his intention
to surrender.

“Full speed ahead!” roared Darrin, and Ensign Tupper rang in the
signal.

The hull of the submarine was hardly more than awash when five or
six shots from the “Logan” struck it at about the same time.

Veering around to the southward the “Logan” prepared to circle the


dying enemy. The German craft filled and sank, and Darrin presently
gazed overboard at the oil-topped waters through which he was
passing.

“A wonderful job! I wonder that you had the nerve to risk it,”
muttered Dalzell.

“I don’t know whether it was a wonderful job, or a big fool risk,”


Dave almost chattered. “It would have been a fool trick if I had lost
the ship by it. I don’t believe that I shall ever try it again.”

“If you hadn’t done just what you did, a second torpedo would have
been sent at you,” murmured Dalzell. “You saved the ‘Logan’ and
‘got’ the enemy, if you want to know.”

Grinning, for the responsibility had not been theirs, and the ruse had
“worked,” the men of the watch returned to their usual stations,
while those off duty returned to their “watch below.” Darrin,
however, was shaking an hour later. He had dropped the usual
method of defense for once and had tried a trick by which he might
have lost his craft. As commander he knew that he had discretionary
powers, but at the same time he realized that he had taken a
desperate chance.
“Oh, stop that, now!” urged Danny Grin. “If you had steamed
straight at the submarine you would have taken even bigger chances
of losing the ‘Logan.’ Even had she given up the fight and dived,
there wasn’t light enough for you to follow by any trail of bubbles
the enemy might have left. The answer, David, little giant, is that the
submarine is now at the bottom, and every Hun aboard is now a
dead man. In this war the commander who wins victories is the only
one who counts.”

Through that day Dave and Dan slept, alternately, only an hour or
two at a time. All they sighted were three cargo steamers, two
headed toward Liverpool and one returning to “an American port.”

At nine o’clock in the evening Darrin, after another hour’s nap, softly
parted the curtains of the chart-room door and peered out. He saw a
young sailor standing just back of the open doorway of the radio
room. Slight as it was there was a something in the sailor’s attitude
of listening that Darrin did not quite like. He stepped out on the
deck.

Sighting him, the sailor saluted.

“Jordan!” called Dave, even before his hand reached his visor cap in
acknowledgment of the salute.

“Yes, sir!” answered the seaman, coming to attention.

“You belong to this watch?”

“Yes, sir.”

“Your station is with the stern watch?”

“Yes, sir.”

“Then what are you doing forward?”


“I left my station, by permission, to go below, sir.”

“Have you been below?”

“Yes, sir.”

“Then why are you loitering here?”

Seaman Jordan hesitated, shifted on his feet, glanced down, then


hurriedly replied:

“I—I don’t know, sir. I just stopped here a moment. There’s a relief
man in my place, sir.”

“Return to your station, Jordan!”

“Aye, aye, sir,” replied the sailor, saluting, wheeling and walking
away.

“And I’ll keep my eye on you,” mused Darrin, as he watched the


departing sailor. “I may be wrong, but when I first sighted him there
was a look on that lad’s face that I didn’t like.”

Even before he reached his station Seaman Jordan was quaking


inwardly more apprehensively than is usual with a sailor caught in a
slight delinquency.
CHAPTER III—QUICK “DOINGS” OVER THE SHOAL

For several days after that Darrin and the “Logan” cruised back and
forth over the area assigned for patrol. During these days nothing
much happened out of the usual. Then came a forenoon when
Darrin received a wireless message, in code, ordering him to report
back at once to the commanding officer of the destroyer patrol.

Mid afternoon found the “Logan” fifteen miles off the port of
destination.

“Be on the alert every instant,” was the order Darrin gave out to
officers and men. “There have been several sinkings, the last month,
in these waters. We are nearing Fisherman’s Shoal, which is believed
to be a favorite bit of ground for submarines that hide on the
bottom.”

Over Fisherman’s Shoal the water was only about seventy feet in
depth—an ideal spot for a lurking, hiding undersea craft.

Five minutes later the bow lookout announced quietly:

“Trail of bubbles ahead, sir.”

Leaving Ensign Phelps on the bridge, Dave and Dan darted down
and forward.

A less practised eye might have seen nothing worth noting, but to
the two young officers the trail ahead was unmistakable, though
Darrin quickly brought up his glass to aid his vision.

“Pass the word for slow speed, Mr. Dalzell,” Dave commanded,
quietly. “We want to keep behind that craft for a moment. Pass word
to Mr. Briggs to stand by ready to drop a depth bomb.”

Quietly as the orders were given, they were executed with lightning
speed. The destroyer began to move more slowly, keeping well
behind the bubble trail. At any instant, however, the “Logan” could
be expected to leap forward, dropping the depth bomb at just the
right moment. Then would come a muffled explosion, and, if the
bomb were rightly placed, a broad coating of oil would appear upon
the surface.

Dave was now in the very peak of the bow. Watching the bubbly trail
he knew that the hidden enemy craft was moving more slowly than
the destroyer, and he signalled for bare headway. And now the
bubbles were rising as though from a stationary object under the
waves.

“Buoy, there!” he ordered, quickly. “Overboard with it.”

Slowly the destroyer moved past the spot, but the weighted,
bobbing buoy marked the spot plainly.

“Have a diver ready, Mr. Dalzell,” Dave called. “Make ready to clear
away a launch!”

In the matter of effective speed Darrin’s officers and crew had been
trained to the last word. Only a few hundred yards did the “Logan”
move indolently along, then lay to.

Soon after that the diver and launch were ready. Dave stepped into
the launch to take command himself.

“May I go, too, sir?” asked Dan Dalzell, saluting. “I haven’t seen this
done before.”

“Clear away a second launch, Mr. Dalzell. The crew will be armed.
You will take also a corporal and squad of marines.”
That meant the entire marine force aboard the “Logan.” Dalzell
quickly got his force together, while Darrin gave orders to pull back
to where the bobbing buoy lay on the water.

“Ready, diver?” called Dave, as the launch backed water and stopped
beside the buoy.

“Aye, aye, sir.” The diver’s helmet was fitted into position and the air
pump started. The diver signalled that he was ready to go down.

“Men, stand by to help him over the side,” Darrin commanded. “Over
he goes!”

Hugging a hammer under one arm the diver took hold of the flexible
cable ladder as soon as it had been lowered. Sailors paid out the
rope, life line and air pipe as the man in diver’s suit vanished under
the water.

Down and down went the diver, a step at a time. The buoy had been
placed with such exactness that he did not have to step from the
ladder to the sandy bottom. Instead, he stepped on to the deck of a
great lurking underseas craft.

He must have grinned, that diver, as he knelt on top of the gray hull
and hammered briskly, in the International Code, this message to
the Germans inside the submarine shell:

“Come up and surrender, or stay where you are and take a bomb!
Which do you want?”

Surely he grinned hard, under his diver’s mask, as he noted the time
that elapsed. He knew full well that his hammered message had
been heard and understood by the trapped Huns. He could well
imagine the panic that the receipt of the message had caused the
enemy.
“We’ll send you a bomb, then?” the diver rapped on the hull with his
hammer. “I’m going up.”

To this there came instant response. From the inside came the
hammered message:

“Don’t bomb! We’ll rise and surrender!”

Chuckling, undoubtedly, the diver signalled and was hoisted to the


surface. The instant that his head showed above water the seaman-
diver nodded three times toward Darrin. Then he was hauled into
the boat, and the launch pulled away from the spot.

“It took the Huns some time to make up their minds?” queried Dave
Darrin smilingly, after the diver’s helmet had been removed.

“They didn’t answer until they got the second signal, sir,” replied the
diver.

Dalzell’s launch was hovering in the near vicinity, filled with sailors
and marines, a rapid-fire one-pounder mounted in the bow.

Both boats were so placed as not to interfere with gun-fire from the
“Logan.” Officers and men alike understood that the Huns might
attempt treachery after their promise to surrender.

Soon the watchers glimpsed a vague outline rising through the


water. The top of a conning tower showed above the water, then the
rest of it, and last of all the ugly-looking hull rose until the craft lay
fully exposed on the surface of the sea.

The critical moment was now at hand. It would be possible for the
submarine to torpedo the destroyer; there was grave danger of the
attempt being made even though the vengeful Germans knew that
in all probability their own lives would pay the penalty.
The hatch in the tower opened and a young German officer stepped
out, waving a white handkerchief. He was followed by several
members of the crew. It was evident that the enemy had elected to
save their lives, and smiles of grim satisfaction lighted the faces of
the watchful American jackies.

“Give way, and lay alongside,” Dave ordered his coxswain, while
signalling Dalzell to keep his launch back for the present.

Then Dave addressed the young German officer:

“You understand English?”

“Yes,” came the reply, with a scowl.

“We are coming alongside. Your officers and men will be searched
for weapons, then transferred, in detachments, to our launch, and
taken aboard our craft.”

The German nodded, addressing a few murmured words to his men,


who moved well up forward on the submarine’s slippery deck.

As the launch drew alongside two seamen leaped to the submarine’s


deck and held the lines that made the launch fast to it.

Half a dozen armed seamen sprang aboard, with Darrin, who


signalled to the second launch to come up on the other side of the
German boat.

“Be good enough, sir, to order the rest of your men on deck,” Dave
directed, and the German officer shouted the order in his own
tongue. More sullen-looking German sailors appeared through the
conning tower and lined up forward.

“Did you command here?” Dave demanded of the officer.

“No; my commander is below. I am second in command.”


Dave stepped to the conning tower, bawling down in English:

“All hands on deck. Lively.”

Another human stream answered. Darrin turned to the German


officer to ask:

“Are all your crew on deck now?”

Quickly counting, the enemy officer replied:

“Yes; all.”

“And your captain?”

“I do not know why he is not here. I cannot give him orders.”

By this time the marines were aboard from the second launch.
Already the first detachment of German sailors, after search, was
being transferred to the launch.

“Corporal,” called Darrin, “take four men and go below to find the
commander. Watch out for treachery, and shoot fast if you have to.”

“Aye, aye, sir,” returned the corporal, saluting and entering the
tower. His men followed him closely.

“I’ve seen the outside of enough of these pests,” said Dave to his
chum. “Suppose we go below and see what the inside looks like. The
German submarines are different from our own.”

Dalzell nodded and followed, at the same time ordering a couple of


stalwart sailors to follow. A boatswain’s mate now remained in
command on the submarine deck.

“You get back there!” growled the corporal. Dave reached the lower
deck just in time to see the corporal pointing his revolver at a
protesting German naval officer.
“Look what he’s been doing, sir,” called the corporal. “Look on the
floor, sir.”

On the deck lay a heap of charred papers, still smoking.

Charred papers still smoking.

“If I’d got down a minute earlier, sir, he wouldn’t have had a chance
to have that nice little bonfire,” grumbled the corporal.

Dave gave a great start as he took his first look at the face of the
German captain.

As for the German, he seemed at least equally disconcerted. Dave


Darrin was the first to recover.
“I cannot say that I think your German uniform becoming to a man
of your name, Mr. Matthews,” Darrin uttered, in savage banter.

“Matthews?” repeated the German, in a puzzled voice, though he


spoke excellent English. “I cannot imagine why you should apply
that name to me.”

“It’s your own fault if you can’t,” Darrin retorted. “It’s the name you
gave me at the hotel.”

“I’ve never seen you until the present moment,” declared the
German, stoutly.

“Surely you have,” Danny Grin broke in. “And how is your firm in
Chicago, Mr. Matthews?”

“Chicago?” repeated the German, apparently more puzzled than


before.

“If Matthews isn’t your name, and I believe it isn’t,” Darrin


continued, “by what name do you prefer to be addressed.”

“I am Ober-Lieutenant von Bechtold,” replied the German.

“Very good, von Bechtold; will you stand back a bit and not bother
the corporal?”

Dave bent over to stir the charred, smoking heap of paper with his
foot. But the job had been too thoroughly done. Not a scrap of white
paper could be found in the heap.

“Of course you do not object to telling me what papers you


succeeded in burning,” Darrin bantered.

Ober-Lieutenant von Bechtold smiled.

“You wouldn’t believe me, if I told you, so why tax your credulity?”
came his answer.
“Perhaps you didn’t have time to destroy all your records,” Dave
went on. “Under the circumstances I know you will pardon me for
searching the boat.”

Thrusting aside a curtain, Dave entered a narrow passageway near


the stern. Off this passageway were the doors of two sleeping cabins
on either side. Dave opened the doors on one side and glanced in.
Dan opened one on the other side, but the second door resisted his
efforts.

“This locked cabin may contain whatever might be desired to


conceal,” Dan hinted.

Turning quickly, Darrin saw that von Bechtold had followed. This the
corporal had permitted, but he and a marine private had followed, to
keep their eyes on the prisoner.

“If you have the key to this locked door, Captain, it will save us the
trouble of smashing the door,” Dave warned. He had followed the
usual custom in terming the ober-lieutenant a captain since he had
an independent naval command.

“I do not know where the key is,” replied von Bechtold, carelessly.
“You may break the door down, if you wish, but you will not be
repaid for your trouble.”

“I’ll take the trouble, anyway,” Darrin retorted. “Mr. Dalzell, your
shoulder and mine both together.”

As the two young officers squared themselves for the assault on the
door a black cloud appeared briefly on von Bechtold’s face. But as
Darrin turned, after the first assault, the deep frown was succeeded
by a dark smile of mockery.

Bump! bump! At the third assault the lock of the door gave way so
that Dave and Dan saved themselves from pitching into the room
headfirst.
“Oh, whew!” gasped Danny Grin.

An odor as of peach-stone kernels assailed their nostrils. They


thought little of this. It was a sight, rather than the odor, that
instantly claimed their attention.

For on the berth, over the coverlid, and fully dressed in civilian attire
of good material, lay a man past fifty, stout and with prominent
abdomen. He was bald-headed, the fringe of hair at the sides being
strongly tinged with gray.

At first glance one might have believed the stranger to be merely


asleep, though he would have been a sound sleeper who could
slumber on while the door was crashing in. Dave stepped close to
the berth.

Dalzell followed, and after them came the submarine’s commander.

“You will go back to the cabin and remain there, Mr. von Bechtold,”
Dave directed, without too plain discourtesy. “Corporal, detail one of
your men to remain with the prisoner, and see that he doesn’t come
back here unless I send for him. Also see to it that he doesn’t do
anything else except wait.”

Scowling, von Bechtold withdrew, the marine following at his heels.

As Darrin stepped back into the cabin he saw the stranger lying as
they left him.

“Dead!” uttered Dave, bending over the man and looking at him
closely. “He lay down for a nap. Look, Dan, how peaceful his
expression is. He never had an intimation that it was his last sleep,
though this looks like suicide, not accidental death, for the peach-
stone odor is that of prussic acid. He has killed himself with a swift
poison. Why? Is it that he feared to fall into enemy hands and be
quizzed?”
“A civilian, and occupying an officer’s cabin,” Dan murmured. “He
must have been of some consequence, to be a passenger on a
submarine. He wasn’t a man in the service, or he would have been
in uniform.”

“We’ll know something about him, soon, I fancy,” Darrin went on.
“Here is a wallet in his coat pocket, also a card case and an envelope
well padded with something. Yes,” glancing inside the envelope,
“papers. I think we’ll soon solve the secret of this civilian passenger
who has met an unplanned death.”

“Here, you! Stop that, or I’ll shoot!” sounded, angrily, the voice of
von Bechtold’s guard behind them.

But the German officer, regardless of threats, had dashed past the
marine, and was now in the passageway.

“Here, I’ll soon settle you!” cried the marine, wrathfully. But he
didn’t, for von Bechtold let a solid fist fly, and the marine, caught
unawares, was knocked to the floor.

All in a jiffy von Bechtold reached his objective, the envelope.


Snatching it, he made a wild leap back to the cabin, brushing the
marine private aside like a feather.

“Grab him!” yelled Dave Darrin, plunging after the German. “Don’t
let him do anything to that envelope!”
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!

textbookfull.com

You might also like