{ "metadata": { "name": "", "signature": "sha256:60d976a21b55caeaa47bb2c8586e986eed54234a29f15a816e7270f74ad2660e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 16: Templates

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.1, Page Number: 376

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def swapargs(a,b):\n", " temp=a[0]\n", " a[0]=b[0]\n", " b[0]=temp\n", "\n", "#Variable declaration\n", "i=[10]\n", "j=[20]\n", "x=[10.1]\n", "y=[23.3]\n", "a=['x']\n", "b=['z']\n", "\n", "print \"Original i, j: \",i[0],j[0]\n", "print \"Original x,y: \",x[0],y[0]\n", "print \"Original a,b: \",a[0],b[0]\n", "\n", "swapargs(i,j) #swap integers\n", "swapargs(x,y) #swap floats\n", "swapargs(a,b) #swap chars\n", "\n", "#Result\n", "print \"Swapped i, j: \",i[0],j[0]\n", "print \"Swapped x,y: \",x[0],y[0]\n", "print \"Swapped a,b: \",a[0],b[0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original i, j: 10 20\n", "Original x,y: 10.1 23.3\n", "Original a,b: x z\n", "Swapped i, j: 20 10\n", "Swapped x,y: 23.3 10.1\n", "Swapped a,b: z x\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.2, Page Number: 378

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def myfunc(x,y):\n", " print x,y\n", " \n", "myfunc(10,\"hi\")\n", "myfunc(0.23,10L)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 hi\n", "0.23 10\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.3, Page Number: 379

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def swapargs(a,b):\n", " if isinstance(a[0],int): #integer version\n", " temp=a[0]\n", " a[0]=b[0]\n", " b[0]=temp\n", " print \"Inside swapargs int specialization.\"\n", " else: #generic version\n", " temp=a[0]\n", " a[0]=b[0]\n", " b[0]=temp\n", " print \"Inside template swapargs.\"\n", "\n", "#Variable declaration\n", "i=[10]\n", "j=[20]\n", "x=[10.1]\n", "y=[23.3]\n", "a=['x']\n", "b=['z']\n", "\n", "print \"Original i, j: \",i[0],j[0]\n", "print \"Original x,y: \",x[0],y[0]\n", "print \"Original a,b: \",a[0],b[0]\n", "\n", "swapargs(i,j) #calls explicitly overloaded swapargs()\n", "swapargs(x,y) #calls generic swapargs()\n", "swapargs(a,b) #calls generic swapargs()\n", "\n", "#Result\n", "print \"Swapped i, j: \",i[0],j[0]\n", "print \"Swapped x,y: \",x[0],y[0]\n", "print \"Swapped a,b: \",a[0],b[0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original i, j: 10 20\n", "Original x,y: 10.1 23.3\n", "Original a,b: x z\n", "Inside swapargs int specialization.\n", "Inside template swapargs.\n", "Inside template swapargs.\n", "Swapped i, j: 20 10\n", "Swapped x,y: 23.3 10.1\n", "Swapped a,b: z x\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.4, Page Number: 381

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def f(a,b=None):\n", " if(b==None): #First version of f()\n", " print \"Inside f(X a)\"\n", " else: #Second version of f()\n", " print \"Inside f(X a, Y b)\"\n", " \n", "f(10) #calls f(X)\n", "f(10,20) #calls f(X,Y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Inside f(X a)\n", "Inside f(X a, Y b)\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.5, Page Number: 382

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def repeat(data,times):\n", " while times:\n", " print data\n", " times-=1\n", " \n", "repeat(\"This is a test\",3)\n", "repeat(100,5)\n", "repeat(99.0/2, 4)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is a test\n", "This is a test\n", "This is a test\n", "100\n", "100\n", "100\n", "100\n", "100\n", "49.5\n", "49.5\n", "49.5\n", "49.5\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.6, Page Number: 383

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def myabs(val):\n", " if val<0:\n", " return -val\n", " else:\n", " return val\n", "\n", "#Result\n", "print myabs(-10)\n", "print myabs(-10.0)\n", "print myabs(-10L)\n", "print myabs(-10.0)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n", "10.0\n", "10\n", "10.0\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.7, Page Number: 385

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def SIZE():\n", " return 100\n", "class queue:\n", " def __init__(self):\n", " self.q=[]\n", " self.sloc=self.rloc=0\n", " #Put an object into the queue\n", " def qput(self,i):\n", " if self.sloc==SIZE():\n", " print \"Queue is full.\"\n", " return\n", " self.sloc+=1\n", " self.q.append(i)\n", " #Get an object from the queue.\n", " def qget(self):\n", " if self.rloc==self.sloc:\n", " print \"Queue Underflow.\"\n", " return\n", " a=self.rloc\n", " self.rloc+=1\n", " return self.q[a]\n", " \n", "#Create two integer queues\n", "a=queue()\n", "b=queue()\n", "a.qput(10)\n", "b.qput(19)\n", "a.qput(20)\n", "b.qput(1)\n", "\n", "print a.qget(),\n", "print a.qget(),\n", "print b.qget(),\n", "print b.qget()\n", "\n", "#Create two double queues\n", "c=queue()\n", "d=queue()\n", "c.qput(10.12)\n", "d.qput(19.99)\n", "c.qput(-20.0)\n", "d.qput(0.986)\n", "\n", "print c.qget(),\n", "print c.qget(),\n", "print d.qget(),\n", "print d.qget()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 20 19 1\n", "10.12 -20.0 19.99 0.986\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.8, Page Number: 387

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class myclass:\n", " def __init__(self,a,b):\n", " self.__i=a\n", " self.__j=b\n", " def show(self):\n", " print self.__i,self.__j\n", "\n", "ob1=myclass(10,0.23)\n", "ob2=myclass('X',\"This is a test\")\n", "\n", "#Result\n", "ob1.show() #Show int,double\n", "ob2.show() #Show char.char*\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 0.23\n", "X This is a test\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.9, Page Number: 388

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def SIZE():\n", " return 10\n", "\n", "class atype:\n", " def __init__(self):\n", " self.a=[]\n", " for i in range(SIZE()):\n", " self.a.append(i)\n", " #Implementing the [] overloading\n", " def op1(self,i,j):\n", " if (i<0 or i>=SIZE()):\n", " print \"\\nIndex value of \",\n", " print i,\" is out-of-bounds.\"\n", " return \n", " self.a[i]=j\n", " def op2(self,i):\n", " if (i<0 or i>SIZE()-1):\n", " print \"\\nIndex value of \",\n", " print i,\" is out-of-bounds.\"\n", " return\n", " return self.a[i]\n", " \n", "#Variable declaration\n", "intob=atype()\n", "doubleob=atype()\n", " \n", "print \"Integer array: \",\n", "for i in range(SIZE()):\n", " intob.op1(i,i)\n", "for i in range(SIZE()):\n", " print intob.op2(i),\n", " \n", "print \"\"\n", "\n", "print \"Double array: \",\n", "for i in range(SIZE()):\n", " doubleob.op1(i,float(i)/3)\n", "for i in range(SIZE()):\n", " print doubleob.op2(i),\n", " \n", "intob.op1(12,100)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Integer array: 0 1 2 3 4 5 6 7 8 9 \n", "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n", "Index value of 12 is out-of-bounds.\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.10, Page Number: 389

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class atype:\n", " def __init__(self,size):\n", " self.a=[]\n", " self.size=size\n", " for i in range(size):\n", " self.a.append(i)\n", " #Implementing the [] overloading\n", " def op1(self,i,j):\n", " if (i<0 or i>self.size-1):\n", " print \"\\nIndex value of \",\n", " print i,\" is out-of-bounds.\"\n", " return\n", " self.a[i]=j\n", " def op2(self,i):\n", " if (i<0 or i>self.size-1):\n", " print \"\\nIndex value of \",\n", " print i,\" is out-of-bounds.\"\n", " return\n", " return self.a[i]\n", " \n", "#Variable declaration\n", "intob=atype(10)\n", "doubleob=atype(15)\n", " \n", "print \"Integer array: \",\n", "for i in range(10):\n", " intob.op1(i,i)\n", "for i in range(10):\n", " print intob.op2(i),\n", " \n", "print \"\"\n", "\n", "print \"Double array: \",\n", "for i in range(15):\n", " doubleob.op1(i,float(i)/3)\n", "for i in range(15):\n", " print doubleob.op2(i),\n", " \n", "intob.op1(12,100) #generates runtime error\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Integer array: 0 1 2 3 4 5 6 7 8 9 \n", "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 3.33333333333 3.66666666667 4.0 4.33333333333 4.66666666667 \n", "Index value of 12 is out-of-bounds.\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.11, Page Number: 391

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class atype:\n", " def __init__(self,size=10):\n", " self.a=[]\n", " for i in range(size):\n", " self.a.append(i)\n", " #Implementing the [] overloading\n", " def op1(self,i,j):\n", " if (i<0 and i>SIZE()-1):\n", " print \"Index value of \"\n", " print i,\" is out-of-bounds.\"\n", " exit(1)\n", " self.a[i]=j\n", " def op2(self,i):\n", " if (i<0 and i>SIZE()-1):\n", " print \"Index value of \"\n", " print i,\" is out-of-bounds.\"\n", " exit()\n", " return self.a[i]\n", " \n", "#Variable declaration\n", "intob=atype(100)\n", "doubleob=atype()\n", "defarray=atype()\n", " \n", "print \"Integer array: \",\n", "for i in range(100):\n", " intob.op1(i,i)\n", "for i in range(100):\n", " print intob.op2(i),\n", " \n", "print \"\"\n", "\n", "print \"Double array: \",\n", "for i in range(10):\n", " doubleob.op1(i,float(i)/3)\n", "for i in range(10):\n", " print doubleob.op2(i),\n", " \n", "print \"\"\n", "\n", "print \"Defarray array: \",\n", "for i in range(10):\n", " doubleob.op1(i,i)\n", "for i in range(10):\n", " print doubleob.op2(i),\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Integer array: 0 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 \n", "Double array: 0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 \n", "Defarray array: 0 1 2 3 4 5 6 7 8 9\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.12, Page Number: 393

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class myclass:\n", " def __init__(self,a):\n", " if isinstance(a,int):\n", " print \"Inside myclassspecialization\"\n", " self.__x=a*a\n", " else:\n", " print \"Inside generic myclass\"\n", " self.__x=a\n", " def getx(self):\n", " return self.__x\n", " \n", "d=myclass(10.1)\n", "print \"double: \",d.getx(),\"\\n\"\n", "\n", "i=myclass(5)\n", "print \"int: \",i.getx()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Inside generic myclass\n", "double: 10.1 \n", "\n", "Inside myclassspecialization\n", "int: 25\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }