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
|
{
"metadata": {
"name": "Chapter VIII"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Chapter 8: Working with functions"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.1, Page number: 120"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.1.py\n",
"#Writing a Function in python\n",
"\n",
"\n",
"def printMessage(): #Function to Print Message\n",
" print(\"Programming is Fun.\\n\") #Print Statement\n",
"\n",
"\n",
"def main(): #Main() function\n",
" printMessage()\n",
"\n",
"\n",
"if __name__ =='__main__': #Setting Top-level conditional script\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Programming is Fun.\n",
"\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.2, Page number: 121"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.2.py\n",
"#Calling Functions\n",
"\n",
"\n",
"def printMessage(): #Function to Print Message\n",
" print(\"Programming is Fun.\\n\") #Print Statement\n",
"\n",
"\n",
"def main(): #Main() function\n",
" printMessage() #First Function call\n",
" printMessage() #Second Function Call\n",
"\n",
"if __name__ =='__main__': #Setting Top-level conditional script\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Programming is Fun.\n",
"\n",
"Programming is Fun.\n",
"\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.3, Page number: 122"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.3.py\n",
"#More on Calling Functions\n",
"\n",
"\n",
"def printMessage(): #Function to Print Message\n",
" print(\"Programming is Fun.\\n\") #Print Statement\n",
"\n",
"\n",
"def main(): \n",
" for i in range (1,6): #Main() function\n",
" printMessage()\n",
"\n",
"\n",
"if __name__ =='__main__': #Setting Top-level script environment\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Programming is Fun.\n",
"\n",
"Programming is Fun.\n",
"\n",
"Programming is Fun.\n",
"\n",
"Programming is Fun.\n",
"\n",
"Programming is Fun.\n",
"\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.4, Page number: 123"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.4.py\n",
"#Calculating the nth Triangular Number\n",
"\n",
"\n",
"def calculateTriangularNumber(n): #Function to calculate triangular number\n",
" triangularNumber=0 #Variable Dclaration\n",
" \n",
" for i in range (1,n+1): #Calculation/Iteration\n",
" triangularNumber+=i\n",
" \n",
" print(\"Triangular Number {0} is {1}\".format(n,triangularNumber))\n",
"\n",
"\n",
"def main():\n",
" calculateTriangularNumber(10) #Function call-1\n",
" calculateTriangularNumber(20) #Function call-2\n",
" calculateTriangularNumber(50) #Function call-3\n",
" \n",
"\n",
"if __name__=='__main__': #Setting top level conditional script\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Triangular Number 10 is 55\n",
"Triangular Number 20 is 210\n",
"Triangular Number 50 is 1275\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.5, Page number: 125"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.5.py\n",
"#Revising the Program to Find the Greatest Common Divisor\n",
"\n",
"#Import Library \n",
"import sys \n",
"\n",
"def gcd(u,v): #Function to calculate gcd\n",
" sys.stdout.write(\"gcd of {0} and {1} is: \".format(u,v))\n",
" while(v!=0):\n",
" temp=u%v\n",
" u=v\n",
" v=temp\n",
" sys.stdout.write(\"{0}\\n\".format(u))\n",
"\n",
"\n",
"\n",
"def main(): #Main() function\n",
" gcd(150,35) \n",
" gcd(1026,405)\n",
" gcd(83,240)\n",
"\n",
"if __name__=='__main__': #Setting Top level conditional script \n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"gcd of 150 and 35 is: 5\n",
"gcd of 1026 and 405 is: 27\n",
"gcd of 83 and 240 is: 1\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.6, Page number: 127"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.6.py\n",
"#Finding the Greatest Common Divisor and Returning the Results\n",
"\n",
"#Import Library \n",
"import sys \n",
"\n",
"def gcd(u,v): #Function to calculate gcd\n",
"\n",
" while(v!=0):\n",
" temp=u%v\n",
" u=v\n",
" v=temp\n",
" return u\n",
"\n",
"def main(): #Main() function\n",
" result= gcd(150,35) \n",
" print(\"the gcd of 150 and 35 is: {0}\".format(result))\n",
" result=gcd(1026,405)\n",
" print(\"the gcd of 1026 and 405 is: {0}\".format(result)) \n",
" result= gcd(83,240)\n",
" print(\"the gcd of 83 and 240 is: {0}\".format(result))\n",
"\n",
"if __name__=='__main__': #Top level conditional script \n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"the gcd of 150 and 35 is: 5\n",
"the gcd of 1026 and 405 is: 27\n",
"the gcd of 83 and 240 is: 1\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.7, Page number: 129"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.7.py\n",
"#Calculating the Absolute Value\n",
"\n",
"#yourstory.in\n",
"\n",
"#Calculations\n",
"def absoluteValue(x): #Function to calculate & return absolute values\n",
" if(x<0):\n",
" x=-x\n",
" return x\n",
"\n",
"def main(): #Main() Function\n",
" \n",
" f1=-15.5\n",
" f2=20.0\n",
" f3=-5.0\n",
" il=-716\n",
"#Result \n",
" result=absoluteValue(f1)\n",
" print(\"result= {0:.2f}\".format(result))\n",
" print(\"f1={0:.2f}\".format(f1))\n",
"\n",
" result=absoluteValue(f2)+absoluteValue(f3)\n",
" print(\"result= {0:.2f}\".format(result))\n",
"\n",
" result=absoluteValue(float(il))\n",
" print(\"result= {0:.2f}\".format(result))\n",
" \n",
" result=absoluteValue(il)\n",
" print(\"resut= {0:.2f}\".format(result))\n",
"\n",
" print(\"{0:.2f}\".format(absoluteValue((-6.0)/4)))\n",
"\n",
"\n",
"#End of Main()\n",
" \n",
"if __name__=='__main__': #Setting Top level conditional script\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"result= 15.50\n",
"f1=-15.50\n",
"result= 25.00\n",
"result= 716.00\n",
"resut= 716.00\n",
"1.50\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.8, Page number: 132"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.8.py\n",
"#Calculating the Square Root of a Number using Newton-Raphson Method \n",
"\n",
"\n",
"#Function to return absolute value\n",
"def absoluteValue(x): \n",
" if(x<0):\n",
" x=-x\n",
" return x\n",
"\n",
"#function to calculate square root\n",
"def squareRoot(x):\n",
" epsilon=0.0001\n",
" guess=1.0\n",
" while(absoluteValue(guess*guess-x)>=epsilon):\n",
" guess=(x/guess+guess)/2.0\n",
" return guess\n",
"\n",
"#Main()\n",
"def main():\n",
" print(\"squareRoot (2.0) = {0}\".format(squareRoot (2.0)));\n",
" print(\"squareRoot (144.0) = {0}\".format(squareRoot (144.0)));\n",
" print(\"squareRoot (17.5) = {0}\".format(squareRoot (17.5)));\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"squareRoot (2.0) = 1.41421568627\n",
"squareRoot (144.0) = 12.0000000124\n",
"squareRoot (17.5) = 4.18330153622\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.9, Page number: 138"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.9.py\n",
"#Finding the Minimum Value in an Array\n",
"\n",
"#Function to return minimum value\n",
"def minimum(values):\n",
" minValue=values[0] #Variable Declaration\n",
" for i in range (1,10): #Calculation\n",
" if(values[i]<minValue):\n",
" minValue=values[i]\n",
"\n",
" return minValue\n",
" \n",
"#Main()\n",
"def main():\n",
" scores=[] #Variable Declaration\n",
" print(\"Enter 10 scores:\")\n",
" for i in range (0,10):\n",
" scores.append(5) #score=5 for all cases\n",
" #scores.append(input()) \n",
"\n",
" minScore=minimum(scores) #Function call & assignment\n",
" print(\"\\nMinimum score is {0}\\n\".format(minScore)) #Result\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter 10 scores:\n",
"\n",
"Minimum score is 5\n",
"\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.10, Page number: 141"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.10.py\n",
"#Revising the Function to Find the Minimum Value in an Array\n",
"\n",
"#Function to return minimum value\n",
"def minimum(values,numberOfElements): \n",
" minValue=values[0] #Variable Declaration\n",
" for i in range(1,numberOfElements): #Iteration/Calculations\n",
" if(values[i]<minValue):\n",
" minValue=values[i]\n",
" \n",
" return minValue\n",
"\n",
"#Main()\n",
"def main():\n",
" #List/Variable Delcaration\n",
" array1 = [ 157, -28, -37, 26, 10 ] \n",
" array2 = [ 12, 45, 1, 10, 5, 3, 22 ]\n",
" \n",
" #Result\n",
" print(\"array1 minimum: {0}\".format(minimum (array1, 5)));\n",
" print(\"array2 minimum: {0}\".format(minimum (array2, 7)));\n",
"\n",
"#Setting top level conditional script \n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"array1 minimum: -37\n",
"array2 minimum: 1\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.11, Page number: 142"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.11.py\n",
"#Changing Array Elements in Functions\n",
"\n",
"#Import System Library\n",
"import sys\n",
"\n",
"#Function to multiply elements by 2\n",
"def multiplyBy2(array,n):\n",
" for i in range (0,n): #Iteration/Calculation\n",
" array[i]*=2\n",
"\n",
"#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration\n",
" floatVals=[1.2, -3.7, 6.2, 8.55] \n",
" multiplyBy2(floatVals,4)\n",
"\n",
" for i in range (0,4):\n",
" sys.stdout.write(\"{0:.2f} \".format(floatVals[i])) #Result\n",
" print(\"\\n\")\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2.40 -7.40 12.40 17.10 \n",
"\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.12, Page number: 145"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.12.py\n",
"#\n",
"\n",
"#Import system Library \n",
"import sys\n",
"\n",
"#Function to sort the array\n",
"def sort(a,n):\n",
"\n",
" for i in range (0,n-1): #Calculations\n",
" for j in range (i+1,n):\n",
" if(a[i]>a[j]): #Conditional swapping\n",
" temp=a[i]\n",
" a[i]=a[j]\n",
" a[j]=temp\n",
"\n",
"#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration \n",
" array= [ 34, -5, 6, 0, 12, 100, 56, 22,\\\n",
" 44, -3, -9, 12, 17, 22, 6, 11 ]\n",
" \n",
" print(\"The array before the sort:\\n\");\n",
" for i in range (0,16):\n",
" sys.stdout.write(\"{0} \".format(array[i]))\n",
" \n",
" sort (array, 16); #Function Call\n",
" \n",
" print(\"\\n\\n\\nThe array after the sort:\\n\")\n",
" for i in range (0,16):\n",
" sys.stdout.write(\"{0} \".format(array[i]))\n",
" print(\"\\n\")\n",
"\n",
"\n",
"#Setting top level conditional script \n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The array before the sort:\n",
"\n",
"34 -5 6 0 12 100 56 22 44 -3 -9 12 17 22 6 11 \n",
"\n",
"\n",
"The array after the sort:\n",
"\n",
"-9 -5 -3 0 6 6 11 12 12 17 22 22 34 44 56 100 \n",
"\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.13, Page number: 147"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.13.py\n",
"#Using Multidimensional Arrays and Functions\n",
"\n",
"#Import system Library\n",
"import sys\n",
"\n",
"#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration\n",
" sampleMatrix =[ [ 7, 16, 55, 13, 12 ],\n",
" [ 12, 10, 52, 0, 7 ],\n",
" [ -2, 1, 2, 4, 9 ] ]\n",
"\n",
"\n",
" print(\"Original matrix:\\n\")\n",
" displayMatrix(sampleMatrix) #Function call-1\n",
"\n",
" scalarMultiply(sampleMatrix, 2) #Function Call-2\n",
" print(\"\\nMultiplied by 2:\\n\")\n",
" \n",
" displayMatrix(sampleMatrix); #Function call-3\n",
" scalarMultiply(sampleMatrix, -1) #Function call-4\n",
" \n",
" print(\"\\nThen multiplied by -1:\\n\")\n",
" displayMatrix(sampleMatrix) #Function call-5\n",
"\n",
"\n",
"#Function to multiply matrix by a scalar quantity \n",
"def scalarMultiply(matrix,scalar):\n",
" for row in range(0,3): #Calculation\n",
" for column in range(0,5):\n",
" matrix[row][column]*=scalar \n",
"\n",
"\n",
"#Function to display the matrix\n",
"def displayMatrix(matrix):\n",
" for row in range(0,3): #Result\n",
" for column in range(0,5):\n",
" sys.stdout.write(\"{0:7}\".format(matrix[row][column]))\n",
" sys.stdout.write(\"\\n\")\n",
"\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Original matrix:\n",
"\n",
" 7 16 55 13 12\n",
" 12 10 52 0 7\n",
" -2 1 2 4 9\n",
"\n",
"Multiplied by 2:\n",
"\n",
" 14 32 110 26 24\n",
" 24 20 104 0 14\n",
" -4 2 4 8 18\n",
"\n",
"Then multiplied by -1:\n",
"\n",
" -14 -32 -110 -26 -24\n",
" -24 -20 -104 0 -14\n",
" 4 -2 -4 -8 -18\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.13A, Page number: 150"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.13A.py\n",
"#Multidimensional Variable-Length Arrays\n",
"\n",
"#Import system Library\n",
"import sys\n",
"\n",
"#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration\n",
" sampleMatrix =[ [ 7, 16, 55, 13, 12 ],\n",
" [ 12, 10, 52, 0, 7 ],\n",
" [ -2, 1, 2, 4, 9 ] ]\n",
"\n",
"\n",
" print(\"Original matrix:\\n\")\n",
" displayMatrix(3,5,sampleMatrix) #Function call-1\n",
"\n",
" scalarMultiply(3,5,sampleMatrix, 2) #Function Call-2\n",
" print(\"\\nMultiplied by 2:\\n\")\n",
" \n",
" displayMatrix(3,5,sampleMatrix); #Function call-3\n",
" scalarMultiply(3,5,sampleMatrix, -1) #Function call-4\n",
" \n",
" print(\"\\nThen multiplied by -1:\\n\")\n",
" displayMatrix(3,5,sampleMatrix) #Function call-5\n",
"\n",
"\n",
"#Function to multiply matrix by a scalar quantity \n",
"def scalarMultiply(nRows,nCols,matrix,scalar):\n",
" for row in range(0,nRows): #Calculation\n",
" for column in range(0,nCols):\n",
" matrix[row][column]*=scalar \n",
"\n",
"\n",
"#Function to display the matrix\n",
"def displayMatrix(nRows,nCols,matrix):\n",
" for row in range(0,nRows): #Result\n",
" for column in range(0,nCols):\n",
" sys.stdout.write(\"{0:7}\".format(matrix[row][column]))\n",
" sys.stdout.write(\"\\n\")\n",
"\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Original matrix:\n",
"\n",
" 7 16 55 13 12\n",
" 12 10 52 0 7\n",
" -2 1 2 4 9\n",
"\n",
"Multiplied by 2:\n",
"\n",
" 14 32 110 26 24\n",
" 24 20 104 0 14\n",
" -4 2 4 8 18\n",
"\n",
"Then multiplied by -1:\n",
"\n",
" -14 -32 -110 -26 -24\n",
" -24 -20 -104 0 -14\n",
" 4 -2 -4 -8 -18\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.14, Page number: 153"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.14.py\n",
"#Converting a Positive Integer to Another Base\n",
"\n",
"#Import system Libraries\n",
"import sys\n",
"\n",
"#Function to get Number & Base\n",
"def getNumberAndBase():\n",
" \n",
" #Global Reference\n",
" global digit\n",
" global numberToConvert\n",
" global base\n",
" \n",
" #Variable Declaration\n",
" digit=0\n",
" numberToConvert=420 #numberToConvert=input(\"Number to be converted ?\")\n",
" base=8 #base=input(\"Base?\")\n",
" if(base<2 or base>16):\n",
" print(\"Bad base - must be between 2 and 16\\n\");\n",
" base = 10;\n",
"\n",
"#Conversion Function\n",
"def convertNumber():\n",
" \n",
" #Global Reference\n",
" global numberToConvert\n",
" global base\n",
" global convertedNumber\n",
" global digit\n",
" convertedNumber=[0]*64 #List declaration\n",
" \n",
" while(numberToConvert!=0):\n",
" convertedNumber[digit]=numberToConvert%base #Calculations\n",
" digit=digit+1\n",
" numberToConvert/=base\n",
"\n",
"\n",
"#Function to display\n",
"def displayConvertedNumber():\n",
"\n",
" #Global reference \n",
" global baseDigits\n",
" global digit \n",
"\n",
" #List/Variable Declaration\n",
" baseDigits=[ '0', '1', '2', '3', '4', '5', '6', '7',\\\n",
" '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]\n",
" sys.stdout.write(\"Converted number = \")\n",
" \n",
" digit=digit-1\n",
"\n",
" for i in range(digit,-1,-1):\n",
" nextDigit = convertedNumber[i];\n",
" sys.stdout.write(\"{0}\".format(baseDigits[nextDigit])); #Result\n",
"\n",
" sys.stdout.write(\"\\n\")\n",
"\n",
"\n",
"\n",
"#Main()\n",
"def main():\n",
" getNumberAndBase() #Function call-1\n",
" convertNumber() #Function call-2\n",
" displayConvertedNumber() #Function call-3\n",
"\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Converted number = 644\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.15, Page number: 157"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.15.py\n",
"#Illustrating Static and Automatic Variables\n",
"\n",
"\n",
"#Function to display variables\n",
"def auto_static():\n",
" global staticVar #Global reference\n",
" autoVar=1 #variable Declaration\n",
" print(\"automatic = {0}, static = {1}\\n\".format(autoVar,staticVar)); #Result\n",
" \n",
" #Calculation \n",
" autoVar+=1\n",
" staticVar+=1\n",
"\n",
"#Main()\n",
"def main():\n",
" global staticVar\n",
" staticVar=1 #Variable Declaration\n",
" for i in range(0,5):\n",
" auto_static()\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"automatic = 1, static = 1\n",
"\n",
"automatic = 1, static = 2\n",
"\n",
"automatic = 1, static = 3\n",
"\n",
"automatic = 1, static = 4\n",
"\n",
"automatic = 1, static = 5\n",
"\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Program 8.16, Page number: 159"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#8.16.py\n",
"#Calculating Factorials Recursively\n",
"\n",
"#Function to calculate factorial\n",
"def factorial(n):\n",
" if( n == 0 ): #Calculation\n",
" result = 1\n",
" else:\n",
" result = n * factorial (n - 1)\n",
" return result;\n",
"\n",
"#Main()\n",
"def main():\n",
" for j in range (0,11):\n",
" print(\"{0:3}! = {1}\\n\".format(j,factorial (j)))\n",
"\n",
"\n",
"#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 0! = 1\n",
"\n",
" 1! = 1\n",
"\n",
" 2! = 2\n",
"\n",
" 3! = 6\n",
"\n",
" 4! = 24\n",
"\n",
" 5! = 120\n",
"\n",
" 6! = 720\n",
"\n",
" 7! = 5040\n",
"\n",
" 8! = 40320\n",
"\n",
" 9! = 362880\n",
"\n",
" 10! = 3628800\n",
"\n"
]
}
],
"prompt_number": 17
}
],
"metadata": {}
}
]
}
|