Friday, November 20, 2009

Chapter 6 -- Page 208 -- Line 9

There is a missing space between __main__ and namespace.

Monday, November 2, 2009

Chapter 4 --Page 140-141 -- Listing 4.7

The values method returns a dict_values object, not a list. However, it can be turned into a list by using the list() function. Line 10 of listing 4.7 should be

countlist = list(countlist.values())

Then the max function is legal on countlist (as shown on line 11).

The text on page 140 should read

Recall that the values() method returns a dict_values object which can be turned into a list by applying the list function.

Monday, September 28, 2009

All Chapters, Source Code for Listings

Here are all the listings for the book: Listings.zip

Chapter 8 Data Files

Again, several have asked for easier access to the sample data files used. Here are links to download the dictionary of words we used in the cryptanalysis chapter along with a link to the text of War of the Worlds.


War of the Worlds




wordlist.txt

chapter 7 Sample Data

Several people have requested that we post the image and sample data from chapter 7 here.

Here is the worldmap.gif

worldmap.gif


And here is the sample earthquakes.txt file:


2.8 2006/10/19 02:02:10 62.391 -149.751 15.0 CENTRAL ALASKA
2.5 2006/10/19 00:31:15 20.119 -156.213 1.5 MAUI REGION, HAWAII
5.0 2006/10/18 21:15:51 4.823 -82.592 37.3 SOUTH OF PANAMA
2.6 2006/10/18 21:12:25 59.934 -147.904 30.0 GULF OF ALASKA
3.4 2006/10/18 20:59:21 36.540 -89.640 7.7 SOUTHEASTERN MISSOURI
2.7 2006/10/18 20:11:22 61.023 -151.418 60.0 SOUTHERN ALASKA
3.1 2006/10/18 16:40:15 20.282 -156.611 4.7 MAUI REGION, HAWAII
2.7 2006/10/18 14:12:19 59.808 -152.538 50.0 SOUTHERN ALASKA
2.8 2006/10/18 14:02:12 60.686 -151.871 90.0 KENAI PENINSULA, ALASKA
4.9 2006/10/18 12:10:01 1.758 127.488 127.0 HALMAHERA, INDONESIA
6.2 2006/10/18 10:45:36 -15.081 167.243 138.5 VANUATU
2.8 2006/10/18 10:45:17 32.162 -115.895 6.3 BAJA CALIFORNIA, MEXICO
3.3 2006/10/18 10:08:45 32.165 -115.891 7.3 BAJA CALIFORNIA, MEXICO
2.8 2006/10/18 08:22:27 32.263 -115.297 3.4 BAJA CALIFORNIA, MEXICO
3.7 2006/10/18 05:34:15 62.326 -151.224 85.9 CENTRAL ALASKA
4.6 2006/10/18 03:25:03 -21.538 -66.593 201.7 POTOSI, BOLIVIA
3.7 2006/10/18 02:32:26 57.560 -137.186 1.0 OFF THE COAST OF SOUTHEASTERN ALASKA
4.9 2006/10/18 02:01:27 1.355 97.157 25.8 NIAS REGION, INDONESIA
2.5 2006/10/18 00:18:42 19.801 -155.391 10.6 ISLAND OF HAWAII, HAWAII
3.1 2006/10/17 22:59:01 61.444 -150.523 60.0 SOUTHERN ALASKA
3.1 2006/10/17 22:59:00 61.445 -150.656 50.0 SOUTHERN ALASKA
2.5 2006/10/17 20:58:26 34.145 -116.973 11.1 SOUTHERN CALIFORNIA
4.9 2006/10/17 20:53:16 40.403 -125.989 10.0 OFF THE COAST OF NORTHERN CALIFORNIA
4.5 2006/10/17 19:47:45 -20.911 -68.525 97.5 POTOSI, BOLIVIA
4.9 2006/10/17 19:19:08 37.542 142.864 8.9 OFF THE EAST COAST OF HONSHU, JAPAN
2.7 2006/10/17 18:49:24 19.852 -155.946 41.4 ISLAND OF HAWAII, HAWAII
3.5 2006/10/17 16:57:34 19.987 -156.028 43.5 HAWAII REGION, HAWAII
3.2 2006/10/17 15:32:34 32.339 -115.119 6.0 BAJA CALIFORNIA, MEXICO
3.9 2006/10/17 15:26:46 20.029 -155.957 12.6 HAWAII REGION, HAWAII
2.7 2006/10/17 15:24:31 50.899 -178.524 1.0 ANDREANOF ISLANDS, ALEUTIAN IS., ALASKA
4.9 2006/10/17 14:37:53 63.103 -150.591 100.8 CENTRAL ALASKA
2.6 2006/10/17 13:32:04 52.681 -166.483 15.0 FOX ISLANDS, ALEUTIAN ISLANDS, ALASKA
3.0 2006/10/17 13:02:32 51.100 -179.318 15.0 ANDREANOF ISLANDS, ALEUTIAN IS., ALASKA
3.2 2006/10/17 10:59:58 19.895 -155.893 35.7 ISLAND OF HAWAII, HAWAII
2.5 2006/10/17 10:41:51 40.634 -123.335 27.6 NORTHERN CALIFORNIA
5.4 2006/10/17 08:58:26 -5.854 151.145 32.8 NEW BRITAIN REGION, PAPUA NEW GUINEA
2.8 2006/10/17 08:56:30 36.110 -80.213 5.0 VIRGINIA-NORTH CAROLINA BORDER REGION
5.1 2006/10/17 07:59:18 10.305 91.787 30.0 ANDAMAN ISLANDS, INDIA REGION
3.2 2006/10/17 06:53:53 18.218 -68.190 150.8 MONA PASSAGE, DOMINICAN REPUBLIC
3.2 2006/10/17 06:20:33 20.090 -156.158 11.6 MAUI REGION, HAWAII
2.8 2006/10/17 05:18:04 35.230 -92.290 3.8 ARKANSAS
5.2 2006/10/17 04:30:51 45.226 150.022 47.0 KURIL ISLANDS
5.1 2006/10/17 04:20:18 -5.891 151.014 27.7 NEW BRITAIN REGION, PAPUA NEW GUINEA
5.7 2006/10/17 04:02:38 -20.995 -68.261 139.5 POTOSI, BOLIVIA
3.5 2006/10/17 02:27:48 18.915 -64.058 70.6 VIRGIN ISLANDS REGION
5.2 2006/10/17 01:59:14 -17.358 167.882 44.6 VANUATU
3.3 2006/10/17 01:31:52 18.996 -64.787 44.8 VIRGIN ISLANDS REGION
6.6 2006/10/17 01:25:13 -5.846 151.012 32.0 NEW BRITAIN REGION, PAPUA NEW GUINEA
2.9 2006/10/17 00:39:45 34.317 -118.456 5.0 GREATER LOS ANGELES AREA, CALIFORNIA
5.3 2006/10/16 23:23:19 48.226 153.160 139.2 KURIL ISLANDS
4.7 2006/10/16 23:03:58 -6.112 147.528 70.3 EASTERN NEW GUINEA REG, PAPUA NEW GUINEA
3.1 2006/10/16 22:34:38 59.634 -152.077 35.0 SOUTHERN ALASKA
2.9 2006/10/16 22:34:36 59.584 -152.074 20.0 SOUTHERN ALASKA
2.9 2006/10/16 20:15:55 46.172 -122.177 0.0 MOUNT ST. HELENS AREA, WASHINGTON
2.8 2006/10/16 19:03:52 19.890 -155.821 15.9 ISLAND OF HAWAII, HAWAII
4.8 2006/10/16 17:31:32 5.097 126.958 10.0 MINDANAO, PHILIPPINES
2.9 2006/10/16 14:10:54 58.812 -154.375 140.0 ALASKA PENINSULA
4.1 2006/10/16 14:10:04 33.837 70.296 80.3 PAKISTAN
2.5 2006/10/16 13:30:27 19.462 -154.897 3.1 HAWAII REGION, HAWAII
2.5 2006/10/16 11:59:39 19.771 -155.419 27.8 ISLAND OF HAWAII, HAWAII
4.7 2006/10/16 11:36:42 49.785 155.539 86.0 KURIL ISLANDS
3.7 2006/10/16 11:15:58 20.800 -156.939 7.2 MAUI REGION, HAWAII
2.5 2006/10/16 10:14:16 19.969 -156.029 16.7 HAWAII REGION, HAWAII
4.6 2006/10/16 10:11:07 1.183 -77.682 122.4 COLOMBIA
2.7 2006/10/16 08:46:23 20.105 -155.823 8.8 HAWAII REGION, HAWAII
2.7 2006/10/16 06:45:27 19.940 -156.053 9.6 HAWAII REGION, HAWAII
2.6 2006/10/16 05:40:57 32.341 -115.110 6.0 BAJA CALIFORNIA, MEXICO
2.5 2006/10/16 04:39:48 19.879 -156.058 13.2 HAWAII REGION, HAWAII
2.8 2006/10/16 04:22:35 19.922 -155.929 13.4 ISLAND OF HAWAII, HAWAII
2.5 2006/10/16 03:38:15 20.054 -155.983 10.0 HAWAII REGION, HAWAII
4.6 2006/10/16 02:51:27 41.228 141.970 51.5 HOKKAIDO, JAPAN REGION
2.6 2006/10/16 02:26:26 19.883 -156.069 15.5 HAWAII REGION, HAWAII
5.3 2006/10/16 01:33:34 -30.303 -71.472 47.1 COQUIMBO, CHILE
3.4 2006/10/16 01:11:04 19.887 -155.935 36.3 ISLAND OF HAWAII, HAWAII
5.8 2006/10/16 00:35:45 -50.900 158.830 0.0 NORTH OF MACQUARIE ISLAND
3.2 2006/10/16 00:22:01 58.829 -152.319 25.0 KODIAK ISLAND REGION, ALASKA
2.9 2006/10/15 23:58:05 62.539 -151.548 100.0 CENTRAL ALASKA
5.0 2006/10/15 23:50:13 -30.313 -177.818 35.0 KERMADEC ISLANDS, NEW ZEALAND
4.3 2006/10/15 22:08:44 -2.069 101.074 87.8 SOUTHERN SUMATRA, INDONESIA
2.7 2006/10/15 21:45:39 20.014 -155.783 10.9 HAWAII REGION, HAWAII
2.7 2006/10/15 21:30:25 20.625 -156.681 7.0 MAUI REGION, HAWAII
2.5 2006/10/15 21:08:10 19.924 -155.989 12.0 ISLAND OF HAWAII, HAWAII
4.4 2006/10/15 20:35:21 20.130 -156.025 24.9 MAUI REGION, HAWAII
2.7 2006/10/15 20:20:27 20.834 -156.643 5.4 MAUI REGION, HAWAII
2.6 2006/10/15 20:11:14 19.910 -155.920 11.7 ISLAND OF HAWAII, HAWAII
3.2 2006/10/15 19:57:45 19.852 -155.871 16.3 ISLAND OF HAWAII, HAWAII
4.8 2006/10/15 19:44:42 3.124 128.156 132.8 NORTH OF HALMAHERA, INDONESIA
3.1 2006/10/15 19:29:54 20.612 -156.248 7.0 MAUI REGION, HAWAII
2.8 2006/10/15 19:19:52 20.175 -155.962 7.8 HAWAII REGION, HAWAII
2.6 2006/10/15 19:17:38 19.844 -155.987 40.8 ISLAND OF HAWAII, HAWAII
2.5 2006/10/15 18:45:06 20.744 -156.356 7.0 MAUI REGION, HAWAII
2.5 2006/10/15 18:39:39 19.873 -155.626 0.0 ISLAND OF HAWAII, HAWAII
2.7 2006/10/15 18:34:47 19.943 -156.079 15.3 HAWAII REGION, HAWAII
2.9 2006/10/15 18:23:05 19.934 -156.020 11.1 HAWAII REGION, HAWAII
3.7 2006/10/15 18:19:58 19.936 -155.924 35.2 ISLAND OF HAWAII, HAWAII
2.6 2006/10/15 18:14:53 19.770 -155.773 30.3 ISLAND OF HAWAII, HAWAII
2.6 2006/10/15 18:10:44 19.811 -155.997 42.8 ISLAND OF HAWAII, HAWAII
2.5 2006/10/15 18:04:31 19.950 -156.020 16.4 HAWAII REGION, HAWAII
2.5 2006/10/15 17:56:27 19.855 -155.895 9.6 ISLAND OF HAWAII, HAWAII
3.1 2006/10/15 17:53:39 19.884 -155.957 9.0 ISLAND OF HAWAII, HAWAII
2.9 2006/10/15 17:50:29 20.015 -155.946 2.8 HAWAII REGION, HAWAII
3.1 2006/10/15 17:45:43 20.879 -156.894 7.0 MAUI REGION, HAWAII
2.6 2006/10/15 17:38:35 19.938 -156.030 13.4 HAWAII REGION, HAWAII
2.5 2006/10/15 17:35:57 19.918 -155.953 16.9 ISLAND OF HAWAII, HAWAII
3.1 2006/10/15 17:33:52 19.846 -156.178 37.6 HAWAII REGION, HAWAII
3.3 2006/10/15 17:30:02 20.154 -155.844 13.0 HAWAII REGION, HAWAII
3.6 2006/10/15 17:29:05 19.883 -155.896 36.6 ISLAND OF HAWAII, HAWAII
2.8 2006/10/15 17:27:35 19.868 -155.834 9.4 ISLAND OF HAWAII, HAWAII
6.0 2006/10/15 17:14:12 20.129 -155.983 18.9 HAWAII REGION, HAWAII
6.7 2006/10/15 17:07:48 19.801 -156.053 29.0 HAWAII REGION, HAWAII
2.9 2006/10/15 14:37:24 32.283 -115.122 0.0 BAJA CALIFORNIA, MEXICO
4.4 2006/10/15 13:34:35 -5.856 145.649 69.3 EASTERN NEW GUINEA REG, PAPUA NEW GUINEA
3.0 2006/10/15 12:58:03 57.476 -155.668 50.0 ALASKA PENINSULA
3.3 2006/10/15 12:27:20 19.579 -65.363 13.2 PUERTO RICO REGION
4.3 2006/10/15 09:12:54 25.268 141.101 290.1 VOLCANO ISLANDS, JAPAN REGION
4.5 2006/10/15 08:51:37 3.067 93.185 32.3 OFF THE WEST COAST OF NORTHERN SUMATRA
4.8 2006/10/15 04:48:19 -9.374 -78.851 49.8 NEAR THE COAST OF NORTHERN PERU
4.4 2006/10/15 04:43:05 -20.656 -178.768 639.1 FIJI REGION
4.0 2006/10/15 04:15:03 -22.265 -68.980 86.2 ANTOFAGASTA, CHILE
3.4 2006/10/15 03:30:11 59.578 -152.870 100.0 SOUTHERN ALASKA
4.4 2006/10/15 03:21:43 -20.098 -177.831 533.7 FIJI REGION
2.7 2006/10/15 02:46:40 59.204 -153.149 90.0 SOUTHERN ALASKA
4.0 2006/10/15 02:20:14 53.960 -164.059 1.0 UNIMAK ISLAND REGION, ALASKA
4.7 2006/10/15 01:35:11 34.958 33.813 38.5 CYPRUS REGION
3.0 2006/10/14 21:42:36 18.553 -66.207 89.7 PUERTO RICO REGION
2.8 2006/10/14 20:03:16 38.824 -122.783 0.1 NORTHERN CALIFORNIA
3.1 2006/10/14 18:01:54 38.816 -122.790 4.5 NORTHERN CALIFORNIA
4.8 2006/10/14 17:55:16 7.553 -78.118 12.0 PANAMA
3.7 2006/10/14 16:54:36 18.273 -63.883 93.2 VIRGIN ISLANDS REGION
2.7 2006/10/14 15:30:48 46.850 -121.594 5.2 WASHINGTON
5.1 2006/10/14 15:18:39 -23.017 -68.191 108.2 ANTOFAGASTA, CHILE
2.6 2006/10/14 15:15:27 37.520 -118.824 6.6 CENTRAL CALIFORNIA
2.6 2006/10/14 14:10:02 46.848 -121.587 6.3 WASHINGTON
3.3 2006/10/14 11:13:01 51.626 -166.466 1.0 SOUTH OF THE ALEUTIAN ISLANDS
3.3 2006/10/14 10:59:22 18.922 -64.475 6.1 VIRGIN ISLANDS REGION
4.8 2006/10/14 09:09:32 -31.458 -71.642 38.6 OFFSHORE COQUIMBO, CHILE
4.3 2006/10/14 07:41:08 51.400 15.960 10.0 POLAND
3.0 2006/10/14 06:49:27 18.831 -64.980 39.7 VIRGIN ISLANDS REGION
4.2 2006/10/14 06:02:12 20.756 120.272 35.0 PHILIPPINE ISLANDS REGION
4.3 2006/10/14 04:50:02 -7.808 127.500 10.0 KEPULAUAN BARAT DAYA, INDONESIA
4.6 2006/10/14 04:02:15 5.987 124.644 390.0 MINDANAO, PHILIPPINES
2.8 2006/10/14 03:55:33 58.598 -155.419 140.0 ALASKA PENINSULA
4.2 2006/10/14 02:05:46 11.376 -87.629 43.8 NEAR THE COAST OF NICARAGUA
5.3 2006/10/13 21:37:58 34.816 140.165 23.6 NEAR THE EAST COAST OF HONSHU, JAPAN
4.6 2006/10/13 21:31:00 14.371 -92.508 52.6 OFFSHORE CHIAPAS, MEXICO
4.3 2006/10/13 17:40:00 13.600 92.623 30.0 ANDAMAN ISLANDS, INDIA REGION
4.6 2006/10/13 17:31:30 -7.895 109.153 126.2 JAVA, INDONESIA
5.1 2006/10/13 16:12:55 -31.390 -71.540 31.1 COQUIMBO, CHILE
2.5 2006/10/13 16:09:58 61.119 -149.870 15.0 SOUTHERN ALASKA
4.3 2006/10/13 15:41:17 60.590 165.931 10.0 NEAR THE EAST COAST OF KORYAKIA, RUSSIA
4.4 2006/10/13 15:33:49 -6.826 106.289 137.0 JAVA, INDONESIA
4.9 2006/10/13 14:45:53 -22.847 -176.926 119.5 SOUTH OF THE FIJI ISLANDS
2.7 2006/10/13 14:39:49 46.197 -122.188 0.0 MOUNT ST. HELENS AREA, WASHINGTON
3.7 2006/10/13 14:00:01 50.200 -179.000 40.0 ANDREANOF ISLANDS, ALEUTIAN IS., ALASKA
6.3 2006/10/13 13:47:40 46.311 153.273 8.8 KURIL ISLANDS
5.1 2006/10/13 10:28:45 -18.021 -69.148 133.2 LA PAZ, BOLIVIA
2.5 2006/10/13 10:03:28 63.211 -144.896 1.0 CENTRAL ALASKA
2.9 2006/10/13 09:36:33 18.579 -64.940 6.9 VIRGIN ISLANDS REGION
2.7 2006/10/13 09:13:37 62.711 -143.669 1.0 CENTRAL ALASKA
4.1 2006/10/13 08:51:05 -21.268 -67.707 118.1 POTOSI, BOLIVIA
2.6 2006/10/13 08:47:39 63.149 -144.574 10.0 CENTRAL ALASKA
4.8 2006/10/13 08:36:47 2.364 126.752 82.9 MOLUCCA SEA
5.2 2006/10/13 05:21:00 46.560 152.999 10.0 KURIL ISLANDS
2.9 2006/10/13 05:18:06 45.274 -123.031 16.8 OREGON
4.2 2006/10/13 05:15:26 51.978 152.821 418.6 NORTHWEST OF THE KURIL ISLANDS
4.0 2006/10/13 04:12:59 13.897 144.598 153.3 GUAM REGION
4.2 2006/10/13 03:11:04 -14.482 167.028 250.0 VANUATU
2.6 2006/10/13 02:29:00 59.733 -153.436 123.8 SOUTHERN ALASKA
4.6 2006/10/13 01:56:21 -15.500 167.339 98.7 VANUATU
4.8 2006/10/13 01:51:23 20.744 119.976 10.0 PHILIPPINE ISLANDS REGION
2.9 2006/10/13 01:20:12 38.792 -122.727 0.6 NORTHERN CALIFORNIA
4.8 2006/10/13 01:18:51 37.399 142.777 31.8 OFF THE EAST COAST OF HONSHU, JAPAN
3.7 2006/10/13 01:04:56 57.211 -155.440 100.0 ALASKA PENINSULA
5.0 2006/10/13 00:41:46 42.838 79.853 10.0 EASTERN KAZAKHSTAN
4.9 2006/10/13 00:32:09 -5.278 102.547 20.4 SOUTHERN SUMATRA, INDONESIA
5.1 2006/10/13 00:08:38 -7.560 127.790 112.4 KEPULAUAN BARAT DAYA, INDONESIA
4.7 2006/10/13 00:07:23 -56.020 -121.491 10.0 SOUTHERN EAST PACIFIC RISE
3.3 2006/10/12 22:41:17 32.315 -115.159 0.0 BAJA CALIFORNIA, MEXICO
3.5 2006/10/12 19:36:35 53.543 -164.431 35.0 UNIMAK ISLAND REGION, ALASKA
6.4 2006/10/12 18:05:56 -31.256 -71.390 29.3 COQUIMBO, CHILE
4.7 2006/10/12 18:04:50 51.762 -176.839 65.1 ANDREANOF ISLANDS, ALEUTIAN IS., ALASKA
5.3 2006/10/12 17:08:20 39.811 54.730 41.2 TURKMENISTAN
5.0 2006/10/12 16:27:31 50.892 -173.057 2.4 ANDREANOF ISLANDS, ALEUTIAN IS., ALASKA
4.5 2006/10/12 15:46:35 -1.845 127.606 35.0 KEPULAUAN OBI, INDONESIA
4.1 2006/10/12 15:42:50 -26.315 27.530 10.0 SOUTH AFRICA
2.6 2006/10/12 15:23:07 62.931 -150.579 100.0 CENTRAL ALASKA
5.7 2006/10/12 14:46:31 24.095 122.628 47.3 TAIWAN REGION
3.1 2006/10/12 13:49:53 53.762 -163.963 1.0 UNIMAK ISLAND REGION, ALASKA
4.6 2006/10/12 13:31:04 34.437 26.528 38.2 CRETE, GREECE
2.5 2006/10/12 12:02:48 51.175 -176.607 5.0 ANDREANOF ISLANDS, ALEUTIAN IS., ALASKA
4.5 2006/10/12 11:57:01 -19.716 -176.048 163.8 FIJI REGION
4.4 2006/10/12 10:23:02 8.538 -103.435 10.0 NORTHERN EAST PACIFIC RISE
2.6 2006/10/12 09:26:51 18.647 -65.990 65.1 PUERTO RICO REGION
4.9 2006/10/12 05:51:14 -23.420 -175.986 38.7 TONGA REGION
4.9 2006/10/12 05:30:38 -23.575 -175.851 55.4 TONGA REGION
5.5 2006/10/12 05:30:35 4.994 95.037 30.0 NORTHERN SUMATRA, INDONESIA
4.8 2006/10/12 05:26:06 -23.436 -175.869 35.0 TONGA REGION
4.5 2006/10/12 04:51:09 8.170 -103.488 10.0 NORTHERN EAST PACIFIC RISE
4.2 2006/10/12 03:20:06 37.830 29.188 8.9 WESTERN TURKEY
4.5 2006/10/12 03:09:04 -7.227 129.391 37.1 KEPULAUAN BABAR, INDONESIA
4.7 2006/10/12 02:26:30 -23.495 -176.040 67.8 SOUTH OF THE FIJI ISLANDS

Friday, September 25, 2009

Chapter 5 Listing 5.4

Here is a corrected version of listing 5.4. See the notes in the other listings for an explanation of the new urllib and the use of decode on the end of the readline call.


import urllib.request

def countHead(url):
"""docstring for countHead"""
page = urllib.request.urlopen(url)
numHeadLines = 0
numBodyLines = 0

line = page.readline().decode('ascii')
while '' not in line:
numHeadLines = numHeadLines + 1
line = page.readline().decode('ascii')

while "" not in line:
line = page.readline().decode('ascii')

while line != "" and "" not in line:
numBodyLines = numBodyLines + 1
print(line[:-1])
line = page.readline().decode('ascii')

print("number of lines in header = ", numHeadLines)
print("number of lines in body = ", numBodyLines)

page.close()

countHead("http://knuth.luther.edu/python/test.html")

Thursday, September 24, 2009

Chapter 2 -- Page 56 -- Problems 2.13, 2.14

It is probably more correct to define the Fibonacci sequence as starting with 0 and 1 as the first two numbers. In problem 2.14, assume that N will be greater or equal to 3

Wednesday, September 23, 2009

Chapter 11 Listing 11.12

The loop in lines 20 through 27 re-uses the newfish variable rather than a more correctly named newbear. the function still works properly but the variable name is confusing. Here is a corrected version of lines 20 -- 27.


for i in rnage(numberOfBears):
newbear = Bear()
x = random.randrange(myworld.getMaxX())
y = random.randrange(myworld.getMaxY())
while not myworld.emptyLocation(x,y):
x = random.randrange(myworld.getMaxX())
y = random.randrange(myworld.getMaxY())
myworld.addThing(newbear,x,y)

Chapter 10 Listing 10.9

The print on line 20 is missing parenthesis around the argument. The line should read:


print(aplanet)

Chapter 10 Exercise 10.17

The __getslice__ method does not exist anymore. The same functionality can be implemented bye writing your own __getitem__ method.

Chapter 9 Exercise 9.34

The rule F ->

means that F can be replaced by nothing.

Chapter 9 Listing 9.7

when setposition is called to restore the turtle's position and heading to a previously stored location, the tail should be up. so Lines 16 and following should read:


elif cmd == ']':
pos,head = stateSaver.pop()
aTurtle.up()
aTurtle.setposition(pos)
aTurtle.setheading(head)
aTurtle.down()

Chapter 5 page 174

On Line 9 the sentence should read

Without this call to readline, line would have no value. The book incorrectly states that read would have no value.

Chapter 3 Session 3.4

The last expression of Session 3.4 should be name[len(name)-1] not name[len(name)]. name[len(name)] generates a range error because the length of the string is 1 past the end of the string.

Friday, September 18, 2009

Chapter 2 -- Page 81 -- Problems 2.2,2.3,2.4

The problems say to use a parameter called n. It probably makes more sense to use r or perhaps radius.

Chapter 2 -- Page 81 -- Problem 2.5

The equation given in this problem is missing constants. It should read:

16 arctan(1/5) - 4 arctan(1/239)

Monday, September 7, 2009

Hanging Turtles

If you are using idle, some of the initial interactive examples will appear to hang. That is, if the turtle window gets behind another window its impossible to bring it to the front, and/or the window does not get refreshed. This is due to some weird threading interactions between the IDLE GUI and the shell. The problems are frustrating when you run into them in the first chapter. However as you write programs longer than 4 or 5 lines that include the exitOnClick call, they disappear quickly. A couple of suggestions to avoid this initial frustration:

  • Run these first examples from the command line shell. e.g. just type python3.1

  • Run idle using -n command line option. Windows and OS X can also be configured to start idle with -n

  • Carefully setup your idle shell and the turtle window so they are side by side with no other windows to distract.

Wednesday, August 26, 2009

Chapter 2 -- Page 82 -- Problem 2.9

This problem may use a notation that is hard to follow. Basically what this approximation method does is use a previous "guess" to compute a "new guess" for the square root of a number called n. The previous is the kth and the next is the k+1st. We assume that the first guess (called the zeroith) is 1.

So, to compute the "next guess", we take 1/2 times the sum of the "previous guess" and n divided by the "previous guess". That new guess becomes the old guess on the next try. The number of tries is referred to as the number of terms.

Chapter 2 -- Page 59 -- last line

Again, these initializations can simply be 1 and 2 since the division performed will automatically return floating point.

The correction should be made to lines 2 and 3 of Listing 2.4 as well.

Chapter 2 -- Page 54 -- Problem 2.6

In this problem we are testing the archimedes function to see how many sides are required to get the same result as that provided by the math library (math.pi).

Instead of "How many repetitions..." the question should ask "How many sides..."

Also, the values may never be equal. When are they "close enough" to call them equal?

Chapter 2 -- Page 47 -- Second line and Session 2.1

This line of text refers to floating point division and states that it is necessary for one of the operands to be a floating point number. This is NOT necessary. Division ("single slash") in Python 3 is always assumed to be floating point. So 22/7 will yield the correct result.

22.0, 355.0, and 9801.0 can simply be 22, 355, and 9801 in Session 2.1

Chapter 1 -- Page 13 -- Problem 1.6

This problem needs better wording, something like

Compute the number of handshakes required for each person in your class to shake hands with every other person exactly one time.

Monday, August 24, 2009

Chapter 12 -- Listing 12.8

There is a typo in Listing 12.8. In the init method the instance variable is visibleObjects. The b is missing in the text. In addition, the calls to up() and tracer should be added to the drawAll() method as they are in listing 12.4

Here is a complete, corrected version of Listing 12.8


class Canvas:

def __init__(self,w,h):
self.width = w
self.height = h
self.visibleObjects = []
self.turtle = cTurtle.Turtle()
self.turtle.setup(width=self.width,height=self.height)
self.turtle.hideturtle()

def drawAll(self):
self.turtle.reset()
self.turtle.tracer(0)
for shape in self.visibleObjects:
shape._draw(self.turtle)
self.turtle.tracer(1)
self.turtle.hideturtle()

def addShape(self,shape):
self.visibleObjects.append(shape)

def draw(self,gObject):
gObject.setCanvas(self)
gObject.setVisible(True)
self.turtle.up()
self.turtle.tracer(0)
gObject._draw(self.turtle)
self.turtle.tracer(1)
self.addShape(gObject)


Wednesday, May 20, 2009

Chapter 8 -- Exercise 8.11

Question 8.11 asks you to provide the sort function a cmp function rather than a keyword function. This is old school. In Python 3.0 cmp was removed.

Friday, May 8, 2009

Listing 12.1

Lines 4, 6, and 16 incorrectly call a method named fillColor. the name of the method should be setFill. For example line 4 should read house.setFill('blue')

Thursday, April 23, 2009

Exercises 10.19, 20, 21

In Python 3.0 the __cmp__ special method was removed. In favor of keeping __lt__, __le__, __gt__, __ge__, __eq__, and __neq__. In the 'good old days' if you wrote __cmp__ that would suffice for any of the comparison operators. Now you must explicitly write a method for each comparison operator you want to write. If it seems weird and confusing you are right, in fact it took until Python 3.0.1 for the core python developers to truly rid the language of cmp.

You can implement any or all of these special methods for exercises 10.19, 10.20, and 10.21.

Wednesday, March 18, 2009

Chapter 5 -- Page 162 -- Table 5.3

The fifth example in Table 5.3 should read %20.2f to be consistent with the description.

Chapter 5 -- Listing 5.6

Now that we understand how to decode bytes into strings we only need to make one small change to Listing 5.6


def stockCorrelate(ticker1, ticker2):
url1 = urllib.request.urlopen('http://ichart.finance.yahoo.com/table.csv?s=%s'%ticker1)
url2 = urllib.request.urlopen('http://ichart.finance.yahoo.com/table.csv?s=%s'%ticker2)
t1Data = url1.readlines()
t2Data = url2.readlines()
t1Data = [line.decode('ascii').split(',') for line in t1Data[1:] ]
t2Data = [line.decode('ascii').split(',') for line in t2Data[1:] ]
t1Close = []
t2Close = []
for i in range(min(len(t1Data), len(t2Data))):
if t1Data[i][0] == t2Data[i][0]:
t1Close.append(float(t1Data[i][4]))
t2Close.append(float(t2Data[i][4]))

print(len(t1Close), len(t2Close))
return correlation(t1Close, t2Close)

Chapter 5 -- Session 5.9

In this post we get into a few more of the difficulties introduced by the new urllib. But first here is a little background. In Python 3.0 strings are encoded using an encoding mechanism called unicode. Unicode is capable of encoding the characters in many languages, even languages with thousands of symbols in their written language. Prior to 3.0 Python encoded strings using ASCII, the American Standard Code for Information Interchange.

The difference between ASCII and Unicode involves the number of bits that are required to store a symbol in memory. We're not going to get into the details here. If you are interested you can google it and spend a day reading up on Unicode. Suffice to say that unicode may take up to 4 bytes to store a single symbol whereas ASCII always uses just a single byte. Now when we read data over the internet we get a result that is stored as a list of bytes. If the characters coming from the website are encoded using ASCII everything looks normal. In our examples you can see that the website must be stored as ascii because everything looks normal.

In fact we can tell Python to decode the bytes into a string by using the decode method. The decode method takes a single parameter that specifies the method used to encode the bytes in the first place. So to return to our original test page.


>>> page = urllib.request.urlopen('http://www.cs.luther.edu/python/test.html')
>>> pageBytes = page.read()
>>> pageText = pageBytes.decode('ascii')
>>> pageText
'\n\n\n\t\n\tTest Page\n\t\n\t\n\t\n\n\n

Hello Python Programmer!

\n

This is a test page for the urllib2 module program

\n\n\n'
>>>

Now you can see that we have decoded the data from the website into a string that is convenient for us to work with.

Now the question is how do we know what encoding is used for a particular website? We can tell by looking, but Python can also tell us. Each object that is created by urlopen has an attribute called headers. The headers attribute contains lots of data about the data returned by the web server. One of the things stored in the headers is how the data was encoded. The example below shows us that the test webpage was encoded as 'iso-8859-1'. ISO-8859 is an extension to ASCII that includes characters for the standard western languages. it is fully compatible with ASCII and each character is stored as a single byte. So, we could decode the page using decode('iso-8859-1') as well.


>>> page.headers.get_content_charset()
'iso-8859-1'


To fully automate the decoding process we can simply do:

encoding = page.headers.get_content_charset()
pageText = pageBytes.decode(encoding)


Finally to return to Session 5.9 we can use the decode method on each bytes object in our list of lines.


>>> url1 = urllib.request.urlopen('http://ichart.finance.yahoo.com/table.csv?s=AAPL')
>>> t1Data = url1.readlines()
>>> t1Data[0].decode('ascii').split(',')
['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close\n']
>>> t1Data = [line.decode('ascii').split(',') for line in t1Data[1:]]
>>> t1Data[:3]
[['2009-03-17', '95.24', '99.69', '95.07', '99.66',
'28094500', '99.66\n'], ['2009-03-16', '96.53', '97.39',
'94.18', '95.42', '28473000', '95.42\n'], ['2009-03-13',
'96.30', '97.20', '95.01', '95.93', '21470300', '95.93\n']]



Chapter 5 -- Session 5.8

For completeness, Session 5.8 should look like this:


>>> u = urllib.request.urlopen('http://ichart.finance.yahoo.com/table.csv?s=TGT')
>>> u.readlines()[:10]
[b'Date,Open,High,Low,Close,Volume,Adj Close\n',
b'2009-03-17,29.44,30.45,29.13,30.45,10204800,30.45\n',
b'2009-03-16,30.30,30.44,28.75,28.83,11136300,28.83\n',
b'2009-03-13,28.69,30.01,28.18,29.97,16870100,29.97\n',
b'2009-03-12,26.89,28.69,26.60,28.49,12663900,28.49\n',
b'2009-03-11,27.25,27.85,26.76,26.90,15474000,26.90\n',
b'2009-03-10,25.75,27.66,25.46,27.21,13459800,27.21\n',
b'2009-03-09,25.40,26.35,25.13,25.37,11816500,25.37\n',
b'2009-03-06,26.63,26.79,25.00,25.65,12434600,25.65\n',
b'2009-03-05,26.88,27.82,26.04,26.31,13311700,26.31\n']

Chapter 5 -- Session 5.7

One big change in Python 3.0 that we did not notice until after the book had gone to press was a change to the module for reading from the Internet. The urllib module was changed substantially.

As you see from the session below the urlopen function is no longer a part of urllib, it is now a part of urllib.request.


>>> import urllib
>>> page = urllib.request.urlopen('http://www.cs.luther.edu/python/test.html')
Traceback (most recent call last):
File "", line 1, in
page = urllib.request.urlopen('http://www.cs.luther.edu/python/test.html')
AttributeError: 'module' object has no attribute 'request'
>>> import urllib.request
>>> page = urllib.request.urlopen('http://www.cs.luther.edu/python/test.html')
>>> pageText = page.read()
>>> pageText
b'\n\n\n\t\n\tTest Page\n\t\n\t\n\t\n\n\n

Hello Python Programmer!

\n

This is a test page for the urllib2 module program

\n\n\n'
>>> type(pageText)




If simply moving the urlopen function to urllib.request was the only change that would not have been too bad. The more difficult change is the very subtle addition of the b before the quotes in the pageText string. In fact you can see that the variable pageText refers to something that is called bytes.

The good news is that bytes objects act very similarly to strings. The bad news is that you cannot simply mix and match strings with bytes.

The session below illustrates the difficulty:


>>> 'foo' + b'bar'
Traceback (most recent call last):
File "", line 1, in
'foo' + b'bar'
TypeError: Can't convert 'bytes' object to str implicitly


We will work through these differences in subsequent posts about the rest of chapter 5.

Tuesday, March 17, 2009

Chapter 4 -- Page 151 -- Problem 4.46

We felt that problem 4.46 deserved a bit more detail so we added it as programming exercise 4.2. Unfortunately, we forgot to delete 4.46.  To understand more about regression lines, refer to programming exercise 4.2.

Tuesday, March 3, 2009

Chapter 3 -- Listing 3.12 Vignere cipher

Line 5 of Listing 3.12 contains the statement charNum = 0. The variable charNum is not used anywhere in the encryptVignere function and the line is not needed.

Line 2 is also extraneous. It contains the template docstring for the encryptVignere function.

Friday, February 20, 2009

Chapter 2 -- Page 52 -- Listing 2.2

This code, as it evolved in Section 2.4.1, is not wrong but it does have an unfortunate use of identifier names.  The name sideS is meant to refer to the length of side S in the triangle shown. However, the parameter, sides, refers to the number of sides in the polygon being used for the approximation.  Since Python is case sensitive, these are two different names.  But they can easily be confused as seen in Line 12.  It would certainly have been better to name the parameter something like numSides.

Thursday, February 12, 2009

Chapter 1 Programming Exercise 1.3

This exercise asks the student to generalize the previous exercise from a five pointed star to an n pointed star. The instructions should include the limitation that n will be odd.

Wednesday, February 11, 2009

Chapter 1 Session 1.5 caption

The caption for session 1.5 should read Calculating the volume of a cylinder with assignment statements. The caption in the book incorrectly says sphere.

Chapter 1: Sum of first n integers

The formula on page 8 is a little confusing for our mathematician friends. To be perfectly correct the formula should read:

0349BB8B-EDCB-49E5-8FF3-13CE7E447732.jpg

Tuesday, February 10, 2009

Chapter 9 -- Page 314 -- Listing 9.4

The recursive calls to sierpinski in lines 14,15, and 16 have an extra comma at the end of the parameter list as shown below.
sierpinski(myTurtle,p1,midPoint(p1,p2),midPoint(p1,p3),depth-1,)
sierpinski(myTurtle,p2,midPoint(p2,p3),midPoint(p2,p1),depth-1,)
sierpinski(myTurtle,p3,midPoint(p3,p1),midPoint(p3,p2),depth-1,)
This is a typo.  Although these extra commas do not cause any syntactic problem, they should be removed.

Friday, February 6, 2009

Chapter 7 -- Page 258 -- Listing 7.12

Line 15 refers to a variable k that is undefined in this function.  k has been used to denote the number of clusters.  Lines 3 and 4 tell us that we are creating 6 clusters of earthquakes.  k should be 6 in line 15.

Wednesday, February 4, 2009

Chapter 4 -- Page 128 -- Problem 4.6d

This problem asks you to implement a function that works like the list method find. Of course, there is no method find...it should be index. The problem should read...  Implement a function that works like the list method index.

Chapter 6 -- Page 222 -- Exercise 6.23 typo

There is a typo in exercise 6.23.  It should read  "Write a general function for enlarging..."

Chapter 6 -- Page 220 -- Listing 6.9

This listing has transposed the width and height in lines 7 and 8.  The row should iterate over the height and the column over the width.  The correct version is:
   
def double(oldimage):
oldw = oldimage.getWidth()
oldh = oldimage.getHeight()

newim = EmptyImage(oldw*2,oldh*2)

for row in range(newim.getHeight()):
for col in range(newim.getWidth()):

originalCol = col//2
originalRow = row//2
oldpixel = oldimage.getPixel(originalCol,originalRow)

newim.setPixel(col,row,oldpixel)

return newim

Thursday, January 29, 2009

Chapter 4 -- Page 128 -- Problem 4.2g

In Python3.0, the sort method will not work across different data types. Performing sort on the list in this problem will cause an error.

>>> alist
[7, 9, 'a', 'cat', False]
>>> alist.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()

Chapter 2 -- Page 78 -- Listing 2.6

The variable numdarts in line 5 and line 23 should be numDarts (camelcase).

Chapter 2 -- Page 75 -- Problem 2.38

This problem asks you to create a boolean function called inCircle that returns True if the point is in the circle, False otherwise. Unfortunately, there is a local variable in the montePi function already called inCircle.  For clarity, use a different name for the boolean function...perhaps isInCircle or pointInCircle.

Wednesday, January 28, 2009

Chapter 13 -- Etch

Well here it is, the first bug report I've received from a reader. Thanks to Gregor Lingl (The same guy who wrote the excellent xturtle module that we based cTurtle on).

Listing 13.4 is wrong. Listing 13.4 provides a class that inherits from Turtle and provides simple onKey callbacks for turning left, right, forward and backward. These callbacks should turn a default amount or go back or forward a default number of spaces.

To avoid a crazy recursion the callbacks were named left5 and right5 in the working version of the code, but somehow the 5 got left off in the published listing.

Here's a complete, tested and working version of listing 13.4


import sys
import os
from cTurtle import Turtle, mainloop

class Etch(Turtle):
defaultDist = 5
def __init__(self):
super(Etch, self).__init__()
self.color('blue')
self.pensize(2)
self.speed(0)
self.distance = 5
self.turn = 5
self.onKey(self.forward,"Up")
self.onKey(self.bkwd,"Down")
self.onKey(self.left5,"Left")
self.onKey(self.right5,"Right")
self.onKey(self.quit,"q")
self.listen()
self.main()

def forward(self,distance=defaultDist):
super(Etch,self).forward(distance)

def bkwd(self):
self.backward(self.distance)

def left5(self):
self.left(self.turn)

def right5(self):
self.right(self.turn)

def quit(self):
sys.exit()

def main(self):
mainloop()

if __name__ == '__main__':
etch = Etch()