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
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
|
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 9: More on Pointers"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.1, Page number: 318"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from array import *\n",
"\n",
"# Variable declaration\n",
"a = [[1,2,3,4,5],\n",
" [6,7,8,9,10],\n",
" [11,12,13,14,15],\n",
" [16,17,18,19,20],\n",
" [21,22,23,24,25]]\n",
"\n",
"# Calculation and result\n",
"print ('The value of a is %x ' % id(a))\n",
"\n",
"for i in range (0, 5) :\n",
"\tprint ('The value of a[%d] is %x ' % (i, id(i)))\n",
"\n",
"for i in range (0, 5) :\n",
"\tfor j in range (0, 5) : \n",
"\t\tprint ('The cell [%d][%d] i.e. %x has %d ' % (i, j, id(a[i][j]), a[i][j]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The value of a is 7f55dc3e08c0 \n",
"The value of a[0] is 18861b0 \n",
"The value of a[1] is 1886198 \n",
"The value of a[2] is 1886180 \n",
"The value of a[3] is 1886168 \n",
"The value of a[4] is 1886150 \n",
"The cell [0][0] i.e. 1886198 has 1 \n",
"The cell [0][1] i.e. 1886180 has 2 \n",
"The cell [0][2] i.e. 1886168 has 3 \n",
"The cell [0][3] i.e. 1886150 has 4 \n",
"The cell [0][4] i.e. 1886138 has 5 \n",
"The cell [1][0] i.e. 1886120 has 6 \n",
"The cell [1][1] i.e. 1886108 has 7 \n",
"The cell [1][2] i.e. 18860f0 has 8 \n",
"The cell [1][3] i.e. 18860d8 has 9 \n",
"The cell [1][4] i.e. 18860c0 has 10 \n",
"The cell [2][0] i.e. 18860a8 has 11 \n",
"The cell [2][1] i.e. 1886090 has 12 \n",
"The cell [2][2] i.e. 1886078 has 13 \n",
"The cell [2][3] i.e. 1886060 has 14 \n",
"The cell [2][4] i.e. 1886048 has 15 \n",
"The cell [3][0] i.e. 1886030 has 16 \n",
"The cell [3][1] i.e. 1886018 has 17 \n",
"The cell [3][2] i.e. 1886000 has 18 \n",
"The cell [3][3] i.e. 1885fe8 has 19 \n",
"The cell [3][4] i.e. 1885fd0 has 20 \n",
"The cell [4][0] i.e. 1885fb8 has 21 \n",
"The cell [4][1] i.e. 1885fa0 has 22 \n",
"The cell [4][2] i.e. 1885f88 has 23 \n",
"The cell [4][3] i.e. 1885f70 has 24 \n",
"The cell [4][4] i.e. 1885f58 has 25 \n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.2, Page number: 320"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from array import *\n",
"\n",
"# Variable declaration\n",
"a = [[[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]],\n",
" [[21,22,23,24,25], [26,27,28,29,30], [31,32,33,34,35], [36,37,38,39,40]]]\n",
"\n",
"# Calculation and result\n",
"print ('The value of a is %x ' % id(a))\n",
"\n",
"for i in range (0, 2) :\n",
"\tprint ('The value of a[%d] is %x ' % (i, id(a[i])))\n",
"\tfor j in range (0, 4) :\n",
"\t\tprint ('The value of a[%d][%d] is %x ' % (i, j, id(a[i][j])))\n",
"\tprint\n",
"\n",
"for i in range (0, 2) :\n",
"\tfor j in range (0, 4) : \n",
"\t\tfor k in range (0, 5) :\n",
"\t\t\tprint ('The cell [%d][%d][%d] i.e. %x has %d ' % (i, j, k, id(a[i][j][k]), a[i][j][k]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The value of a is 7f55de4772d8 \n",
"The value of a[0] is 7f55de486b90 \n",
"The value of a[0][0] is 7f55de4db368 \n",
"The value of a[0][1] is 7f55de48f050 \n",
"The value of a[0][2] is 7f55de4db3f8 \n",
"The value of a[0][3] is 7f55de4c7908 \n",
"\n",
"The value of a[1] is 7f55de477320 \n",
"The value of a[1][0] is 7f55de4ff3f8 \n",
"The value of a[1][1] is 7f55de477290 \n",
"The value of a[1][2] is 7f55de4773b0 \n",
"The value of a[1][3] is 7f55de477368 \n",
"\n",
"The cell [0][0][0] i.e. 1886198 has 1 \n",
"The cell [0][0][1] i.e. 1886180 has 2 \n",
"The cell [0][0][2] i.e. 1886168 has 3 \n",
"The cell [0][0][3] i.e. 1886150 has 4 \n",
"The cell [0][0][4] i.e. 1886138 has 5 \n",
"The cell [0][1][0] i.e. 1886120 has 6 \n",
"The cell [0][1][1] i.e. 1886108 has 7 \n",
"The cell [0][1][2] i.e. 18860f0 has 8 \n",
"The cell [0][1][3] i.e. 18860d8 has 9 \n",
"The cell [0][1][4] i.e. 18860c0 has 10 \n",
"The cell [0][2][0] i.e. 18860a8 has 11 \n",
"The cell [0][2][1] i.e. 1886090 has 12 \n",
"The cell [0][2][2] i.e. 1886078 has 13 \n",
"The cell [0][2][3] i.e. 1886060 has 14 \n",
"The cell [0][2][4] i.e. 1886048 has 15 \n",
"The cell [0][3][0] i.e. 1886030 has 16 \n",
"The cell [0][3][1] i.e. 1886018 has 17 \n",
"The cell [0][3][2] i.e. 1886000 has 18 \n",
"The cell [0][3][3] i.e. 1885fe8 has 19 \n",
"The cell [0][3][4] i.e. 1885fd0 has 20 \n",
"The cell [1][0][0] i.e. 1885fb8 has 21 \n",
"The cell [1][0][1] i.e. 1885fa0 has 22 \n",
"The cell [1][0][2] i.e. 1885f88 has 23 \n",
"The cell [1][0][3] i.e. 1885f70 has 24 \n",
"The cell [1][0][4] i.e. 1885f58 has 25 \n",
"The cell [1][1][0] i.e. 1885f40 has 26 \n",
"The cell [1][1][1] i.e. 1885f28 has 27 \n",
"The cell [1][1][2] i.e. 1885f10 has 28 \n",
"The cell [1][1][3] i.e. 1885ef8 has 29 \n",
"The cell [1][1][4] i.e. 1885ee0 has 30 \n",
"The cell [1][2][0] i.e. 1885ec8 has 31 \n",
"The cell [1][2][1] i.e. 1885eb0 has 32 \n",
"The cell [1][2][2] i.e. 1885e98 has 33 \n",
"The cell [1][2][3] i.e. 1885e80 has 34 \n",
"The cell [1][2][4] i.e. 1885e68 has 35 \n",
"The cell [1][3][0] i.e. 1886618 has 36 \n",
"The cell [1][3][1] i.e. 1886600 has 37 \n",
"The cell [1][3][2] i.e. 18865e8 has 38 \n",
"The cell [1][3][3] i.e. 18865d0 has 39 \n",
"The cell [1][3][4] i.e. 18865b8 has 40 \n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.3, Page number: 326"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Function declaration, calculation and result\n",
"def readdata (a, num) :\n",
"\twhile num > 0 :\n",
"\t\ta[num-1] = int(raw_input('Enter value in cell[%d] ' % num))\n",
"\t\tnum = num - 1\n",
"\t\t\n",
"def printdata (a, num) :\n",
"\twhile num > 0 :\n",
"\t\tprint ('Value in cell[%d] is %d ' % (num, a[num-1]))\n",
"\t\tnum = num - 1\n",
"\n",
"n = int(raw_input('Enter the number of elements '))\n",
"a = [0 for i in xrange(n)]\n",
"readdata (a, n)\n",
"print\n",
"printdata (a, n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number of elements 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[3] 30\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[2] 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[1] 10\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Value in cell[3] is 30 \n",
"Value in cell[2] is 20 \n",
"Value in cell[1] is 10 \n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.4, Page number: 328"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Function declaration, calculation and result\n",
"def readdata (a, n) :\n",
"\twhile n > 0 :\n",
"\t\ta[n-1] = int(raw_input('Enter value in cell[%d] ' % n))\n",
"\t\tn = n - 1\n",
"\t\t\n",
"def printdata (a, n) :\n",
"\twhile n > 0 :\n",
"\t\tprint ('Value in cell[%d] is %d ' % (n, a[n-1]))\n",
"\t\tn = n - 1\n",
"\n",
"n = int(raw_input('Enter the number of elements '))\n",
"a = [0 for i in xrange(n)]\n",
"readdata (a, n)\n",
"print\n",
"printdata (a, n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number of elements 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[5] 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[4] 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[3] 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[2] 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[1] 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Value in cell[5] is 1 \n",
"Value in cell[4] is 2 \n",
"Value in cell[3] is 3 \n",
"Value in cell[2] is 4 \n",
"Value in cell[1] is 5 \n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.5, Page number: 330"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Function declaration, calculation and result\n",
"global n\n",
"n = 0\n",
"\n",
"def makearray (n) :\n",
"\tn = int(raw_input('Enter the number of elements '))\t\n",
"\treturn n\n",
"\t\n",
"def readdata (a, n) :\n",
"\twhile n > 0 :\n",
"\t\ta[n-1] = int(raw_input('Enter value in cell[%d] ' % n))\n",
"\t\tn = n - 1\n",
"\t\t\n",
"def printdata (a, n) :\n",
"\twhile n > 0 :\n",
"\t\tprint ('Value in cell[%d] is %d ' % (n, a[n-1]))\n",
"\t\tn = n - 1\n",
"\t\t\n",
"n = makearray (n)\n",
"a = [0 for i in xrange(n)]\n",
"readdata (a, n)\n",
"print\n",
"printdata (a, n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number of elements 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[4] 400\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[3] 300\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[2] 200\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value in cell[1] 100\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Value in cell[4] is 400 \n",
"Value in cell[3] is 300 \n",
"Value in cell[2] is 200 \n",
"Value in cell[1] is 100 \n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.6, Page number: 335"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Variable declaration, calculation and result\n",
"string = raw_input('Enter the string: ')\n",
"\n",
"rev_string = reversed(string)\n",
"\n",
"if list(string) == list(rev_string) :\n",
"\tprint ('%s is a PALINDROME ' % string)\n",
"else :\n",
"\tprint ('%s is not a PALINDROME ' % string)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the string: malayalam\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"malayalam is a PALINDROME \n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.7, Page number: 337"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Function declaration, calculation and result\n",
"def readdata (a, n) :\n",
"\tfor i in range (0, n) :\n",
"\t\ta[i] = int(raw_input('Enter data in cell [%d] ' % i))\n",
"\treturn a\n",
"\n",
"def bubble (a, n) :\n",
"\tfor p in range (0, n) :\n",
"\t\tfor i in range (0, n-1-p) :\n",
"\t\t\tif a[i] > a[i+1] :\n",
"\t\t\t\ttemp = a[i]\n",
"\t\t\t\ta[i] = a[i+1]\n",
"\t\t\t\ta[i+1] = temp\n",
"\treturn a\n",
"\n",
"def printdata (a, n) :\n",
"\tfor i in range (0, n) :\n",
"\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n",
"\n",
"n = int(raw_input('Enter the number of elements to be entered: '))\n",
"a = [0] * n\n",
"a = readdata (a, n)\n",
"a = bubble (a, n)\n",
"print ('\\nThe sorted data is ')\n",
"printdata (a, n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number of elements to be entered: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter data in cell [0] 46\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter data in cell [1] 75\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter data in cell [2] 68\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter data in cell [3] 34\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter data in cell [4] 49\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The sorted data is \n",
"The data in cell [0] is 34 \n",
"The data in cell [1] is 46 \n",
"The data in cell [2] is 49 \n",
"The data in cell [3] is 68 \n",
"The data in cell [4] is 75 \n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.8, Page number: 341"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Variable declaration, calculation and result\n",
"i = 6\n",
"c = 'a'\n",
"\n",
"the_data = i\n",
"print ('the_data points to the integer value %d ' % the_data)\n",
"\n",
"the_data = c\n",
"print ('the_data points to the character value %c ' % the_data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"the_data points to the integer value 6 \n",
"the_data points to the character value a \n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.9, Page number: 343"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Variable declaration\n",
"marks = [[0 for x in xrange(5)] for x in xrange(5)]\n",
"avg = 0\n",
"percent = [0 for x in xrange(5)]\n",
"\n",
"# Function declaration, calculation and result\n",
"def readncompute (marks, percent, avg) :\n",
"\tno_of_students = int(raw_input('How many students marks do you want to enter? '))\n",
"\tfor i in range (0, no_of_students) :\n",
"\t\tprint ('\\nEnter marks of 5 subjects for roll no. %d ' % (i+1))\n",
"\t\tfor j in range (0, 5) :\n",
"\t\t\tmarks[i][j] = int(raw_input('Subject %d: ' % (j+1)))\n",
"\t\t\tpercent[i] = percent[i] + marks[i][j]\n",
"\t\t\n",
"\t\tpercent[i] = percent[i]/5\n",
"\t\tavg = avg + percent[i]\n",
"\treturn avg/no_of_students, no_of_students\n",
"\n",
"def printdata (score, per, avg, no_of_students) :\n",
"\tfor i in range (0, no_of_students) :\n",
"\t\tprint ('\\nMarks for roll no. %d are ' % (i+1))\n",
"\t\tfor j in range (0, 5) :\n",
"\t\t\tprint ('%d ' % score[i][j])\n",
"\t\tprint ('Percentage for roll no. is %.2f ' % per[i])\n",
"\t\tif per[i] < avg :\n",
"\t\t\tprint ('Below average')\n",
"\t\telse :\n",
"\t\t\tprint ('Above average')\n",
"\n",
"average, nos = readncompute(marks, percent, avg)\n",
"print ('\\nThe average marks are %.2f' % average)\n",
"printdata (marks, percent, average, nos)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"How many students marks do you want to enter? 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter marks of 5 subjects for roll no. 1 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 1: 65\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 2: 76\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 3: 54\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 4: 83\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 5: 94\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter marks of 5 subjects for roll no. 2 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 1: 84\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 2: 62\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 3: 94\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 4: 73\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 5: 62\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The average marks are 74.00\n",
"\n",
"Marks for roll no. 1 are \n",
"65 \n",
"76 \n",
"54 \n",
"83 \n",
"94 \n",
"Percentage for roll no. is 74.00 \n",
"Above average\n",
"\n",
"Marks for roll no. 2 are \n",
"84 \n",
"62 \n",
"94 \n",
"73 \n",
"62 \n",
"Percentage for roll no. is 75.00 \n",
"Above average\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.10, Page number: 349"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Variable declaration\n",
"marks = [[0 for x in xrange(5)] for x in xrange(5)]\n",
"avg = 0\n",
"percent = [0 for x in xrange(5)]\n",
"\n",
"# Function declaration, calculation and result\n",
"def readncompute (marks, percent, avg) :\n",
"\tno_of_students = int(raw_input('How many students marks do you want to enter? '))\n",
"\tfor i in range (0, no_of_students) :\n",
"\t\tprint ('\\nEnter marks of 5 subjects for roll no. %d ' % (i+1))\n",
"\t\tfor j in range (0, 5) :\n",
"\t\t\tmarks[i][j] = int(raw_input('Subject %d: ' % (j+1)))\n",
"\t\t\tpercent[i] = percent[i] + marks[i][j]\n",
"\t\t\n",
"\t\tpercent[i] = percent[i]/5\n",
"\t\tavg = avg + percent[i]\n",
"\treturn avg/no_of_students, no_of_students\n",
"\n",
"def printdata (score, per, avg, no_of_students) :\n",
"\tfor i in range (0, no_of_students) :\n",
"\t\tprint ('\\nMarks for roll no. %d are ' % (i+1))\n",
"\t\tfor j in range (0, 5) :\n",
"\t\t\tprint ('%d ' % score[i][j])\n",
"\t\tprint ('Percentage for roll no. is %.2f ' % per[i])\n",
"\t\tif per[i] < avg :\n",
"\t\t\tprint ('Below average')\n",
"\t\telse :\n",
"\t\t\tprint ('Above average')\n",
"\n",
"average, nos = readncompute(marks, percent, avg)\n",
"print ('\\nThe average marks are %.2f' % average)\n",
"printdata (marks, percent, average, nos)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"How many students marks do you want to enter? 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter marks of 5 subjects for roll no. 1 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 1: 65\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 2: 76\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 3: 54\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 4: 32\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 5: 89\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter marks of 5 subjects for roll no. 2 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 1: 76\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 2: 86\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 3: 43\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 4: 32\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Subject 5: 21\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The average marks are 57.00\n",
"\n",
"Marks for roll no. 1 are \n",
"65 \n",
"76 \n",
"54 \n",
"32 \n",
"89 \n",
"Percentage for roll no. is 63.00 \n",
"Above average\n",
"\n",
"Marks for roll no. 2 are \n",
"76 \n",
"86 \n",
"43 \n",
"32 \n",
"21 \n",
"Percentage for roll no. is 51.00 \n",
"Below average\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9.11, Page number: 351"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Function declaration, calculation and result\n",
"def readdata (a, n) :\n",
"\tfor i in range (0, n) :\n",
"\t\ta[i] = int(raw_input('Enter data in cell [%d] ' % i))\n",
"\treturn a\n",
"\n",
"def bubble (name) :\n",
"\tfor p in range (0, 4) :\n",
"\t\tfor i in range (0, 3-p) :\n",
"\t\t\tif name[i] > name[i+1] :\n",
"\t\t\t\ttemp = name[i]\n",
"\t\t\t\tname[i] = name[i+1]\n",
"\t\t\t\tname[i+1] = temp\n",
"\treturn name\n",
"\n",
"def printdata (name) :\n",
"\tfor i in range (0, 4) :\n",
"\t\tprint ('The data in cell [%d] is %s ' % ((i+1), name[i]))\n",
"\n",
"name = [0] * 4\n",
"for i in range (0, 4) :\n",
"\tname[i] = raw_input('Enter string[%d] ' % (i+1))\n",
"\n",
"name = bubble (name)\n",
"print ('\\nThe sorted data is ')\n",
"printdata (name)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter string[1] zaheer\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter string[2] ashita\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter string[3] tamanna\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter string[4] purti\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The sorted data is \n",
"The data in cell [1] is ashita \n",
"The data in cell [2] is purti \n",
"The data in cell [3] is tamanna \n",
"The data in cell [4] is zaheer \n"
]
}
],
"prompt_number": 9
}
],
"metadata": {}
}
]
}
|