summaryrefslogtreecommitdiff
path: root/Programming_With_C/chapter11.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-07-07 16:34:28 +0530
committerJovina Dsouza2014-07-07 16:34:28 +0530
commitfffcc90da91b66ee607066d410b57f34024bd1de (patch)
tree7b8011d61013305e0bf7794a275706abd1fdb0d3 /Programming_With_C/chapter11.ipynb
parent299711403e92ffa94a643fbd960c6f879639302c (diff)
downloadPython-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.tar.gz
Python-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.tar.bz2
Python-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.zip
adding book
Diffstat (limited to 'Programming_With_C/chapter11.ipynb')
-rwxr-xr-xProgramming_With_C/chapter11.ipynb977
1 files changed, 977 insertions, 0 deletions
diff --git a/Programming_With_C/chapter11.ipynb b/Programming_With_C/chapter11.ipynb
new file mode 100755
index 00000000..d858cd07
--- /dev/null
+++ b/Programming_With_C/chapter11.ipynb
@@ -0,0 +1,977 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:81f953770b0acac35782d6d61b5968cf0c7681b2e047ca75cfe8abd7244d5bab"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 11: Pointers<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.1, Page 11.2<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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\n",
+ "v= 3 &v= 56575840 pv= 56575840 *pv 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.2, Page number: 11.3<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.3, Page munber: 11.4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.7, Page number: 11.6<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.8, Page number: 11.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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",
+ "\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.12, Page number: 11.15<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.14, Page number: 11.17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.16, Page number: 11.19<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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": [
+ "\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.17, Page number: 11.21<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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",
+ "\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.18, Page number: 11.22<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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",
+ "\n",
+ "py - px = -3C\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.22, Page number: 11.26<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.24, Page number: 11.31<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.26, Page number: 11.34<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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",
+ "\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.28, Page number: 11.37<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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",
+ "\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
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11.30, Page number: 11.44<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "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",
+ "\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
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 25
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file