summaryrefslogtreecommitdiff
path: root/_Programming_With_C/chapter7.ipynb
blob: e33dc0cba6ce1cfa1c2f48723cc7cf1b75a59198 (plain)
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
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 7: Functions<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.1, Page number: 7.2<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# lowercase to uppercase character conversion\n",
      "\n",
      "\n",
      "def lower_to_upper(c1):\n",
      "\n",
      "    if c1>='a' and c1<='z':\n",
      "        c2=chr(ord('A')+ord(c1)-ord('a'))\n",
      "\n",
      "    else:\n",
      "        c2=c1\n",
      "\n",
      "    return c2\n",
      "\n",
      "lower='g'\n",
      "upper=lower_to_upper(lower)\n",
      "print \"the upper case equivalent is : \",upper\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "the upper case equivalent is :  G\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.3, Page number: 7.5<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# a variation to the function appeared in example 7.1\n",
      "\n",
      "\n",
      "def lower_to_upper(c1):\n",
      "\n",
      "    if c1>='a' and c1<='z':\n",
      "        return (chr(ord('A')+ord(c1)-ord('a')))\n",
      "    else:\n",
      "        return c1\n",
      "\n",
      "lower='g'\n",
      "upper=lower_to_upper(lower)\n",
      "print \"The uppercase equivalent is : \",upper\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The uppercase equivalent is :  G\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.4, Page number: 7.5<h3> "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# determine the larger of the two integer quantities\n",
      "\n",
      "\n",
      "def maximum(x,y):\n",
      "    if x>=y:\n",
      "        z=x\n",
      "    else:\n",
      "        z=y\n",
      "\n",
      "    print \"Maximum value is : \",z\n",
      "\n",
      "    return\n",
      "\n",
      "maximum(5,6)\n",
      "maximum(6,5)\n",
      "maximum(5,5)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Maximum value is :  6\n",
        "Maximum value is :  6\n",
        "Maximum value is :  5\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.5, Page number: 7.5<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# calculates the factorial of n\n",
      "\n",
      "\n",
      "def factorial(n):\n",
      "    prod=1\n",
      "    if n>1:\n",
      "        for i in range(2,n+1):\n",
      "            prod=prod*i\n",
      "\n",
      "    return prod\n",
      "\n",
      "fact=factorial(6)\n",
      "print \"Its factorial is : \",fact\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Its factorial is :  720\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.7, Page number: 7.7<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# finds max\n",
      "\n",
      "\n",
      "def maximum(x,y):\n",
      "\n",
      "    if x>=y:\n",
      "        z=x\n",
      "    else:\n",
      "        z=y\n",
      "\n",
      "    print 'Maximum value is = ',z\n",
      "    return\n",
      "\n",
      "maximum(5,9)\n",
      "maximum(8,3)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Maximum value is =  9\n",
        "Maximum value is =  8\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.9, Page number: 7.8<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Largest of the three quantities\n",
      "\n",
      "\n",
      "def maximum(x,y):\n",
      "    if x>=y:\n",
      "        z=x\n",
      "    else:\n",
      "        z=y\n",
      "\n",
      "    return z\n",
      "\n",
      "a=5\n",
      "b=4\n",
      "c=7\n",
      "\n",
      "d=maximum(a,b)\n",
      "print \"Maximum = \",maximum(c,d)\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Maximum =  7\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.10, Page number: 7.10<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# calculates the factorial of n\n",
      "\n",
      "\n",
      "def factorial(n):\n",
      "    prod=1\n",
      "    if n>1:\n",
      "        for i in range(2,n+1):\n",
      "            prod=prod*i\n",
      "\n",
      "    return prod\n",
      "\n",
      "n=7\n",
      "fact=factorial(n)\n",
      "print \"Its factorial is : \",fact\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Its factorial is :  5040\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.11, Page number: 7.12<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Simulation of a game of chance (shooting craps)\n",
      "\n",
      "\n",
      "\n",
      "def play():\n",
      "    print \"Throwing the dice....\"\n",
      "    score1=throw()\n",
      "    print \"%2d\" %(score1)\n",
      "\n",
      "    if score1==7 or score1==11:\n",
      "        print \"Congratulations!! you WIN on the first throw\"\n",
      "\n",
      "    elif score1==2 or score1==3 or score1==12:\n",
      "        print \"sorry!! you LOSE on the first throw\"\n",
      "\n",
      "    else:\n",
      "        while(True):\n",
      "            print \"Throwing the dice again...\"\n",
      "            score2=throw()\n",
      "            print \"%2d\" %(score2)\n",
      "            if score2==score1 or score2==7:\n",
      "                break\n",
      "\n",
      "        if score2==score1:\n",
      "            print \"You WIN by matching your first score\"\n",
      "        else:\n",
      "            print \"You LOSE by failing to match your first score\"\n",
      "\n",
      "\n",
      "    return\n",
      "\n",
      "\n",
      "def throw():\n",
      "\n",
      "    n1=random.randrange(1,7)\n",
      "    n2=random.randrange(1,7)\n",
      "    return n1+n2\n",
      "\n",
      "\n",
      "import random\n",
      "\n",
      "\n",
      "print \"Welcome to the Game of Craps \\n\\n\"\n",
      "random.seed(2365)\n",
      "play()\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Welcome to the Game of Craps \n",
        "\n",
        "\n",
        "Throwing the dice....\n",
        " 9\n",
        "Throwing the dice again...\n",
        " 4\n",
        "Throwing the dice again...\n",
        "11\n",
        "Throwing the dice again...\n",
        " 9\n",
        "You WIN by matching your first score\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.12, Page number: 7.18<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# function that alters the value of the argument\n",
      "\n",
      "\n",
      "def modify(a):\n",
      "    a=a*3\n",
      "    print \"a= %d (from the function, after being modified)\" %(a)\n",
      "    return\n",
      "\n",
      "a=2\n",
      "print \"a=%d (from main, before calling the function)\" %(a)\n",
      "modify(a)\n",
      "print \"a=%d (from main, after calling the function)\" %(a)\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "a=2 (from main, before calling the function)\n",
        "a= 6 (from the function, after being modified)\n",
        "a=2 (from main, after calling the function)\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.13, Page number: 7.19<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# calculating depreciation\n",
      "\n",
      "\n",
      "def sl(val,n):\n",
      "    deprec=val/n\n",
      "    print \"in \",n\n",
      "    \n",
      "    for year in range(1,n+1):\n",
      "        val=val-deprec\n",
      "        writeoutput(year,deprec,val)\n",
      "\n",
      "    return\n",
      "\n",
      "def ddb(val,n):\n",
      "    for year in range(1,n+1):\n",
      "        deprec=2*val/n\n",
      "        val=val-deprec\n",
      "        writeoutput(year,deprec,val)\n",
      "\n",
      "    return\n",
      "\n",
      "def syd(val,n):\n",
      "    tag=val\n",
      "    for year in range(1,n+1):\n",
      "        deprec=(n-year+1)*tag/(n*(n+1)/2)\n",
      "        val=val-deprec\n",
      "        writeoutput(year,deprec,val)\n",
      "\n",
      "    return\n",
      "\n",
      "def writeoutput(year,depreciation,value):\n",
      "    print \"End of the year %2d  Depreciation: %7.2f  Current Value: %8.2f\" %(year,depreciation,value)\n",
      "\n",
      "    return\n",
      "\n",
      "\n",
      "\n",
      "def main(choice,val,n):\n",
      "    \n",
      "    print \"Original value : \",val\n",
      "    val=float(val)\n",
      "    print \"Number of years : \",n\n",
      "\n",
      "    if choice==1:\n",
      "        print \"Straight-Line Method\\n\\n\"\n",
      "        sl(val,n)\n",
      "    elif choice==2:\n",
      "        print \"Double-Declining-Balance Method \\n\\n\"\n",
      "        ddb(val,n)\n",
      "    elif choice==3:\n",
      "        print \"Sum-Of-The-Years'-Digits Method\"\n",
      "        syd(val,n)\n",
      "\n",
      "    return\n",
      "\n",
      "\n",
      "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
      "main(1,8000,10)\n",
      "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
      "main(2,8000,10)\n",
      "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
      "main(3,8000,10)\n",
      "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
      "main(1,5000,4)\n",
      "\n",
      "\n",
      "    \n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "\n",
        "Method: (1-SL 2-DDB 3-SYD)\n",
        "Original value :  8000\n",
        "Number of years :  10\n",
        "Straight-Line Method\n",
        "\n",
        "\n",
        "in  10\n",
        "End of the year  1  Depreciation:  800.00  Current Value:  7200.00\n",
        "End of the year  2  Depreciation:  800.00  Current Value:  6400.00\n",
        "End of the year  3  Depreciation:  800.00  Current Value:  5600.00\n",
        "End of the year  4  Depreciation:  800.00  Current Value:  4800.00\n",
        "End of the year  5  Depreciation:  800.00  Current Value:  4000.00\n",
        "End of the year  6  Depreciation:  800.00  Current Value:  3200.00\n",
        "End of the year  7  Depreciation:  800.00  Current Value:  2400.00\n",
        "End of the year  8  Depreciation:  800.00  Current Value:  1600.00\n",
        "End of the year  9  Depreciation:  800.00  Current Value:   800.00\n",
        "End of the year 10  Depreciation:  800.00  Current Value:     0.00\n",
        "\n",
        "\n",
        "Method: (1-SL 2-DDB 3-SYD)\n",
        "Original value :  8000\n",
        "Number of years :  10\n",
        "Double-Declining-Balance Method \n",
        "\n",
        "\n",
        "End of the year  1  Depreciation: 1600.00  Current Value:  6400.00\n",
        "End of the year  2  Depreciation: 1280.00  Current Value:  5120.00\n",
        "End of the year  3  Depreciation: 1024.00  Current Value:  4096.00\n",
        "End of the year  4  Depreciation:  819.20  Current Value:  3276.80\n",
        "End of the year  5  Depreciation:  655.36  Current Value:  2621.44\n",
        "End of the year  6  Depreciation:  524.29  Current Value:  2097.15\n",
        "End of the year  7  Depreciation:  419.43  Current Value:  1677.72\n",
        "End of the year  8  Depreciation:  335.54  Current Value:  1342.18\n",
        "End of the year  9  Depreciation:  268.44  Current Value:  1073.74\n",
        "End of the year 10  Depreciation:  214.75  Current Value:   858.99\n",
        "\n",
        "\n",
        "Method: (1-SL 2-DDB 3-SYD)\n",
        "Original value :  8000\n",
        "Number of years :  10\n",
        "Sum-Of-The-Years'-Digits Method\n",
        "End of the year  1  Depreciation: 1454.55  Current Value:  6545.45\n",
        "End of the year  2  Depreciation: 1309.09  Current Value:  5236.36\n",
        "End of the year  3  Depreciation: 1163.64  Current Value:  4072.73\n",
        "End of the year  4  Depreciation: 1018.18  Current Value:  3054.55\n",
        "End of the year  5  Depreciation:  872.73  Current Value:  2181.82\n",
        "End of the year  6  Depreciation:  727.27  Current Value:  1454.55\n",
        "End of the year  7  Depreciation:  581.82  Current Value:   872.73\n",
        "End of the year  8  Depreciation:  436.36  Current Value:   436.36\n",
        "End of the year  9  Depreciation:  290.91  Current Value:   145.45\n",
        "End of the year 10  Depreciation:  145.45  Current Value:     0.00\n",
        "\n",
        "\n",
        "Method: (1-SL 2-DDB 3-SYD)\n",
        "Original value :  5000\n",
        "Number of years :  4\n",
        "Straight-Line Method\n",
        "\n",
        "\n",
        "in  4\n",
        "End of the year  1  Depreciation: 1250.00  Current Value:  3750.00\n",
        "End of the year  2  Depreciation: 1250.00  Current Value:  2500.00\n",
        "End of the year  3  Depreciation: 1250.00  Current Value:  1250.00\n",
        "End of the year  4  Depreciation: 1250.00  Current Value:     0.00\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.14, Page number: 7.24<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# calculating factorials in recursive way\n",
      "\n",
      "\n",
      "def factorial(n):\n",
      "\n",
      "    if n<=1:\n",
      "        return 1\n",
      "\n",
      "    else:\n",
      "        return (n*factorial(n-1))\n",
      "\n",
      "\n",
      "n=10\n",
      "fact=factorial(n)\n",
      "print \"Its factorial is \",fact\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Its factorial is  3628800\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.15, Page number: 7.26<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Printing Backwards: (recrsive)\n",
      "\n",
      "def reverse(text,n):\n",
      "\n",
      "    if n<0:\n",
      "        return\n",
      "    else:\n",
      "        print text[n],\n",
      "        reverse(text,n-1)\n",
      "\n",
      "\n",
      "\n",
      "text='Now is the time for all good men to come to tje aid of their country!'\n",
      "n=len(text)\n",
      "reverse(text,n-1)\n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "! y r t n u o c   r i e h t   f o   d i a   e j t   o t   e m o c   o t   n e m   d o o g   l l a   r o f   e m i t   e h t   s i   w o N\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.16, Page number: 7.16<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# the Towers of Hanoi - using recursion\n",
      "\n",
      "\n",
      "def transfer(n,From,to,temp):\n",
      "\n",
      "    if n>0:\n",
      "        transfer(n-1,From,temp,to)\n",
      "        print \"Move disk %d from %c to %c\" %(n,From,to)\n",
      "        transfer(n-1,temp,to,From)\n",
      "\n",
      "    return\n",
      "\n",
      "print \"Welcome to the TOWERS OF HANOI \\n\\n\"\n",
      "transfer(3,'L','R','C')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Welcome to the TOWERS OF HANOI \n",
        "\n",
        "\n",
        "Move disk 1 from L to R\n",
        "Move disk 2 from L to C\n",
        "Move disk 1 from R to C\n",
        "Move disk 3 from L to R\n",
        "Move disk 1 from C to L\n",
        "Move disk 2 from C to R\n",
        "Move disk 1 from L to R\n"
       ]
      }
     ],
     "prompt_number": 17
    }
   ],
   "metadata": {}
  }
 ]
}