1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
{
"metadata": {
"name": "",
"signature": "sha256:c565a0de8f316cbc19d18a7bdba735b6241f6d0c0c10c2d49245aa88403df9e7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 6, The While and do...While Loops"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-1 , Page number: 86"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# A first look at the while loop\n",
"\n",
"i = 1\n",
"\n",
"while i < 11:\n",
"\tprint \"This is line number %d\"%i\n",
"\ti += 1 \t\t\t# Python doesn't support \"++\" or \"--\" shorthand expressions"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This is line number 1\n",
"This is line number 2\n",
"This is line number 3\n",
"This is line number 4\n",
"This is line number 5\n",
"This is line number 6\n",
"This is line number 7\n",
"This is line number 8\n",
"This is line number 9\n",
"This is line number 10\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-2 , Page number: 89"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Countdown to blastoff\n",
"\n",
"i = 10\n",
"\n",
"print \"Countdown\"\n",
"\n",
"while i >= 0 :\n",
"\tprint \"%d\"%i\n",
"\ti -= 1\n",
"\n",
"print \"\\n\\nBlastoff!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Countdown\n",
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"0\n",
"\n",
"\n",
"Blastoff!\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-3 , Page number: 90"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Hailstones\n",
"\n",
"counter = 0;\n",
"\n",
"print \"Enter a positive integer:\",\n",
"n = 51\n",
"print n\n",
"\n",
"nsave = n\n",
"\n",
"\n",
"while n > 1:\n",
"\tif n % 2 :\t\t\t# if (n % 2) is non-zero, n is not divisible by 2 and is therefore odd\n",
"\t\tn = n * 3 + 1\n",
"\telse :\n",
"\t\tn /= 2\n",
"\tcounter += 1\n",
"\tprint \"Step %4d: n = %4d\"%(counter,n)\n",
"\n",
"print \"\\n\\n%d went to 1 in %d steps.\"%(nsave,counter)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a positive integer: 51\n",
"Step 1: n = 154\n",
"Step 2: n = 77\n",
"Step 3: n = 232\n",
"Step 4: n = 116\n",
"Step 5: n = 58\n",
"Step 6: n = 29\n",
"Step 7: n = 88\n",
"Step 8: n = 44\n",
"Step 9: n = 22\n",
"Step 10: n = 11\n",
"Step 11: n = 34\n",
"Step 12: n = 17\n",
"Step 13: n = 52\n",
"Step 14: n = 26\n",
"Step 15: n = 13\n",
"Step 16: n = 40\n",
"Step 17: n = 20\n",
"Step 18: n = 10\n",
"Step 19: n = 5\n",
"Step 20: n = 16\n",
"Step 21: n = 8\n",
"Step 22: n = 4\n",
"Step 23: n = 2\n",
"Step 24: n = 1\n",
"\n",
"\n",
"51 went to 1 in 24 steps.\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-4 , Page number: 92"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Computing the factorial\n",
"\n",
"print \"Type in a positive integer:\",\n",
"n = 6\n",
"print n\n",
"\n",
"nsave = n\n",
"factorial = 1\n",
"\n",
"while n > 1 :\t\n",
"\tfactorial *= n\n",
"\tn -= 1\n",
"\n",
"print \"Factorial of %d = %d\"%(nsave,factorial)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Type in a positive integer: 6\n",
"Factorial of 6 = 720\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-5 , Page number: 93"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Computing the factorial (shorter version)\n",
"\n",
"print \"Type in a positive integer: \",\n",
"n = 6\n",
"print n\n",
"\n",
"nsave = n\n",
"factorial = 1\n",
"\n",
"while n > 1 :\n",
"\tfactorial *= n # Since python doesn't support '--' expression, the given program cannot be shortened\n",
"\tn -= 1\n",
"\n",
"print \"Factorial of %d = %d\"%(nsave,factorial)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Type in a positive integer: 6\n",
"Factorial of 6 = 720\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-6 , Page number: 95"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Illustration of do..while loops\n",
"\n",
"# This program reads in an integer, adds up the values of each of its individual digits, and prints out the sum.\n",
"\n",
"sum = 0\n",
"\n",
"print \"Type in an integer:\",\n",
"n = 1776\n",
"print n\n",
"\n",
"# There is no do...while loop in python\n",
"# An equivalent while loop can work\n",
"\n",
"while n > 0 :\n",
"\trightmost_digit = n % 10\t# Extract rightmost digit\n",
"\tsum += rightmost_digit\n",
"\tn /= 10\t\t\t\t\t\t# Move next digit into rightmost position\n",
"\n",
"print \"The sum of the digits is %d\"%sum\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Type in an integer: 1776\n",
"The sum of the digits is 21\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-7 , Page number: 97"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys\n",
"\n",
"# This program reads in an integer and prints it out backwards\n",
"\n",
"print \"Type in an integer: \",\n",
"n = 5746\n",
"print n\n",
"\n",
"print \"%d backwards is \"%n,\n",
"\n",
"while n > 0 :\n",
"\trightmost_digit = n % 10\n",
"\tsys.stdout.write(\"%d\"%rightmost_digit) # For printing in the same line without implicit whitespaces\n",
"\tn /= 10\n",
"print \t\t\t# For the new line\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Type in an integer: 5746\n",
"5746 backwards is 6475\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-8 , Page number: 99"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# A program to read in numbers and print out their squares\n",
"\n",
"print \"Enter an integer (negative to quit):\",\n",
"n = 5\n",
"print n\n",
"\n",
"while n > 0 :\n",
"\tprint \"%d squared is %d\"%(n,n*n)\n",
"\tprint \"\\nEnter an integer (negative to quit):\",\n",
"\tn = -1\n",
"\tprint n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter an integer (negative to quit): 5\n",
"5 squared is 25\n",
"\n",
"Enter an integer (negative to quit): -1\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-9 , Page number: 100"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# A program to read in radius values and print out the circumference and area\n",
"\n",
"pi = 3.1416\n",
"\n",
"print \"Enter radius (negative to quit):\",\n",
"radius = 4.5\n",
"print radius\n",
"\n",
"while radius >= 0.0 :\n",
"\tprint \"The circumference is %f\"%(2.0 * pi * radius)\n",
"\tprint \"The area is %f\"%(pi * radius * radius)\n",
"\tprint \"\\nEnter radius (negative to quit):\",\n",
"\tradius = -1.0\n",
"\tprint radius\n",
"\t\n",
"\t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter radius (negative to quit): 4.5\n",
"The circumference is 28.274400\n",
"The area is 63.617400\n",
"\n",
"Enter radius (negative to quit): -1.0\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6-10 , Page number: 101"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# A program to read in length and width and print out the perimeter and area\n",
"\n",
"print \"Enter length and width\"\n",
"print \" (one or both negative to quit):\",\n",
"length = 4\n",
"width = 5\n",
"print length,width\n",
"\n",
"while length > 0 and width > 0 : \n",
"\tprint \"The perimeter is %d\"%(2*(length+width))\n",
"\tprint \"The area is %d\"%(length*width)\n",
"\tprint \"\\nEnter length and width\"\n",
"\tprint \" (one or both negative to quit):\",\n",
"\tlength = -1\n",
"\twidth = 0\n",
"\tprint length,width\t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter length and width\n",
" (one or both negative to quit): 4 5\n",
"The perimeter is 18\n",
"The area is 20\n",
"\n",
"Enter length and width\n",
" (one or both negative to quit): -1 0\n"
]
}
],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
|