diff options
Diffstat (limited to 'The_C_Book/Chapter4.ipynb')
-rwxr-xr-x | The_C_Book/Chapter4.ipynb | 648 |
1 files changed, 648 insertions, 0 deletions
diff --git a/The_C_Book/Chapter4.ipynb b/The_C_Book/Chapter4.ipynb new file mode 100755 index 00000000..878ed94c --- /dev/null +++ b/The_C_Book/Chapter4.ipynb @@ -0,0 +1,648 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:619769e887714e6791182739416189f2f7fa6743af97af5b6710a3d8f08e8d46" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Functions" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Example 4.1, page no. 103" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def aax1():\n", + "# code for function body \n", + " return (1.0)\n", + "\n", + "r = aax1();\n", + "print r" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.2 & 4.3, page no. 106-108" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This won't generate any output\n", + "Examples 4.2 and 4.3 are same for python language.\n", + "'''\n", + "\n", + "\n", + "def pmax(a1,a2):\n", + " biggest = 0\n", + " if(a1 > a2):\n", + " biggest = a1\n", + " else:\n", + " biggest = a2\n", + "\n", + "#print \"larger of %d and %d is %d\\n\" %(a1, a2, biggest);\n", + "\n", + "\n", + "\n", + "for i in range(11):\n", + " for j in range(-10,11):\n", + " pmax(i,j)\n", + " \n", + "# It shows that all variables scope is locally only. They can not be accessed from outside its scope. " + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.4, page no. 109" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def sq_root(x):\n", + " DELTA = 0.0001\n", + " curr_appx = 0\n", + " last_appx = 0\n", + " diff = 0\n", + " last_appx = x\n", + " diff = DELTA+1\n", + " while(diff > DELTA):\n", + " curr_appx = 0.5*(last_appx + x/last_appx)\n", + " diff = curr_appx - last_appx\n", + " if(diff < 0):\n", + " diff = -diff\n", + " last_appx = curr_appx\n", + " return(curr_appx)\n", + " \n", + "\n", + "\n", + "for i in range(1,101):\n", + " print \"root of %d is %f\\n\" %( i, sq_root(i))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "root of 1 is 1.000000\n", + "\n", + "root of 2 is 1.414214\n", + "\n", + "root of 3 is 1.732051\n", + "\n", + "root of 4 is 2.000000\n", + "\n", + "root of 5 is 2.236068\n", + "\n", + "root of 6 is 2.449490\n", + "\n", + "root of 7 is 2.645751\n", + "\n", + "root of 8 is 2.828427\n", + "\n", + "root of 9 is 3.000000\n", + "\n", + "root of 10 is 3.162278\n", + "\n", + "root of 11 is 3.316625\n", + "\n", + "root of 12 is 3.464102\n", + "\n", + "root of 13 is 3.605551\n", + "\n", + "root of 14 is 3.741657\n", + "\n", + "root of 15 is 3.872983\n", + "\n", + "root of 16 is 4.000000\n", + "\n", + "root of 17 is 4.123106\n", + "\n", + "root of 18 is 4.242641\n", + "\n", + "root of 19 is 4.358899\n", + "\n", + "root of 20 is 4.472136\n", + "\n", + "root of 21 is 4.582576\n", + "\n", + "root of 22 is 4.690416\n", + "\n", + "root of 23 is 4.795832\n", + "\n", + "root of 24 is 4.898979\n", + "\n", + "root of 25 is 5.000000\n", + "\n", + "root of 26 is 5.099020\n", + "\n", + "root of 27 is 5.196152\n", + "\n", + "root of 28 is 5.291503\n", + "\n", + "root of 29 is 5.385165\n", + "\n", + "root of 30 is 5.477226\n", + "\n", + "root of 31 is 5.567764\n", + "\n", + "root of 32 is 5.656854\n", + "\n", + "root of 33 is 5.744563\n", + "\n", + "root of 34 is 5.830952\n", + "\n", + "root of 35 is 5.916080\n", + "\n", + "root of 36 is 6.000000\n", + "\n", + "root of 37 is 6.082763\n", + "\n", + "root of 38 is 6.164414\n", + "\n", + "root of 39 is 6.244998\n", + "\n", + "root of 40 is 6.324555\n", + "\n", + "root of 41 is 6.403124\n", + "\n", + "root of 42 is 6.480741\n", + "\n", + "root of 43 is 6.557439\n", + "\n", + "root of 44 is 6.633250\n", + "\n", + "root of 45 is 6.708204\n", + "\n", + "root of 46 is 6.782330\n", + "\n", + "root of 47 is 6.855655\n", + "\n", + "root of 48 is 6.928203\n", + "\n", + "root of 49 is 7.000000\n", + "\n", + "root of 50 is 7.071068\n", + "\n", + "root of 51 is 7.141428\n", + "\n", + "root of 52 is 7.211103\n", + "\n", + "root of 53 is 7.280110\n", + "\n", + "root of 54 is 7.348469\n", + "\n", + "root of 55 is 7.416198\n", + "\n", + "root of 56 is 7.483315\n", + "\n", + "root of 57 is 7.549834\n", + "\n", + "root of 58 is 7.615773\n", + "\n", + "root of 59 is 7.681146\n", + "\n", + "root of 60 is 7.745967\n", + "\n", + "root of 61 is 7.810250\n", + "\n", + "root of 62 is 7.874008\n", + "\n", + "root of 63 is 7.937254\n", + "\n", + "root of 64 is 8.000000\n", + "\n", + "root of 65 is 8.062258\n", + "\n", + "root of 66 is 8.124038\n", + "\n", + "root of 67 is 8.185353\n", + "\n", + "root of 68 is 8.246211\n", + "\n", + "root of 69 is 8.306624\n", + "\n", + "root of 70 is 8.366600\n", + "\n", + "root of 71 is 8.426150\n", + "\n", + "root of 72 is 8.485281\n", + "\n", + "root of 73 is 8.544004\n", + "\n", + "root of 74 is 8.602325\n", + "\n", + "root of 75 is 8.660254\n", + "\n", + "root of 76 is 8.717798\n", + "\n", + "root of 77 is 8.774964\n", + "\n", + "root of 78 is 8.831761\n", + "\n", + "root of 79 is 8.888194\n", + "\n", + "root of 80 is 8.944272\n", + "\n", + "root of 81 is 9.000000\n", + "\n", + "root of 82 is 9.055385\n", + "\n", + "root of 83 is 9.110434\n", + "\n", + "root of 84 is 9.165151\n", + "\n", + "root of 85 is 9.219544\n", + "\n", + "root of 86 is 9.273618\n", + "\n", + "root of 87 is 9.327379\n", + "\n", + "root of 88 is 9.380832\n", + "\n", + "root of 89 is 9.433981\n", + "\n", + "root of 90 is 9.486833\n", + "\n", + "root of 91 is 9.539392\n", + "\n", + "root of 92 is 9.591663\n", + "\n", + "root of 93 is 9.643651\n", + "\n", + "root of 94 is 9.695360\n", + "\n", + "root of 95 is 9.746794\n", + "\n", + "root of 96 is 9.797959\n", + "\n", + "root of 97 is 9.848858\n", + "\n", + "root of 98 is 9.899495\n", + "\n", + "root of 99 is 9.949874\n", + "\n", + "root of 100 is 10.000000\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.6, page no. 115" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def called_func(a,b):\n", + " temp = a * b\n", + " print temp\n", + "\n", + "called_func(1,2*3.5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "7.0\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.7, page no. 116" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def changer(x):\n", + " while(x!=0):\n", + " print \"changer: x=%d\\n\" %(x)\n", + " x-=1\n", + "\n", + "i = 5\n", + "print \"before i=%d\\n\" %( i);\n", + "changer(i);\n", + "print \"after i=%d\\n\" %( i);" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "before i=5\n", + "\n", + "changer: x=5\n", + "\n", + "changer: x=4\n", + "\n", + "changer: x=3\n", + "\n", + "changer: x=2\n", + "\n", + "changer: x=1\n", + "\n", + "after i=5\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.8, page no. 120" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Do not try to run....\n", + "It's infinite looping program..\n", + "\n", + "This program shows how a function calls itself.\n", + "'''\n", + "\n", + "def expr():\n", + " val = 0\n", + " ch_in = 0\n", + " val = mul_exp()\n", + " while True:\n", + " ch_in = raw_input(\"Enter + or - \")\n", + " if ch_in =='+':\n", + " val = val + mul_exp();\n", + " else:\n", + " if ch_in == '-':\n", + " val = val - mul_exp()\n", + " else:\n", + " return val\n", + "\n", + " return val\n", + "\n", + "def mul_exp():\n", + " val = 0\n", + " ch_in = 0\n", + " val = unary_exp()\n", + " while True:\n", + " ch_in = raw_input(\"Enter * or / or % \")\n", + " if ch_in =='*':\n", + " val = val * unary_exp();\n", + " else:\n", + " if ch_in == '/':\n", + " val = val - mul_exp()\n", + " else:\n", + " if ch_in =='%':\n", + " val = val % unary_exp()\n", + " else:\n", + " return val\n", + " return val\n", + "\n", + "def unary_exp():\n", + " val=0\n", + " ch_in = raw_input(\"Enter + or - \")\n", + " if ch_in =='+':\n", + " val = unary_exp()\n", + " else:\n", + " if ch_in == '-':\n", + " val = -unary_exp()\n", + " else:\n", + " val = primary()\n", + " return val\n", + "\n", + " return val\n", + "\n", + "def primary():\n", + " val = 0\n", + " ch_in = raw_input('Enter value')\n", + " if (ch_in >= '0' and ch_in <= '9'):\n", + " val = ch_in - '0'\n", + " return val\n", + " if ch_in == '(':\n", + " val = expr()\n", + " print \"error: primary read %d\\n\" %ch_in\n", + " return val\n", + "\n", + "\n", + "val = 0\n", + "while True:\n", + " print \"expression: \"\n", + " val = expr();\n", + " if(raw_input()!= '\\n'):\n", + " print \"error\\n\"\n", + " while(raw_input() != '\\n'):\n", + " pass\n", + " \n", + " else:\n", + " print \"result is %d\\n\" %val" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.9, page no. 123" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#No output will be produced\n", + "\n", + "from second import * #importing all content of second.py file\n", + "\n", + "\n", + "i=0\n", + "print f_in_other_place(i) # declaration " + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.10, page no. 124" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Example 4.10\n", + "/* example library module */\n", + "/* only 'callable' is visible outside */\n", + "'''\n", + "\n", + "\n", + "__buf=[]\n", + "__length = 0\n", + "\n", + "def __fillup__():\n", + " while (length <100):\n", + " buf[length] = 0\n", + " length += 1\n", + "\n", + "def callable ():\n", + " if (length ==0):\n", + " fillup ()\n", + " a = buf[length]\n", + " length-=1\n", + " return (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.11, page no. 126" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "python doesn't have static variable \n", + "we can use global variable for counting \n", + "this demonstrates how it works...\n", + "'''\n", + "\n", + "count = 0\n", + "\n", + "def small_val ():\n", + " count+=1\n", + " return (count % 16)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.12, page no. 126" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Example 4.12\n", + "python doesn't have static variables.\n", + "'''\n", + "\n", + "\n", + "depth=0\n", + "def r_func():\n", + " depth+=1\n", + " if (depth > 200):\n", + " print (\"excessive recursion\\n\")\n", + " sys.exit(0)\t\n", + " else:\n", + " '''/* do usual thing,\n", + " * not shown here.\n", + " * This last action\n", + " * occasionally results in another\n", + " * call on r_func()\n", + " */\n", + " '''\n", + " x_func()\n", + "\n", + " depth-=1" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |