diff options
Diffstat (limited to '_Programming_With_C/chapter11.ipynb')
-rw-r--r-- | _Programming_With_C/chapter11.ipynb | 745 |
1 files changed, 697 insertions, 48 deletions
diff --git a/_Programming_With_C/chapter11.ipynb b/_Programming_With_C/chapter11.ipynb index 1f4127e4..d858cd07 100644 --- a/_Programming_With_C/chapter11.ipynb +++ b/_Programming_With_C/chapter11.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "chapter11" + "name": "", + "signature": "sha256:81f953770b0acac35782d6d61b5968cf0c7681b2e047ca75cfe8abd7244d5bab" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,24 +11,49 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h1>Chapter 11: Pointers<h1>" + "source": [ + "<h1>Chapter 11: Pointers<h1>" + ] }, { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.1, Page 11.2<h3>" + "source": [ + "<h3>Example 11.1, Page 11.2<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# realtionship between two variables, their address and associated pointers\n\n\nu,v=[3],[]\npu=u\nv=pu\npv=v\n\nprint \"u=\",u[0],\nprint \"&u=\",id(u),\nprint \"pu=\",id(u),\nprint \"*pu\",pu[0]\n\nprint \"v=\",v[0],\nprint \"&v=\",id(v),\nprint \"pv=\",id(v),\nprint \"*pv\",pv[0]\n", + "input": [ + "\n", + "\n", + "\n", + "u,v=[3],[]\n", + "pu=u\n", + "v=pu\n", + "pv=v\n", + "\n", + "print \"u=\",u[0],\n", + "print \"&u=\",id(u),\n", + "print \"pu=\",id(u),\n", + "print \"*pu\",pu[0]\n", + "\n", + "print \"v=\",v[0],\n", + "print \"&v=\",id(v),\n", + "print \"pv=\",id(v),\n", + "print \"*pv\",pv[0]\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "u= 3 &u= 56575840 pu= 56575840 *pu 3\nv= 3 &v= 56575840 pv= 56575840 *pv 3\n" + "text": [ + "u= 3 &u= 56575840 pu= 56575840 *pu 3\n", + "v= 3 &v= 56575840 pv= 56575840 *pv 3\n" + ] } ], "prompt_number": 11 @@ -35,19 +61,32 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.2, Page number: 11.3<h3>" + "source": [ + "<h3>Example 11.2, Page number: 11.3<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# a simple program using pointers\n\n\nv=[3]\nu1=2*(v[0]+5)\npv=v\nu2=2*(pv[0]+5)\n\nprint \"u1=%d u2=%d\" %(u1,u2)", + "input": [ + "\n", + "\n", + "v=[3]\n", + "u1=2*(v[0]+5)\n", + "pv=v\n", + "u2=2*(pv[0]+5)\n", + "\n", + "print \"u1=%d u2=%d\" %(u1,u2)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "u1=16 u2=16\n" + "text": [ + "u1=16 u2=16\n" + ] } ], "prompt_number": 12 @@ -55,19 +94,32 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.3, Page munber: 11.4<h3>" + "source": [ + "<h3>Example 11.3, Page munber: 11.4<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# a simple programme using pointers\n\nv=[3]\npv=v\nprint \"*pv=%d v=%d\" %(pv[0],v[0])\n\npv[0]=0\nprint \"*pv=%d v=%d\" %(pv[0],v[0])", + "input": [ + "\n", + "v=[3]\n", + "pv=v\n", + "print \"*pv=%d v=%d\" %(pv[0],v[0])\n", + "\n", + "pv[0]=0\n", + "print \"*pv=%d v=%d\" %(pv[0],v[0])" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "*pv=3 v=3\n*pv=0 v=0\n" + "text": [ + "*pv=3 v=3\n", + "*pv=0 v=0\n" + ] } ], "prompt_number": 13 @@ -75,19 +127,53 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.7, Page number: 11.6<h3>" + "source": [ + "<h3>Example 11.7, Page number: 11.6<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# difference between by pass by reference and pass by value\n\n\ndef funct1(u,v):\n u=0\n v=0\n print \"Within funct1 : u=%d v=%d\" %(u,v)\n return\n\ndef funct2(u,v):\n u[0]=0\n v[0]=0\n print \"Within funct2 : *pu=%d *pv=%d\" %(u[0],v[0])\n return\n\nu=[1]\nv=[3]\n\nprint \"Before calling funct1: u=%d v=%d\" %(u[0],v[0])\nfunct1(u[0],v[0])\nprint \"After calling funct1 : u=%d v=%d\" %(u[0],v[0])\n\nprint \"Before calling funct2: u=%d v=%d\" %(u[0],v[0])\nfunct2(u,v)\nprint \"After calling funct2 : u=%d v=%d\" %(u[0],v[0])", + "input": [ + "\n", + "\n", + "def funct1(u,v):\n", + " u=0\n", + " v=0\n", + " print \"Within funct1 : u=%d v=%d\" %(u,v)\n", + " return\n", + "\n", + "def funct2(u,v):\n", + " u[0]=0\n", + " v[0]=0\n", + " print \"Within funct2 : *pu=%d *pv=%d\" %(u[0],v[0])\n", + " return\n", + "\n", + "u=[1]\n", + "v=[3]\n", + "\n", + "print \"Before calling funct1: u=%d v=%d\" %(u[0],v[0])\n", + "funct1(u[0],v[0])\n", + "print \"After calling funct1 : u=%d v=%d\" %(u[0],v[0])\n", + "\n", + "print \"Before calling funct2: u=%d v=%d\" %(u[0],v[0])\n", + "funct2(u,v)\n", + "print \"After calling funct2 : u=%d v=%d\" %(u[0],v[0])" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Before calling funct1: u=1 v=3\nWithin funct1 : u=0 v=0\nAfter calling funct1 : u=1 v=3\nBefore calling funct2: u=1 v=3\nWithin funct2 : *pu=0 *pv=0\nAfter calling funct2 : u=0 v=0\n" + "text": [ + "Before calling funct1: u=1 v=3\n", + "Within funct1 : u=0 v=0\n", + "After calling funct1 : u=1 v=3\n", + "Before calling funct2: u=1 v=3\n", + "Within funct2 : *pu=0 *pv=0\n", + "After calling funct2 : u=0 v=0\n" + ] } ], "prompt_number": 14 @@ -95,19 +181,62 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.8, Page number: 11.8<h3>" + "source": [ + "<h3>Example 11.8, Page number: 11.8<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Analyzing a line of text\n\n\n\ndef scan_line(line,pv,pc,pd,pw,po):\n\n for c in line:\n\n if c=='A' or c=='E' or c=='I' or c=='O' or c=='U':\n pv[0]+=1\n elif c>='A' and c<='Z':\n pc[0]+=1\n elif c>='0' and c<='9':\n pd[0]+=1\n elif c==' ' or c=='\\t':\n pw[0]+=1\n else:\n po[0]+=1\n\n return\n\n\nvowel,consonants,digits,whitespc,other=[0],[0],[0],[0],[0]\nline=\"Personal computers with memories in excess of 4096 KB are now quite common.\"\nline=line.upper()\nscan_line(line,vowel,consonants,digits,whitespc,other)\nprint \"\\n\\n\"\nprint \"No. of vowels : \",vowel[0]\nprint \"No. of consonants : \",consonants[0]\nprint \"No. of digits : \",digits[0]\nprint \"No. of whitespace characters : \",whitespc[0]\nprint \"No. of other characters : \",other[0]", + "input": [ + "\n", + "\n", + "\n", + "def scan_line(line,pv,pc,pd,pw,po):\n", + "\n", + " for c in line:\n", + "\n", + " if c=='A' or c=='E' or c=='I' or c=='O' or c=='U':\n", + " pv[0]+=1\n", + " elif c>='A' and c<='Z':\n", + " pc[0]+=1\n", + " elif c>='0' and c<='9':\n", + " pd[0]+=1\n", + " elif c==' ' or c=='\\t':\n", + " pw[0]+=1\n", + " else:\n", + " po[0]+=1\n", + "\n", + " return\n", + "\n", + "\n", + "vowel,consonants,digits,whitespc,other=[0],[0],[0],[0],[0]\n", + "line=\"Personal computers with memories in excess of 4096 KB are now quite common.\"\n", + "line=line.upper()\n", + "scan_line(line,vowel,consonants,digits,whitespc,other)\n", + "print \"\\n\\n\"\n", + "print \"No. of vowels : \",vowel[0]\n", + "print \"No. of consonants : \",consonants[0]\n", + "print \"No. of digits : \",digits[0]\n", + "print \"No. of whitespace characters : \",whitespc[0]\n", + "print \"No. of other characters : \",other[0]" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "\n\n\nNo. of vowels : 23\nNo. of consonants : 35\nNo. of digits : 4\nNo. of whitespace characters : 12\nNo. of other characters : 1\n" + "text": [ + "\n", + "\n", + "\n", + "No. of vowels : 23\n", + "No. of consonants : 35\n", + "No. of digits : 4\n", + "No. of whitespace characters : 12\n", + "No. of other characters : 1\n" + ] } ], "prompt_number": 15 @@ -115,19 +244,40 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.12, Page number: 11.15<h3>" + "source": [ + "<h3>Example 11.12, Page number: 11.15<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# unlike C in python the elements of a list are not arranged in memory uniformly\n\nx=[10,11,12,13,14,15,16,17,18,19]\n\nfor i in range(0,10):\n print \"i=%d x[i]=%d *(x+1)=%d\" %(i,x[i],x[i]),\n print \" &x[i]=\",id(x[i]),\n print \" x+i=\",id(x[i])", + "input": [ + "\n", + "x=[10,11,12,13,14,15,16,17,18,19]\n", + "\n", + "for i in range(0,10):\n", + " print \"i=%d x[i]=%d *(x+1)=%d\" %(i,x[i],x[i]),\n", + " print \" &x[i]=\",id(x[i]),\n", + " print \" x+i=\",id(x[i])" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "i=0 x[i]=10 *(x+1)=10 &x[i]= 30696620 x+i= 30696620\ni=1 x[i]=11 *(x+1)=11 &x[i]= 30696608 x+i= 30696608\ni=2 x[i]=12 *(x+1)=12 &x[i]= 30696596 x+i= 30696596\ni=3 x[i]=13 *(x+1)=13 &x[i]= 30696584 x+i= 30696584\ni=4 x[i]=14 *(x+1)=14 &x[i]= 30696572 x+i= 30696572\ni=5 x[i]=15 *(x+1)=15 &x[i]= 30696560 x+i= 30696560\ni=6 x[i]=16 *(x+1)=16 &x[i]= 30696548 x+i= 30696548\ni=7 x[i]=17 *(x+1)=17 &x[i]= 30696536 x+i= 30696536\ni=8 x[i]=18 *(x+1)=18 &x[i]= 30696524 x+i= 30696524\ni=9 x[i]=19 *(x+1)=19 &x[i]= 30696512 x+i= 30696512\n" + "text": [ + "i=0 x[i]=10 *(x+1)=10 &x[i]= 30696620 x+i= 30696620\n", + "i=1 x[i]=11 *(x+1)=11 &x[i]= 30696608 x+i= 30696608\n", + "i=2 x[i]=12 *(x+1)=12 &x[i]= 30696596 x+i= 30696596\n", + "i=3 x[i]=13 *(x+1)=13 &x[i]= 30696584 x+i= 30696584\n", + "i=4 x[i]=14 *(x+1)=14 &x[i]= 30696572 x+i= 30696572\n", + "i=5 x[i]=15 *(x+1)=15 &x[i]= 30696560 x+i= 30696560\n", + "i=6 x[i]=16 *(x+1)=16 &x[i]= 30696548 x+i= 30696548\n", + "i=7 x[i]=17 *(x+1)=17 &x[i]= 30696536 x+i= 30696536\n", + "i=8 x[i]=18 *(x+1)=18 &x[i]= 30696524 x+i= 30696524\n", + "i=9 x[i]=19 *(x+1)=19 &x[i]= 30696512 x+i= 30696512\n" + ] } ], "prompt_number": 16 @@ -135,19 +285,38 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.14, Page number: 11.17<h3>" + "source": [ + "<h3>Example 11.14, Page number: 11.17<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# string as one dimensional character array\n\ndef main():\n global x\n y=\"This string is declared within main\"\n y=list(y)\n x=list(x)\n print x\n print y\n\n return\n\nx=\"This string is declared externally\"\nmain()\n", + "input": [ + "\n", + "def main():\n", + " global x\n", + " y=\"This string is declared within main\"\n", + " y=list(y)\n", + " x=list(x)\n", + " print x\n", + " print y\n", + "\n", + " return\n", + "\n", + "x=\"This string is declared externally\"\n", + "main()\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'e', 'x', 't', 'e', 'r', 'n', 'a', 'l', 'l', 'y']\n['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'w', 'i', 't', 'h', 'i', 'n', ' ', 'm', 'a', 'i', 'n']\n" + "text": [ + "['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'e', 'x', 't', 'e', 'r', 'n', 'a', 'l', 'l', 'y']\n", + "['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'w', 'i', 't', 'h', 'i', 'n', ' ', 'm', 'a', 'i', 'n']\n" + ] } ], "prompt_number": 17 @@ -155,19 +324,71 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.16, Page number: 11.19<h3>" + "source": [ + "<h3>Example 11.16, Page number: 11.19<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Reoedering a list of numbers\n\n\ndef reorder(x):\n n=len(x)\n for item in range(0,n-1):\n for i in range(item+1,n):\n if x[i]<x[item]:\n temp=x[item]\n x[item]=x[i]\n x[i]=temp\n\n return\n\nn=10\nx=[]\n\nfor i in range(0,n):\n inp=i+1\n print \"\\ni=%d x=%d\" %(i+1,inp),\n x.append(inp)\n\nreorder(x)\n\nprint \"\\n\"\nfor i in range(0,n):\n print \"i=%d x=%d\" %(i+1,x[i])\n", + "input": [ + "\n", + "\n", + "def reorder(x):\n", + " n=len(x)\n", + " for item in range(0,n-1):\n", + " for i in range(item+1,n):\n", + " if x[i]<x[item]:\n", + " temp=x[item]\n", + " x[item]=x[i]\n", + " x[i]=temp\n", + "\n", + " return\n", + "\n", + "n=10\n", + "x=[]\n", + "\n", + "for i in range(0,n):\n", + " inp=i+1\n", + " print \"\\ni=%d x=%d\" %(i+1,inp),\n", + " x.append(inp)\n", + "\n", + "reorder(x)\n", + "\n", + "print \"\\n\"\n", + "for i in range(0,n):\n", + " print \"i=%d x=%d\" %(i+1,x[i])\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "\ni=1 x=1 \ni=2 x=2 \ni=3 x=3 \ni=4 x=4 \ni=5 x=5 \ni=6 x=6 \ni=7 x=7 \ni=8 x=8 \ni=9 x=9 \ni=10 x=10 \n\ni=1 x=1\ni=2 x=2\ni=3 x=3\ni=4 x=4\ni=5 x=5\ni=6 x=6\ni=7 x=7\ni=8 x=8\ni=9 x=9\ni=10 x=10\n" + "text": [ + "\n", + "i=1 x=1 \n", + "i=2 x=2 \n", + "i=3 x=3 \n", + "i=4 x=4 \n", + "i=5 x=5 \n", + "i=6 x=6 \n", + "i=7 x=7 \n", + "i=8 x=8 \n", + "i=9 x=9 \n", + "i=10 x=10 \n", + "\n", + "i=1 x=1\n", + "i=2 x=2\n", + "i=3 x=3\n", + "i=4 x=4\n", + "i=5 x=5\n", + "i=6 x=6\n", + "i=7 x=7\n", + "i=8 x=8\n", + "i=9 x=9\n", + "i=10 x=10\n" + ] } ], "prompt_number": 18 @@ -175,19 +396,40 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.17, Page number: 11.21<h3>" + "source": [ + "<h3>Example 11.17, Page number: 11.21<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Pointer arithmetic doesnt exixt in python\n\ni=[4]\ni[0]=1\nf=0.3\nd=0.005\nc='*'\n\npx=i\nprint 'Values: i=%d f=%f d=%f c=%c \\n' %(i[0],f,d,c)\nprint 'Addresses: &i=%X &f=%X &d=%X &c=%X \\n' %(id(i[0]),id(f),id(d),id(c))\nprint 'Pointer Values: px=%X px+1=%X px+2=%X px+3=%X' %(id(px[0]),id(px[0]+1),id(px[0]+2),id(px[0]+3))\n\n", + "input": [ + "\n", + "i=[4]\n", + "i[0]=1\n", + "f=0.3\n", + "d=0.005\n", + "c='*'\n", + "\n", + "px=i\n", + "print 'Values: i=%d f=%f d=%f c=%c \\n' %(i[0],f,d,c)\n", + "print 'Addresses: &i=%X &f=%X &d=%X &c=%X \\n' %(id(i[0]),id(f),id(d),id(c))\n", + "print 'Pointer Values: px=%X px+1=%X px+2=%X px+3=%X' %(id(px[0]),id(px[0]+1),id(px[0]+2),id(px[0]+3))\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Values: i=1 f=0.300000 d=0.005000 c=* \n\nAddresses: &i=1D46518 &f=32BD940 &d=32BD950 &c=1D9F158 \n\nPointer Values: px=1D46518 px+1=1D4650C px+2=1D46500 px+3=1D464F4\n" + "text": [ + "Values: i=1 f=0.300000 d=0.005000 c=* \n", + "\n", + "Addresses: &i=1D46518 &f=32BD940 &d=32BD950 &c=1D9F158 \n", + "\n", + "Pointer Values: px=1D46518 px+1=1D4650C px+2=1D46500 px+3=1D464F4\n" + ] } ], "prompt_number": 19 @@ -195,19 +437,34 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.18, Page number: 11.22<h3>" + "source": [ + "<h3>Example 11.18, Page number: 11.22<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# two different pointer variable points to the first and last element of an integer array\n\na=[1,2,3,4,5,6]\n\npx=a[0]\npy=a[5]\n\nprint 'px=%X py=%X \\n' %(id(px),id(py))\nprint 'py - px = %X' %(id(py)-id(px))", + "input": [ + "\n", + "a=[1,2,3,4,5,6]\n", + "\n", + "px=a[0]\n", + "py=a[5]\n", + "\n", + "print 'px=%X py=%X \\n' %(id(px),id(py))\n", + "print 'py - px = %X' %(id(py)-id(px))" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "px=1D46518 py=1D464DC \n\npy - px = -3C\n" + "text": [ + "px=1D46518 py=1D464DC \n", + "\n", + "py - px = -3C\n" + ] } ], "prompt_number": 20 @@ -215,19 +472,96 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.22, Page number: 11.26<h3>" + "source": [ + "<h3>Example 11.22, Page number: 11.26<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# adding two tables of numbers\n\ndef readinput(m,n,i=0):\n\n at=[]\n for row in range(0,m):\n temp=[]\n for col in range(0,n):\n t=i\n i+=2\n temp.append(t)\n at.append(temp)\n\n\n return at\n \ndef computesum(a,b,m,n):\n\n c=[]\n\n for row in range(0,m):\n temp=[]\n for col in range(0,n):\n t=a[row][col]+b[row][col]\n temp.append(t)\n c.append(temp)\n\n return c\n\ndef writeoutput(c,m,n):\n\n for row in range(0,m):\n for col in range(0,n):\n print \"%4d\" %(c[row][col]),\n print\n\n return\n\n\n\nprint \"\\n FIRST TABLE : \\n\"\na=readinput(5,5,1)\nwriteoutput(a,5,5)\n\nprint \"\\n SECOND TABLE : \\n\"\nb=readinput(5,5,50)\nwriteoutput(b,5,5)\n\nc=computesum(a,b,5,5)\nprint \"Sums of the elements : \\n\"\nwriteoutput(c,5,5)\n", + "input": [ + "\n", + "def readinput(m,n,i=0):\n", + "\n", + " at=[]\n", + " for row in range(0,m):\n", + " temp=[]\n", + " for col in range(0,n):\n", + " t=i\n", + " i+=2\n", + " temp.append(t)\n", + " at.append(temp)\n", + "\n", + "\n", + " return at\n", + " \n", + "def computesum(a,b,m,n):\n", + "\n", + " c=[]\n", + "\n", + " for row in range(0,m):\n", + " temp=[]\n", + " for col in range(0,n):\n", + " t=a[row][col]+b[row][col]\n", + " temp.append(t)\n", + " c.append(temp)\n", + "\n", + " return c\n", + "\n", + "def writeoutput(c,m,n):\n", + "\n", + " for row in range(0,m):\n", + " for col in range(0,n):\n", + " print \"%4d\" %(c[row][col]),\n", + " print\n", + "\n", + " return\n", + "\n", + "\n", + "\n", + "print \"\\n FIRST TABLE : \\n\"\n", + "a=readinput(5,5,1)\n", + "writeoutput(a,5,5)\n", + "\n", + "print \"\\n SECOND TABLE : \\n\"\n", + "b=readinput(5,5,50)\n", + "writeoutput(b,5,5)\n", + "\n", + "c=computesum(a,b,5,5)\n", + "print \"Sums of the elements : \\n\"\n", + "writeoutput(c,5,5)\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "\n FIRST TABLE : \n\n 1 3 5 7 9\n 11 13 15 17 19\n 21 23 25 27 29\n 31 33 35 37 39\n 41 43 45 47 49\n\n SECOND TABLE : \n\n 50 52 54 56 58\n 60 62 64 66 68\n 70 72 74 76 78\n 80 82 84 86 88\n 90 92 94 96 98\nSums of the elements : \n\n 51 55 59 63 67\n 71 75 79 83 87\n 91 95 99 103 107\n 111 115 119 123 127\n 131 135 139 143 147\n" + "text": [ + "\n", + " FIRST TABLE : \n", + "\n", + " 1 3 5 7 9\n", + " 11 13 15 17 19\n", + " 21 23 25 27 29\n", + " 31 33 35 37 39\n", + " 41 43 45 47 49\n", + "\n", + " SECOND TABLE : \n", + "\n", + " 50 52 54 56 58\n", + " 60 62 64 66 68\n", + " 70 72 74 76 78\n", + " 80 82 84 86 88\n", + " 90 92 94 96 98\n", + "Sums of the elements : \n", + "\n", + " 51 55 59 63 67\n", + " 71 75 79 83 87\n", + " 91 95 99 103 107\n", + " 111 115 119 123 127\n", + " 131 135 139 143 147\n" + ] } ], "prompt_number": 21 @@ -235,19 +569,96 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.24, Page number: 11.31<h3>" + "source": [ + "<h3>Example 11.24, Page number: 11.31<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# adding two tables of numbers\n\ndef readinput(m,n,i=0):\n\n at=[]\n for row in range(0,m):\n temp=[]\n for col in range(0,n):\n t=i\n i+=2\n temp.append(t)\n at.append(temp)\n\n\n return at\n \ndef computesum(a,b,m,n):\n\n c=[]\n\n for row in range(0,m):\n temp=[]\n for col in range(0,n):\n t=a[row][col]+b[row][col]\n temp.append(t)\n c.append(temp)\n\n return c\n\ndef writeoutput(c,m,n):\n\n for row in range(0,m):\n for col in range(0,n):\n print \"%4d\" %(c[row][col]),\n print\n\n return\n\n\n\nprint \"\\n FIRST TABLE : \\n\"\na=readinput(5,5,1)\nwriteoutput(a,5,5)\n\nprint \"\\n SECOND TABLE : \\n\"\nb=readinput(5,5,50)\nwriteoutput(b,5,5)\n\nc=computesum(a,b,5,5)\nprint \"Sums of the elements : \\n\"\nwriteoutput(c,5,5)\n", + "input": [ + "\n", + "def readinput(m,n,i=0):\n", + "\n", + " at=[]\n", + " for row in range(0,m):\n", + " temp=[]\n", + " for col in range(0,n):\n", + " t=i\n", + " i+=2\n", + " temp.append(t)\n", + " at.append(temp)\n", + "\n", + "\n", + " return at\n", + " \n", + "def computesum(a,b,m,n):\n", + "\n", + " c=[]\n", + "\n", + " for row in range(0,m):\n", + " temp=[]\n", + " for col in range(0,n):\n", + " t=a[row][col]+b[row][col]\n", + " temp.append(t)\n", + " c.append(temp)\n", + "\n", + " return c\n", + "\n", + "def writeoutput(c,m,n):\n", + "\n", + " for row in range(0,m):\n", + " for col in range(0,n):\n", + " print \"%4d\" %(c[row][col]),\n", + " print\n", + "\n", + " return\n", + "\n", + "\n", + "\n", + "print \"\\n FIRST TABLE : \\n\"\n", + "a=readinput(5,5,1)\n", + "writeoutput(a,5,5)\n", + "\n", + "print \"\\n SECOND TABLE : \\n\"\n", + "b=readinput(5,5,50)\n", + "writeoutput(b,5,5)\n", + "\n", + "c=computesum(a,b,5,5)\n", + "print \"Sums of the elements : \\n\"\n", + "writeoutput(c,5,5)\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "\n FIRST TABLE : \n\n 1 3 5 7 9\n 11 13 15 17 19\n 21 23 25 27 29\n 31 33 35 37 39\n 41 43 45 47 49\n\n SECOND TABLE : \n\n 50 52 54 56 58\n 60 62 64 66 68\n 70 72 74 76 78\n 80 82 84 86 88\n 90 92 94 96 98\nSums of the elements : \n\n 51 55 59 63 67\n 71 75 79 83 87\n 91 95 99 103 107\n 111 115 119 123 127\n 131 135 139 143 147\n" + "text": [ + "\n", + " FIRST TABLE : \n", + "\n", + " 1 3 5 7 9\n", + " 11 13 15 17 19\n", + " 21 23 25 27 29\n", + " 31 33 35 37 39\n", + " 41 43 45 47 49\n", + "\n", + " SECOND TABLE : \n", + "\n", + " 50 52 54 56 58\n", + " 60 62 64 66 68\n", + " 70 72 74 76 78\n", + " 80 82 84 86 88\n", + " 90 92 94 96 98\n", + "Sums of the elements : \n", + "\n", + " 51 55 59 63 67\n", + " 71 75 79 83 87\n", + " 91 95 99 103 107\n", + " 111 115 119 123 127\n", + " 131 135 139 143 147\n" + ] } ], "prompt_number": 22 @@ -255,19 +666,77 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.26, Page number: 11.34<h3>" + "source": [ + "<h3>Example 11.26, Page number: 11.34<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Reordering a list of strings\n\n\ndef reorder(x):\n\n n=len(x)\n for item in range(0,n-1):\n for i in range(item+1,n):\n if x[item]>x[i]:\n temp=x[item]\n x[item]=x[i]\n x[i]=temp\n\n\n return\n\nx=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\nprint 'Original list of strings :\\n\\n'\n\nfor i in x:\n print \"String : \",i\n\nreorder(x)\n\nprint \"\\nReodered list of strings : \\n\\n\"\n\nfor i in x:\n print \"String : \",i", + "input": [ + "\n", + "\n", + "def reorder(x):\n", + "\n", + " n=len(x)\n", + " for item in range(0,n-1):\n", + " for i in range(item+1,n):\n", + " if x[item]>x[i]:\n", + " temp=x[item]\n", + " x[item]=x[i]\n", + " x[i]=temp\n", + "\n", + "\n", + " return\n", + "\n", + "x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\n", + "print 'Original list of strings :\\n\\n'\n", + "\n", + "for i in x:\n", + " print \"String : \",i\n", + "\n", + "reorder(x)\n", + "\n", + "print \"\\nReodered list of strings : \\n\\n\"\n", + "\n", + "for i in x:\n", + " print \"String : \",i" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Original list of strings :\n\n\nString : PACIFIC\nString : ATLANTIC\nString : INDIAN\nString : CARIBBEAN\nString : BERING\nString : BLACK\nString : RED\nString : NORTH\nString : BALTIC\nString : CASPIAN\n\nReodered list of strings : \n\n\nString : ATLANTIC\nString : BALTIC\nString : BERING\nString : BLACK\nString : CARIBBEAN\nString : CASPIAN\nString : INDIAN\nString : NORTH\nString : PACIFIC\nString : RED\n" + "text": [ + "Original list of strings :\n", + "\n", + "\n", + "String : PACIFIC\n", + "String : ATLANTIC\n", + "String : INDIAN\n", + "String : CARIBBEAN\n", + "String : BERING\n", + "String : BLACK\n", + "String : RED\n", + "String : NORTH\n", + "String : BALTIC\n", + "String : CASPIAN\n", + "\n", + "Reodered list of strings : \n", + "\n", + "\n", + "String : ATLANTIC\n", + "String : BALTIC\n", + "String : BERING\n", + "String : BLACK\n", + "String : CARIBBEAN\n", + "String : CASPIAN\n", + "String : INDIAN\n", + "String : NORTH\n", + "String : PACIFIC\n", + "String : RED\n" + ] } ], "prompt_number": 23 @@ -275,19 +744,79 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.28, Page number: 11.37<h3>" + "source": [ + "<h3>Example 11.28, Page number: 11.37<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Display the day of the year\n\n\ndef convert(mm,dd,yy):\n yy-=1900\n ndays=long(30.42*(mm-1)+dd)\n if mm==2:\n ndays+=1\n if mm>2 and mm<8:\n ndays-=1\n if yy%4==0 and mm<2:\n ndays+=1\n\n ncycles=yy/4\n ndays+=ncycles*1461\n\n nyears=yy%4\n if nyears>0:\n ndays+=365*nyears+1\n if ndays>59:\n ndays-=1\n\n day=ndays%7\n\n return day\n\ndef main(mm,dd,yy):\n day_of_week=convert(mm,dd,yy)\n print \"%s, %s %d %d\" %(weekday[day_of_week],month[mm-1],dd,yy)\n return\n \nweekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']\nmonth=['January','February','March','April','May','June','July','August',\\\n 'September','October','November','December']\n\nmain(10,29,1929)\nprint\nmain(8,15,1945)\nprint\nmain(7,20,1969)\nprint\nmain(5,24,1997)\nprint\nmain(8,30,2010)\nprint\nmain(4,12,2069)\n", + "input": [ + "\n", + "\n", + "def convert(mm,dd,yy):\n", + " yy-=1900\n", + " ndays=long(30.42*(mm-1)+dd)\n", + " if mm==2:\n", + " ndays+=1\n", + " if mm>2 and mm<8:\n", + " ndays-=1\n", + " if yy%4==0 and mm<2:\n", + " ndays+=1\n", + "\n", + " ncycles=yy/4\n", + " ndays+=ncycles*1461\n", + "\n", + " nyears=yy%4\n", + " if nyears>0:\n", + " ndays+=365*nyears+1\n", + " if ndays>59:\n", + " ndays-=1\n", + "\n", + " day=ndays%7\n", + "\n", + " return day\n", + "\n", + "def main(mm,dd,yy):\n", + " day_of_week=convert(mm,dd,yy)\n", + " print \"%s, %s %d %d\" %(weekday[day_of_week],month[mm-1],dd,yy)\n", + " return\n", + " \n", + "weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']\n", + "month=['January','February','March','April','May','June','July','August',\\\n", + " 'September','October','November','December']\n", + "\n", + "main(10,29,1929)\n", + "print\n", + "main(8,15,1945)\n", + "print\n", + "main(7,20,1969)\n", + "print\n", + "main(5,24,1997)\n", + "print\n", + "main(8,30,2010)\n", + "print\n", + "main(4,12,2069)\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Tuesday, October 29 1929\n\nWednesday, August 15 1945\n\nSunday, July 20 1969\n\nSaturday, May 24 1997\n\nMonday, August 30 2010\n\nFriday, April 12 2069\n" + "text": [ + "Tuesday, October 29 1929\n", + "\n", + "Wednesday, August 15 1945\n", + "\n", + "Sunday, July 20 1969\n", + "\n", + "Saturday, May 24 1997\n", + "\n", + "Monday, August 30 2010\n", + "\n", + "Friday, April 12 2069\n" + ] } ], "prompt_number": 24 @@ -295,19 +824,139 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 11.30, Page number: 11.44<h3>" + "source": [ + "<h3>Example 11.30, Page number: 11.44<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Future value of monthly deposits (compound interest calculations)\n\n\ndef ratio_choose(freq,a,m,n):\n\n if freq=='C':\n ratio=md3(a,m,n)\n elif freq=='D':\n ratio=md2(a,m,n)\n else:\n ratio=md1(a,m,n)\n\n return ratio\n\n\ndef table(freq,a,m,n):\n\n\n print \"Interest Rate Future Amount\\n\\n\"\n for count in range(1,21):\n i=0.01*count\n f=a*ratio_choose(freq,i,m,n)\n print \" %2d %.2f\" %(count,f)\n\n return\n\ndef md1(i,m,n):\n \n factor=1+i/m\n ratio=12*(factor**(m*n)-1)/i\n return ratio\n\ndef md2(i,m,n):\n\n factor=1+i/m\n ratio=(factor**(m*n)-1)/(factor**(m/12)-1)\n return ratio\n\ndef md3(i,dummy,n):\n ratio=(10**(i*n)-1)/(10**(i/12)-1)\n return ratio\n\ndef main(freq):\n m=0\n freq=freq.upper()\n if freq=='A':\n m=1\n print \"\\nAnual Compounding\\n\"\n \n elif freq=='S':\n m=2\n print \"\\nSemiannual Compounding\\n\"\n \n elif freq=='Q':\n print \"\\nQuaterly Compounding\\n\"\n m=4\n \n elif freq=='M':\n m=12\n print \"\\nMonthly Compounding\\n\"\n \n elif freq=='D':\n m=360\n print \"\\nDaily Compounding\\n\"\n\n elif freq=='C':\n m=0\n print \"\\nContinuous Compounding\\n\"\n \n else:\n print \"\\nERROR!!! Please Repeat\\n\\n\"\n return\n \n return m\n\n\na,n,freq=100,3,'m'\nprint \"FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\\n\\n\"\nprint \"Frequency of Compunding (A,S,Q,M,D,C): \",freq\nprint \"Amount of Each Monthly Payement : \",a\nprint \"Number of years: \",n\nm=main(freq)\n\ntable(freq,a,m,n)\n\n", + "input": [ + "\n", + "\n", + "def ratio_choose(freq,a,m,n):\n", + "\n", + " if freq=='C':\n", + " ratio=md3(a,m,n)\n", + " elif freq=='D':\n", + " ratio=md2(a,m,n)\n", + " else:\n", + " ratio=md1(a,m,n)\n", + "\n", + " return ratio\n", + "\n", + "\n", + "def table(freq,a,m,n):\n", + "\n", + "\n", + " print \"Interest Rate Future Amount\\n\\n\"\n", + " for count in range(1,21):\n", + " i=0.01*count\n", + " f=a*ratio_choose(freq,i,m,n)\n", + " print \" %2d %.2f\" %(count,f)\n", + "\n", + " return\n", + "\n", + "def md1(i,m,n):\n", + " \n", + " factor=1+i/m\n", + " ratio=12*(factor**(m*n)-1)/i\n", + " return ratio\n", + "\n", + "def md2(i,m,n):\n", + "\n", + " factor=1+i/m\n", + " ratio=(factor**(m*n)-1)/(factor**(m/12)-1)\n", + " return ratio\n", + "\n", + "def md3(i,dummy,n):\n", + " ratio=(10**(i*n)-1)/(10**(i/12)-1)\n", + " return ratio\n", + "\n", + "def main(freq):\n", + " m=0\n", + " freq=freq.upper()\n", + " if freq=='A':\n", + " m=1\n", + " print \"\\nAnual Compounding\\n\"\n", + " \n", + " elif freq=='S':\n", + " m=2\n", + " print \"\\nSemiannual Compounding\\n\"\n", + " \n", + " elif freq=='Q':\n", + " print \"\\nQuaterly Compounding\\n\"\n", + " m=4\n", + " \n", + " elif freq=='M':\n", + " m=12\n", + " print \"\\nMonthly Compounding\\n\"\n", + " \n", + " elif freq=='D':\n", + " m=360\n", + " print \"\\nDaily Compounding\\n\"\n", + "\n", + " elif freq=='C':\n", + " m=0\n", + " print \"\\nContinuous Compounding\\n\"\n", + " \n", + " else:\n", + " print \"\\nERROR!!! Please Repeat\\n\\n\"\n", + " return\n", + " \n", + " return m\n", + "\n", + "\n", + "a,n,freq=100,3,'m'\n", + "print \"FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\\n\\n\"\n", + "print \"Frequency of Compunding (A,S,Q,M,D,C): \",freq\n", + "print \"Amount of Each Monthly Payement : \",a\n", + "print \"Number of years: \",n\n", + "m=main(freq)\n", + "\n", + "table(freq,a,m,n)\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\n\n\nFrequency of Compunding (A,S,Q,M,D,C): m\nAmount of Each Monthly Payement : 100\nNumber of years: 3\n\nMonthly Compounding\n\nInterest Rate Future Amount\n\n\n 1 3653.00\n 2 3707.01\n 3 3762.06\n 4 3818.16\n 5 3875.33\n 6 3933.61\n 7 3993.01\n 8 4053.56\n 9 4115.27\n 10 4178.18\n 11 4242.31\n 12 4307.69\n 13 4374.33\n 14 4442.28\n 15 4511.55\n 16 4582.17\n 17 4654.18\n 18 4727.60\n 19 4802.45\n 20 4878.78\n" + "text": [ + "FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\n", + "\n", + "\n", + "Frequency of Compunding (A,S,Q,M,D,C): m\n", + "Amount of Each Monthly Payement : 100\n", + "Number of years: 3\n", + "\n", + "Monthly Compounding\n", + "\n", + "Interest Rate Future Amount\n", + "\n", + "\n", + " 1 3653.00\n", + " 2 3707.01\n", + " 3 3762.06\n", + " 4 3818.16\n", + " 5 3875.33\n", + " 6 3933.61\n", + " 7 3993.01\n", + " 8 4053.56\n", + " 9 4115.27\n", + " 10 4178.18\n", + " 11 4242.31\n", + " 12 4307.69\n", + " 13 4374.33\n", + " 14 4442.28\n", + " 15 4511.55\n", + " 16 4582.17\n", + " 17 4654.18\n", + " 18 4727.60\n", + " 19 4802.45\n", + " 20 4878.78\n" + ] } ], "prompt_number": 25 @@ -315,7 +964,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [], |