Skip to content

Commit fb6404e

Browse files
committed
Fixing problems in Chapter 11
1 parent fe91fc4 commit fb6404e

File tree

1 file changed

+67
-53
lines changed

1 file changed

+67
-53
lines changed

book/book.tex

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
\title{Think Python}
3434
\author{Allen B. Downey}
3535
\newcommand{\thetitle}{Think Python: How to Think Like a Computer Scientist}
36-
\newcommand{\theversion}{2nd Edition, Version 2.2.10}
36+
\newcommand{\theversion}{2nd Edition, Version 2.2.14}
3737
\newcommand{\thedate}{}
3838

3939
% these styles get translated in CSS for the HTML version
@@ -330,10 +330,8 @@ \section*{The strange history of this book}
330330

331331

332332
Allen B. Downey \\
333-
Needham MA\\
334333

335-
Allen Downey is a Professor of Computer Science at
336-
the Franklin W. Olin College of Engineering.
334+
Olin College \\
337335

338336

339337
\section*{Acknowledgments}
@@ -376,7 +374,7 @@ \section*{Contributor List}
376374
huge help.
377375

378376
If you have a suggestion or correction, please send email to
379-
{\tt feedback@thinkpython2.com}. If I make a change based on your
377+
{\tt feedback@thinkpython.com}. If I make a change based on your
380378
feedback, I will add you to the contributor list
381379
(unless you ask to be omitted).
382380

@@ -733,8 +731,14 @@ \section*{Contributor List}
733731
\item Andrea Zanella translated the book into Italian, and sent a
734732
number of corrections along the way.
735733

736-
\item Many thanks to Melissa Lewis for excellent comments and
737-
suggestions on the second edition.
734+
\item Many, many thanks to Melissa Lewis and Luciano Ramalho for
735+
excellent comments and suggestions on the second edition.
736+
737+
\item Thanks to Harry Percival from PythonAnywhere for his help
738+
getting people started running Python in a browser.
739+
740+
\item Xavier Van Aubel made several useful corrections in the second
741+
edition.
738742

739743
% ENDCONTRIB
740744

@@ -1098,14 +1102,14 @@ \section{Formal and natural languages}
10981102
Syntax rules come in two flavors, pertaining to {\bf tokens} and
10991103
structure. Tokens are the basic elements of the language, such as
11001104
words, numbers, and chemical elements. One of the problems with
1101-
$3 + = 3 \$ 6$ is that \( \$ \) is not a legal token in mathematics
1105+
$3 += 3 \$ 6$ is that \( \$ \) is not a legal token in mathematics
11021106
(at least as far as I know). Similarly, $_2Zz$ is not legal because
11031107
there is no element with the abbreviation $Zz$.
11041108
\index{token}
11051109
\index{structure}
11061110

11071111
The second type of syntax rule pertains to the way tokens are
1108-
combined. The equation $3 + = 3$ is illegal because even though $+$
1112+
combined. The equation $3 += 3$ is illegal because even though $+$
11091113
and $=$ are legal tokens, you can't have one right after the other.
11101114
Similarly, in a chemical formula the subscript comes after the element
11111115
name, not before.
@@ -4660,8 +4664,8 @@ \section{Incremental development}
46604664
return 0.0
46614665
\end{verbatim}
46624666
%
4663-
If the function is working, it should display \verb"'dx is 3'" and {\tt
4664-
'dy is 4'}. If so, we know that the function is getting the right
4667+
If the function is working, it should display \verb"dx is 3" and
4668+
\verb"dy is 4". If so, we know that the function is getting the right
46654669
arguments and performing the first computation correctly. If not,
46664670
there are only a few lines to check.
46674671

@@ -4728,7 +4732,7 @@ \section{Incremental development}
47284732

47294733
As an exercise, use incremental development to write a function
47304734
called {\tt hypotenuse} that returns the length of the hypotenuse of a
4731-
right triangle given the lengths of the two legs as arguments.
4735+
right triangle given the lengths of the other two legs as arguments.
47324736
Record each stage of the development process as you go.
47334737
\index{hypotenuse}
47344738

@@ -5144,8 +5148,9 @@ \section{Debugging}
51445148
\label{factdebug}
51455149

51465150
Breaking a large program into smaller functions creates natural
5147-
checkpoints for debugging. \index{debugging} If a function is not
5151+
checkpoints for debugging. If a function is not
51485152
working, there are three possibilities to consider:
5153+
\index{debugging}
51495154

51505155
\begin{itemize}
51515156

@@ -6669,7 +6674,7 @@ \section{Exercises}
66696674
\index{method!string}
66706675

66716676
Read the documentation of the string methods at
6672-
\url{http://docs.python.org/2/library/stdtypes.html#string-methods}.
6677+
\url{http://docs.python.org/3/library/stdtypes.html#string-methods}.
66736678
You might want to experiment with some of them to make sure you
66746679
understand how they work. {\tt strip} and {\tt replace} are
66756680
particularly useful.
@@ -6692,7 +6697,7 @@ \section{Exercises}
66926697
There is a string method called {\tt count} that is similar
66936698
to the function in Section~\ref{counter}. Read the documentation
66946699
of this method
6695-
and write an invocation that counts the number of {\tt a}s
6700+
and write an invocation that counts the number of {\tt a}'s
66966701
in \verb"'banana'".
66976702
\end{exercise}
66986703

@@ -6703,7 +6708,7 @@ \section{Exercises}
67036708
\index{operator!slice}
67046709

67056710
A string slice can take a third index that specifies the ``step
6706-
size;'' that is, the number of spaces between successive characters.
6711+
size''; that is, the number of spaces between successive characters.
67076712
A step size of 2 means every other character; 3 means every third,
67086713
etc.
67096714

@@ -6817,7 +6822,7 @@ \chapter{Case study: word play}
68176822
properties. For example, we'll find the longest palindromes
68186823
in English and search for words whose letters appear in
68196824
alphabetical order. And I will present another program development
6820-
plan: reduction to a previously-solved problem.
6825+
plan: reduction to a previously solved problem.
68216826

68226827

68236828
\section{Reading word lists}
@@ -7065,18 +7070,18 @@ \section{Search}
70657070

70667071
If you were really thinking like a computer scientist, you would
70677072
have recognized that \verb"uses_all" was an instance of a
7068-
previously-solved problem, and you would have written:
7073+
previously solved problem, and you would have written:
70697074

70707075
\begin{verbatim}
70717076
def uses_all(word, required):
70727077
return uses_only(required, word)
70737078
\end{verbatim}
70747079
%
70757080
This is an example of a program development plan called {\bf
7076-
reduction to a previously-solved problem}, which means that you
7081+
reduction to a previously solved problem}, which means that you
70777082
recognize the problem you are working on as an instance of a solved
70787083
problem and apply an existing solution. \index{reduction to a
7079-
previously-solved problem} \index{development plan!reduction}
7084+
previously solved problem} \index{development plan!reduction}
70807085

70817086

70827087
\section{Looping with indices}
@@ -7160,9 +7165,9 @@ \section{Looping with indices}
71607165
return True
71617166
\end{verbatim}
71627167

7163-
Or we could reduce to a previously-solved
7168+
Or we could reduce to a previously solved
71647169
problem and write:
7165-
\index{reduction to a previously-solved problem}
7170+
\index{reduction to a previously solved problem}
71667171
\index{development plan!reduction}
71677172

71687173
\begin{verbatim}
@@ -7226,9 +7231,9 @@ \section{Glossary}
72267231
\index{file object}
72277232
\index{object!file}
72287233

7229-
\item[reduction to a previously-solved problem:] A way of solving a
7230-
problem by expressing it as an instance of a previously-solved
7231-
problem. \index{reduction to a previously-solved problem}
7234+
\item[reduction to a previously solved problem:] A way of solving a
7235+
problem by expressing it as an instance of a previously solved
7236+
problem. \index{reduction to a previously solved problem}
72327237
\index{development plan!reduction}
72337238

72347239
\item[special case:] A test case that is atypical or non-obvious
@@ -8365,7 +8370,7 @@ \section{Glossary}
83658370
\section{Exercises}
83668371
83678372
You can download solutions to these exercises from
8368-
from \url{http://thinkpython2.com/code/list_exercises.py}.
8373+
\url{http://thinkpython2.com/code/list_exercises.py}.
83698374
83708375
\begin{exercise}
83718376
@@ -8629,7 +8634,7 @@ \section{A dictionary is a mapping}
86298634
\end{verbatim}
86308635
%
86318636
This line creates an item that maps from the key
8632-
{\tt 'one'} to the value \verb"'uno'". If we print the
8637+
\verb"'one'" to the value \verb"'uno'". If we print the
86338638
dictionary again, we see a key-value pair with a colon
86348639
between the key and value:
86358640
@@ -8666,7 +8671,7 @@ \section{A dictionary is a mapping}
86668671
'dos'
86678672
\end{verbatim}
86688673
%
8669-
The key {\tt 'two'} always maps to the value \verb"'dos'" so the order
8674+
The key \verb"'two'" always maps to the value \verb"'dos'" so the order
86708675
of the items doesn't matter.
86718676
86728677
If the key isn't in the dictionary, you get an exception:
@@ -8798,7 +8803,7 @@ \section{Dictionary as a collection of counters}
87988803
{'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}
87998804
\end{verbatim}
88008805
%
8801-
The histogram indicates that the letters {\tt 'a'} and \verb"'b'"
8806+
The histogram indicates that the letters \verb"'a'" and \verb"'b'"
88028807
appear once; \verb"'o'" appears twice, and so on.
88038808
88048809
@@ -8850,14 +8855,23 @@ \section{Looping and dictionaries}
88508855
o 1
88518856
\end{verbatim}
88528857
%
8853-
Again, the keys are in no particular order.
8854-
Dictionaries have a method called {\tt keys} that returns
8855-
the keys of the dictionary, in no particular order, as a list.
8856-
As an exercise, modify \verb"print_hist" to print the keys and their values
8857-
in alphabetical order.
8858+
Again, the keys are in no particular order. To traverse the keys
8859+
in sorted order, you can use the built-in function {\tt sorted}:
88588860
\index{keys method}
88598861
\index{method!keys}
88608862
8863+
\begin{verbatim}
8864+
>>> for key in sorted(h):
8865+
... print(key, h[key])
8866+
a 1
8867+
o 1
8868+
p 1
8869+
r 2
8870+
t 1
8871+
\end{verbatim}
8872+
8873+
%TODO: get this on Atlas
8874+
88618875
88628876
\section{Reverse lookup}
88638877
\label{raise}
@@ -8891,7 +8905,7 @@ \section{Reverse lookup}
88918905
This function is yet another example of the search pattern, but it
88928906
uses a feature we haven't seen before, {\tt raise}. The
88938907
{\bf raise statement} causes an exception; in this case it causes a
8894-
{\tt LookupError}, which is a built-in exception use to indicate
8908+
{\tt LookupError}, which is a built-in exception used to indicate
88958909
that a lookup operation failed.
88968910
\index{search}
88978911
\index{pattern!search} \index{raise statement} \index{statement!raise}
@@ -8905,19 +8919,19 @@ \section{Reverse lookup}
89058919
89068920
\begin{verbatim}
89078921
>>> h = histogram('parrot')
8908-
>>> k = reverse_lookup(h, 2)
8909-
>>> k
8922+
>>> key = reverse_lookup(h, 2)
8923+
>>> key
89108924
'r'
89118925
\end{verbatim}
89128926
%
89138927
And an unsuccessful one:
89148928
89158929
\begin{verbatim}
8916-
>>> k = reverse_lookup(h, 3)
8930+
>>> key = reverse_lookup(h, 3)
89178931
Traceback (most recent call last):
89188932
File "<stdin>", line 1, in <module>
89198933
File "<stdin>", line 5, in reverse_lookup
8920-
ValueError
8934+
LookupError
89218935
\end{verbatim}
89228936
%
89238937
The effect when you raise an exception is the same as when
@@ -8930,10 +8944,10 @@ \section{Reverse lookup}
89308944
optional argument. For example:
89318945
89328946
\begin{verbatim}
8933-
>>> raise ValueError('value does not appear in the dictionary')
8947+
>>> raise LookupError('value does not appear in the dictionary')
89348948
Traceback (most recent call last):
89358949
File "<stdin>", line 1, in ?
8936-
ValueError: value does not appear in the dictionary
8950+
LookupError: value does not appear in the dictionary
89378951
\end{verbatim}
89388952
%
89398953
A reverse lookup is much slower than a forward lookup; if you
@@ -10453,7 +10467,7 @@ \section{Word frequency analysis}
1045310467
\begin{exercise}
1045410468
1045510469
Modify the program from the previous exercise to print the
10456-
20 most frequently-used words in the book.
10470+
20 most frequently used words in the book.
1045710471
1045810472
\end{exercise}
1045910473
@@ -10555,7 +10569,7 @@ \section{Random numbers}
1055510569
{'a': 2, 'b': 1}
1055610570
\end{verbatim}
1055710571
%
10558-
your function should return {\tt 'a'} with probability $2/3$ and {\tt 'b'}
10572+
your function should return \verb"'a'" with probability $2/3$ and \verb"'b'"
1055910573
with probability $1/3$.
1056010574
\end{exercise}
1056110575
@@ -10606,8 +10620,8 @@ \section{Word histogram}
1060610620
hyphens with spaces before using {\tt split} to break the line into a
1060710621
list of strings. It traverses the list of words and uses {\tt strip}
1060810622
and {\tt lower} to remove punctuation and convert to lower case. (It
10609-
is a shorthand to say that strings are ``converted;'' remember that
10610-
string are immutable, so methods like {\tt strip} and {\tt lower}
10623+
is a shorthand to say that strings are ``converted''; remember that
10624+
strings are immutable, so methods like {\tt strip} and {\tt lower}
1061110625
return new strings.)
1061210626
1061310627
Finally, \verb"process_line" updates the histogram by creating a new
@@ -11452,7 +11466,7 @@ \section{Filenames and paths}
1145211466
\index{working directory}
1145311467
\index{directory!working}
1145411468
11455-
A string like {\tt '/home/dinsdale'} that identifies a file or
11469+
A string like \verb"'/home/dinsdale'" that identifies a file or
1145611470
directory is called a {\bf path}.
1145711471
1145811472
A simple filename, like {\tt memo.txt} is also considered a path,
@@ -12029,15 +12043,15 @@ \section{Exercises}
1202912043
If you download my solution to Exercise~\ref{anagrams} from
1203012044
\url{http://thinkpython2.com/code/anagram_sets.py}, you'll see that it creates
1203112045
a dictionary that maps from a sorted string of letters to the list of
12032-
words that can be spelled with those letters. For example, {\tt
12033-
'opst'} maps to the list {\tt ['opts', 'post', 'pots', 'spot',
12034-
'stop', 'tops']}.
12046+
words that can be spelled with those letters. For example,
12047+
\verb"'opst'" maps to the list
12048+
\verb"['opts', 'post', 'pots', 'spot', 'stop', 'tops']".
1203512049
1203612050
Write a module that imports \verb"anagram_sets" and provides
1203712051
two new functions: \verb"store_anagrams" should store the
1203812052
anagram dictionary in a ``shelf''; \verb"read_anagrams" should
1203912053
look up a word and return a list of its anagrams.
12040-
Solution: \url{http://thinkpython2.com/code/anagram_db.py}
12054+
Solution: \url{http://thinkpython2.com/code/anagram_db.py}.
1204112055
1204212056
\end{exercise}
1204312057
@@ -13915,7 +13929,7 @@ \section{Exercises}
1391513929
\verb"int_to_time") to work with the new implementation. You
1391613930
should not have to modify the test code in {\tt main}. When you
1391713931
are done, the output should be the same as before. Solution:
13918-
\url{http://thinkpython2.com/code/Time2_soln.py}
13932+
\url{http://thinkpython2.com/code/Time2_soln.py}.
1391913933
1392013934
\end{exercise}
1392113935
@@ -15615,7 +15629,7 @@ \section{Gathering keyword args}
1561515629
%
1561615630
When you are working with functions that have a large number of
1561715631
parameters, it is often useful to create and pass around dictionaries
15618-
that specify frequently-used options.
15632+
that specify frequently used options.
1561915633
1562015634
1562115635
\section{Glossary}
@@ -16069,7 +16083,7 @@ \subsection{When I run the program I get an exception.}
1606916083
\end{description}
1607016084
1607116085
The Python debugger ({\tt pdb}) is useful for tracking down
16072-
Exceptions because it allows you to examine the state of the
16086+
exceptions because it allows you to examine the state of the
1607316087
program immediately before the error. You can read
1607416088
about {\tt pdb} at \url{https://docs.python.org/3/library/pdb.html}.
1607516089
\index{debugger (pdb)}

0 commit comments

Comments
 (0)