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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
|
{
"metadata": {
"name": "",
"signature": "sha256:970f28e2206ea4472dd82a3808592db202377fcd9a01fc69b666e61a221db5b1"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 10, Arrays"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-1, Page Number : 227"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Introduction to arrays\n",
"\n",
"array = []\t\t# defining array which is rather called list in python\n",
"\n",
"print \"You are to enter 5 integers.\\n\"\n",
"\n",
"# Input the 5 integers into array \"array\"\n",
"\n",
"for i in range(5) : # equivalent to for i in range(0,5,1)\n",
"\tprint \"Enter integer #%d: \"%(i+1),\n",
"\tval = int(raw_input())\n",
"\tarray.insert(i,val)\t\t\t\t# insert function is used to add elements to list : \n",
"\t\t\t\t\t\t\t\t\t# insert(i,val) where i is the index and val is the value to be inserted\n",
"\n",
"print \"\\nThank you.\\n\"\n",
"\n",
"# Print out the integers stored in array \"array\"\n",
"j = 1\t# used as a counter\n",
"for i in array:\t\t\t\t# shorthand way of scanning through the list; using \"in\"\n",
"\tprint \"Integer #%d = %d.\"%(j,i)\n",
"\tj += 1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"You are to enter 5 integers.\n",
"\n",
"Enter integer #1:"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Enter integer #2:"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"12\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Enter integer #3:"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"8\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Enter integer #4:"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"63\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Enter integer #5:"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" \n",
"Thank you.\n",
"\n",
"Integer #1 = 3.\n",
"Integer #2 = 12.\n",
"Integer #3 = 8.\n",
"Integer #4 = 63.\n",
"Integer #5 = 0.\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-2, Page Number : 228"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Initializing a global array\n",
"\n",
"array = [ 4, 6, 5, 7, 2 ]\n",
"\n",
"def main() :\t\t\t# just a user-defined function ; not equivalent to the built-in main() function in C\n",
"\t\"\"\" prints the integers stored in array \"array\" \"\"\"\n",
"\tj = 1\t\t# counter \n",
"\tfor i in array :\n",
"\t\tprint \"Integer #%d = %d.\"%(j,i)\n",
"\t\tj += 1\n",
"\n",
"main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Integer #1 = 4.\n",
"Integer #2 = 6.\n",
"Integer #3 = 5.\n",
"Integer #4 = 7.\n",
"Integer #5 = 2.\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-3, Page Number: 230"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The number of array elements actually used may vary\n",
"\n",
"# Read in the size of the array\n",
"print \"How many integers will you enter?\",\n",
"array_size = 6\n",
"print array_size\n",
"\n",
"# Read in the integers\n",
"print \"\\nPlease enter %d integers:\"%array_size\n",
"array = [ 12, 3, -5, 21, 0, 899 ]\t# Lists dynamically allocate memory \n",
"for i in range(array_size) :\n",
"\tprint array[i],\n",
"# Print out the integers\n",
"\n",
"print(\"\\n\")\n",
"for i in range(array_size) :\n",
"\tprint \"Integer #%d = %d.\"%(i+1,array[i])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many integers will you enter? 6\n",
"\n",
"Please enter 6 integers:\n",
"12 3 -5 21 0 899 \n",
"\n",
"Integer #1 = 12.\n",
"Integer #2 = 3.\n",
"Integer #3 = -5.\n",
"Integer #4 = 21.\n",
"Integer #5 = 0.\n",
"Integer #6 = 899.\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-4, Page Number: 231"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The trailer method\n",
"\n",
"# Macro definition\n",
"MAX_ARRAY_SIZE = 20\n",
"TRAILER = -9999\n",
"\n",
"# Read in the integers\n",
"\n",
"print \"Please enter up to %d integers, using %d as a trailer:\"%(MAX_ARRAY_SIZE,TRAILER)\n",
"array = [ 3, 75, -34, 6, -1, 2, 41, 0, 90, -9999]\n",
"for array_size in range(MAX_ARRAY_SIZE) :\n",
"\tprint array[array_size],\n",
"\tif array[array_size] == -9999 :\n",
"\t\tbreak\t\n",
"\t\n",
"# Print out the integers\n",
"print \"\\n\\nYou have entered %d integers\"%array_size\n",
"for i in range(array_size) :\n",
"\tprint \"Integer #%d = %d.\"%(i+1,array[i])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter up to 20 integers, using -9999 as a trailer:\n",
"3 75 -34 6 -1 2 41 0 90 -9999 \n",
"\n",
"You have entered 9 integers\n",
"Integer #1 = 3.\n",
"Integer #2 = 75.\n",
"Integer #3 = -34.\n",
"Integer #4 = 6.\n",
"Integer #5 = -1.\n",
"Integer #6 = 2.\n",
"Integer #7 = 41.\n",
"Integer #8 = 0.\n",
"Integer #9 = 90.\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-5, Page Number: 233"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Caculating the average grade for a class\n",
"\n",
"# Macro definition\n",
"\n",
"MAX_GRADES = 20 \t# The class may have up to 20 students\n",
"TRAILER = -9999\t\t# -9999 marks the end of the grade list\n",
"\n",
"# Read in the grades\n",
"\n",
"print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n",
"grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n",
"for num_grades in range(MAX_GRADES) :\n",
"\tprint grades[num_grades]\n",
"\tif grades[num_grades] == -9999 :\n",
"\t\tbreak\n",
"\n",
"print \"\\nYou have entered %d grades.\"%num_grades\n",
"\n",
"# Find the average\n",
"sum = 0.0 \n",
"for i in range(num_grades) :\n",
"\tsum += grades[i]\n",
"\n",
"print \"\\nThe average grade is %f.\"%(sum/num_grades)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter up to 20 grades, using -9999 as a trailer:\n",
"41\n",
"92\n",
"15\n",
"65\n",
"0\n",
"78\n",
"81\n",
"96\n",
"-9999\n",
"\n",
"You have entered 8 grades.\n",
"\n",
"The average grade is 58.500000.\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-6, Page Number: 235"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Determining which grades in a class are above the average\n",
"\n",
"# Macro definition\n",
"\n",
"MAX_GRADES = 20 \t# The class may have up to 20 students\n",
"TRAILER = -9999\t\t# -9999 marks the end of the grade list\n",
"\n",
"# Read in the grades\n",
"\n",
"print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n",
"grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n",
"for num_grades in range(MAX_GRADES) :\n",
"\tprint grades[num_grades]\n",
"\tif grades[num_grades] == -9999 :\n",
"\t\tbreak\n",
"\n",
"print \"\\nYou have entered %d grades.\"%num_grades\n",
"\n",
"# Find the average\n",
"sum = 0.0 \n",
"for i in range(num_grades) :\n",
"\tsum += grades[i]\n",
"\n",
"avg_grade = sum/num_grades\n",
"print \"\\nThe average grade is %f.\"%avg_grade\n",
"\n",
"# Find who got above average\n",
"\n",
"print \"\\nThe following grades were above average:\"\n",
"for i in range(num_grades) :\n",
"\tif grades[i] > avg_grade :\n",
"\t\tprint \"%f (student #%d)\"%(grades[i],i+1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter up to 20 grades, using -9999 as a trailer:\n",
"41\n",
"92\n",
"15\n",
"65\n",
"0\n",
"78\n",
"81\n",
"96\n",
"-9999\n",
"\n",
"You have entered 8 grades.\n",
"\n",
"The average grade is 58.500000.\n",
"\n",
"The following grades were above average:\n",
"92.000000 (student #2)\n",
"65.000000 (student #4)\n",
"78.000000 (student #6)\n",
"81.000000 (student #7)\n",
"96.000000 (student #8)\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-7, Page Number 237"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Reversing an array\n",
"\n",
"# Macro definition\n",
"ARRAY_SIZE = 10 \t# The array has 10 elements \n",
"\n",
"# Read in the array\n",
"\n",
"print \"Please enter %d integers:\"%ARRAY_SIZE\n",
"array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]\n",
"for i in range(10) :\n",
"\tprint array[i],\n",
"\n",
"# Echo the array\n",
"\n",
"print \"\\n\\nThe original array was:\"\n",
"for i in range(10) :\n",
"\tprint array[i],\n",
"\n",
"# Print the array in reverse\n",
"\n",
"print \"\\n\\nThe reversed array is:\"\n",
"for i in range(ARRAY_SIZE-1,-1,-1) :\n",
"\tprint array[i],"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter 10 integers:\n",
"10 42 7 0 923 13 -5 6 19 3 \n",
"\n",
"The original array was:\n",
"10 42 7 0 923 13 -5 6 19 3 \n",
"\n",
"The reversed array is:\n",
"3 19 6 -5 13 923 0 7 42 10\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-8, Page Number: 240"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys\n",
"# Reversing a character string\n",
"\n",
"# Macro definition\n",
"MAX_STRING_SIZE = 10\n",
"\n",
"# Read in the word\n",
"\n",
"print \"Please enter a word of up to %d letters:\"%MAX_STRING_SIZE\n",
"word = \"bingo\"\n",
"print word\n",
"\n",
"# Echo the word \n",
"\n",
"print \"\\nThe word is %s.\"%word\n",
"\n",
"# Find thw word length\n",
"string_size = 0\n",
"for i in word :\t\t\t# To iterate over the entire string\n",
"\tstring_size += 1\n",
"\n",
"# Print the word in reverse\n",
"\n",
"print \"\\nThe reversed word is \",\n",
"for i in range(string_size-1,-1,-1) :\n",
"\tsys.stdout.write(\"%c\"%word[i]) # For printing in the same line without implicit whitespaces\n",
"\n",
"print "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter a word of up to 10 letters:\n",
"bingo\n",
"\n",
"The word is bingo.\n",
"\n",
"The reversed word is ognib\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-9, Page Number: 242"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Calculating the minimum and maximum grade in a class\n",
"\n",
"# Macro definition\n",
"\n",
"MAX_GRADES = 20 \t# The class may have up to 20 students\n",
"TRAILER = -9999\t\t# -9999 marks the end of the grade list\n",
"\n",
"# Read in the grades\n",
"\n",
"print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n",
"grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n",
"for num_grades in range(MAX_GRADES) :\n",
"\tprint grades[num_grades]\n",
"\tif grades[num_grades] == -9999 :\n",
"\t\tbreak\n",
"\n",
"print \"\\nYou have entered %d grades.\"%num_grades\n",
"\n",
"# Find the minimum grade\n",
"\n",
"min = grades[0]\n",
"for i in range(num_grades) :\n",
"\tif grades[i] < min :\n",
"\t\tmin = grades[i]\n",
"\n",
"print \"\\nThe lowest grade is %f\"%min\n",
"\n",
"# Find the maximum grade\n",
"\n",
"max = grades[0]\n",
"for i in range(num_grades) :\n",
"\tif grades[i] > min :\n",
"\t\tmax = grades[i]\n",
"\n",
"print \"\\nThe lowest grade is %f\"%max\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter up to 20 grades, using -9999 as a trailer:\n",
"41\n",
"92\n",
"15\n",
"65\n",
"0\n",
"78\n",
"81\n",
"96\n",
"-9999\n",
"\n",
"You have entered 8 grades.\n",
"\n",
"The lowest grade is 0.000000\n",
"\n",
"The lowest grade is 96.000000\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-10, Page Number: 243"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Sorting an array\n",
"\n",
"# Macro definition\n",
"MAX_ARRAY_SIZE = 20 \t# The array may have up to 20 elements\n",
"\n",
"array_size = 10\n",
"print \"How many numbers?\",\n",
"print array_size\n",
"\n",
"while array_size < 1 or array_size > MAX_ARRAY_SIZE\t:\n",
"\tprint \"How many numbers?\",\n",
"\tprint array_size\n",
"\n",
"# Read in the array\n",
"\n",
"print \"Please enter the integers:\"\n",
"array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]\n",
"for i in range(array_size) :\n",
"\tprint array[i],\n",
"\n",
"# Echo the array\n",
"\n",
"print \"\\n\\nThe original array is:\"\n",
"for i in range(array_size) :\n",
"\tprint array[i],\n",
"\n",
"# Sort the array\n",
"\n",
"for i in range(array_size-1) :\n",
"\tfor j in range(i+1, array_size) :\n",
"\t\tif array[i] > array[j] :\n",
"\t\t\ttemp = array[i]\n",
"\t\t\tarray[i] = array[j]\n",
"\t\t\tarray[j] = temp\n",
"\n",
"# Printing the sorted array\n",
"\n",
"print \"\\n\\nThe sorted array is\"\n",
"for i in range(array_size) :\n",
"\tprint array[i],"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many numbers? 10\n",
"Please enter the integers:\n",
"10 42 7 0 923 13 -5 6 19 3 \n",
"\n",
"The original array is:\n",
"10 42 7 0 923 13 -5 6 19 3 \n",
"\n",
"The sorted array is\n",
"-5 0 3 6 7 10 13 19 42 923\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-11, Page Number: 247"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Finding the median of an array\n",
"\n",
"# Macro definition\n",
"MAX_ARRAY_SIZE = 20 \t# The array may have up to 20 elements\n",
"\n",
"array_size = 0\n",
"array = []\n",
"\n",
"def get_size()\t:\n",
"\t\"\"\" reads the size of the array (until a valid value is entered) \"\"\"\n",
"\tglobal array_size\n",
"\tarray_size = 10\n",
"\tprint \"How many numbers?\",\n",
"\tprint array_size\n",
"\twhile array_size < 1 or array_size > MAX_ARRAY_SIZE\t:\n",
"\t\tprint \"How many numbers?\",\n",
"\t\tprint array_size\n",
"\n",
"\n",
"def read_array() :\n",
"\t\"\"\" read in the array \"\"\"\n",
"\tglobal array\n",
"\tprint \"Please enter the integers:\"\n",
"\tarray = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3 ]\n",
"\tfor i in range(array_size) :\n",
"\t\tprint array[i],\n",
"\n",
"def write_array() :\n",
"\t\"\"\" write out the array \"\"\"\n",
"\tfor i in range(array_size) :\n",
"\t\tprint array[i],\n",
"\n",
"def sort_array() :\n",
"\t\"\"\" sort the array \"\"\"\n",
"\tglobal array\n",
"\tfor i in range(array_size-1) :\n",
"\t\tfor j in range(i+1, array_size) :\n",
"\t\t\tif array[i] > array[j] :\n",
"\t\t\t\ttemp = array[i]\n",
"\t\t\t\tarray[i] = array[j]\n",
"\t\t\t\tarray[j] = temp\n",
"\n",
"def even(n) :\n",
"\t\"\"\" returns non-zero(true) if n is even else returns zero(false) \"\"\"\n",
"\treturn ((n/2)*2 == n)\n",
"\n",
"get_size()\t\t# Read in the size of the array\n",
"\n",
"read_array()\t# Read in the array\n",
"\n",
"print \"\\n\\nThe original array is:\"\n",
"write_array()\t# Echo the array\n",
"\n",
"sort_array()\t# Sort the array\n",
"\n",
"print \"\\n\\nThe sorted array is:\"\n",
"write_array()\t# Print the sorted array\n",
"\n",
"print \"\\n\\nThe median is:\"\n",
"# The equivalent of tertiary conditional operator in python is a if test else b\n",
"print \"%.1f\"%( ((array[ array_size / 2 -1] + array[ array_size / 2 ]) / 2.0 ) if even(array_size) else float(array[ array_size / 2 ]) ) "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many numbers? 10\n",
"Please enter the integers:\n",
"10 42 7 0 923 13 -5 6 19 3 \n",
"\n",
"The original array is:\n",
"10 42 7 0 923 13 -5 6 19 3 \n",
"\n",
"The sorted array is:\n",
"-5 0 3 6 7 10 13 19 42 923 \n",
"\n",
"The median is:\n",
"8.5\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-12, Page Number: 251"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Tally how many of each number within a range are entered\n",
"\n",
"# Macro definition\n",
"RANGE_LOW = 1\n",
"RANGE_HIGH = 10\t\t\t# Tallies numbers between RANGE_LOW and RANGE_HIGH\n",
"TRAILER = -1 \t\t\t# TRAILER marks end of data\n",
"\n",
"inputfeed = [ 3, 5, 1, 10, 8, 5, 10, 6, 2, 11, 4, 9, -2, 3, 2, 9, -1 ]\t\t# additional array for the sake of providing input\n",
"\n",
"tally = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\t\t# empty array\n",
"# Input numbers\n",
"print \"Please enter a number between %d and %d (%d to quit): \"%(RANGE_LOW,RANGE_HIGH,TRAILER),\n",
"num = inputfeed[0]\n",
"print num\n",
"\n",
"i = 1\n",
"while num != TRAILER :\n",
"\tif num < RANGE_LOW or num > RANGE_HIGH : \t\t# Out of range\n",
"\t\tprint \"%d is not between %d and %d.\"%(num,RANGE_LOW,RANGE_HIGH)\n",
"\telse :\n",
"\t\ttally[ num - RANGE_LOW] += 1\t\n",
"\tprint \"\\nEnter another number: \",\n",
"\tnum = inputfeed[i]\n",
"\tprint num\n",
"\ti += 1 \t\t\t# to iterate over the input array\n",
"\n",
"# Print totals\n",
"\n",
"print \"\\n------------\\n\\nYou entered:\"\n",
"for i in range(RANGE_HIGH - RANGE_LOW + 1) :\n",
"\tprint \"%2d %2d's\"%(tally[i],i + RANGE_LOW)\n",
"\n",
"print \"\\nThank you.\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter a number between 1 and 10 (-1 to quit): 3\n",
"\n",
"Enter another number: 5\n",
"\n",
"Enter another number: 1\n",
"\n",
"Enter another number: 10\n",
"\n",
"Enter another number: 8\n",
"\n",
"Enter another number: 5\n",
"\n",
"Enter another number: 10\n",
"\n",
"Enter another number: 6\n",
"\n",
"Enter another number: 2\n",
"\n",
"Enter another number: 11\n",
"11 is not between 1 and 10.\n",
"\n",
"Enter another number: 4\n",
"\n",
"Enter another number: 9\n",
"\n",
"Enter another number: -2\n",
"-2 is not between 1 and 10.\n",
"\n",
"Enter another number: 3\n",
"\n",
"Enter another number: 2\n",
"\n",
"Enter another number: 9\n",
"\n",
"Enter another number: -1\n",
"\n",
"------------\n",
"\n",
"You entered:\n",
" 1 1's\n",
" 2 2's\n",
" 2 3's\n",
" 1 4's\n",
" 2 5's\n",
" 1 6's\n",
" 0 7's\n",
" 1 8's\n",
" 2 9's\n",
" 2 10's\n",
"\n",
"Thank you.\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 10-13, Page Number: 255"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to report car prices. User enters car make by number and car year. \n",
"# Program reports price. All data is fictitious\n",
"\n",
"# 2-D array (4 X 12 ) of prices : row = make , column = year\n",
"price_list = [\t[ 4775, 4980, 5222, 5305, 5483, 5547, 5596, 5713, 5842, 5903, 6043, 6230 ], # Volkswagen\n",
"\t\t\t\t[ 4853, 5140, 5413, 5590, 5723, 5848, 5762, 5944, 6104, 6255, 6370, 6526 ],\t# Chevy\n",
"\t\t\t\t[ 5627, 5772, 5973, 6210, 6539, 6720, 6792, 6930, 7054, 7202, 7355, 7562 ], # Cadillac\n",
"\t\t\t\t[ 1576, 1738, 1970, 2151, 2205, 2280, 2442, 2580, 2814, 1953, 3078, 3201 ],\t# Honda\n",
"\t\t\t]\t\n",
"\n",
"def get_make() : \n",
"\t\"\"\" read in and return the number (make) of the desired car \"\"\"\n",
"\t\n",
"\t# Print the choice numbers and names\n",
"\tfor i in range(1,5) :\n",
"\t\tprint \"%d=\"%i,\n",
"\t\tprint_make(i)\n",
"\t\tprint \",\",\n",
"\tprint \"0=END\"\n",
"\n",
"\t# Read in the selection by number\n",
"\tprint \" Which car do you want? \",\n",
"\tmake = 2\n",
"\tprint make\n",
"\twhile make < 0 or make > 4 :\n",
"\t\tprint \" Which car do you want? \",\n",
"\t\tmake = 2\n",
"\t\tprint make\n",
"\n",
"\treturn make\n",
"\n",
"def get_year() :\t\n",
"\t\"\"\" read in and return the year of the desired car \"\"\"\n",
"\n",
"\tprint \" What year (1975-1986)?\",\n",
"\tyear = 1980\n",
"\tprint year\n",
"\twhile year < 1975 and year > 1986 :\n",
"\t\tprint \" What year (1975-1986)?\",\n",
"\t\tyear = 1980\n",
"\t\tprint year\n",
"\n",
"\treturn year\n",
"\n",
"def price (car,yr) :\n",
"\t\"\"\" Use \"car\" and \"year\" to generate an index into \"price_list\" and return the corresponding entry \"\"\"\n",
"\treturn price_list[ car - 1 ][ yr - 1975]\n",
"\n",
"def print_make(car) :\n",
"\t\"\"\" print the make of \"car\" according to its value \"\"\"\n",
"\tif car == 1 : \n",
"\t\tprint \"Volkswagen\",\n",
"\telif car == 2 :\n",
"\t\tprint \"Chevy\",\n",
"\telif car == 3 :\n",
"\t\tprint \"Cadillac\",\n",
"\telse :\n",
"\t\tprint \"Honda\",\n",
"\n",
"make = get_make()\n",
"\n",
"while make != 0 :\t\t# O indicates end of input\n",
"\tyear = get_year()\n",
"\tprint \"The price of a %d\"%year,\n",
"\tprint_make(make)\n",
"\tprint \" is $%d.\\n\"%price(make,year)\n",
"\tmake = 0 # To terminate the loop\n",
"\n",
"print \"\\nThank you for buying all those cars!\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1= Volkswagen , 2= Chevy , 3= Cadillac , 4= Honda , 0=END\n",
" Which car do you want? 2\n",
" What year (1975-1986)? 1980\n",
"The price of a 1980 Chevy is $5848.\n",
"\n",
"\n",
"Thank you for buying all those cars!\n"
]
}
],
"prompt_number": 13
}
],
"metadata": {}
}
]
}
|