{
 "metadata": {
  "name": "ch13"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 13: Templates and Iterators"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.1, Page no: 300"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def swap(x,y):\n",
      "    x[0],y[0] = y[0],x[0]\n",
      "\n",
      "m = [22]\n",
      "n = [66]\n",
      "swap(m, n)\n",
      "s1 = [\"John Adams\"]\n",
      "s2 = [\"James Madison\"]\n",
      "swap(s1, s2)\n",
      "x = [22/7]\n",
      "y = [-3]\n",
      "swap(x, y)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.2, Page no: 301"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def sort(v,n):\n",
      "    for i in range(1,n):\n",
      "        for j in range(n-i):\n",
      "            if v[j] > v[j+1]:\n",
      "                v[j],v[j+1] = v[j+1],v[j]\n",
      "\n",
      "def print_( v,n):\n",
      "    for i in range(n):\n",
      "        print v[i],\n",
      "    print \"\"\n",
      "    \n",
      "a = [55, 33, 88, 11, 44, 99, 77, 22, 66]\n",
      "print_(a,9);\n",
      "sort(a,9)\n",
      "print_(a,9)\n",
      "s = [\"Tom\", \"Hal\", \"Dan\", \"Bob\", \"Sue\", \"Ann\", \"Gus\"]\n",
      "print_(s,7)\n",
      "sort(s,7)\n",
      "print_(s,7)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "55 33 88 11 44 99 77 22 66 \n",
        "11 22 33 44 55 66 77 88 99 \n",
        "Tom Hal Dan Bob Sue Ann Gus \n",
        "Ann Bob Dan Gus Hal Sue Tom \n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.3, Page no: 303"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Stack:\n",
      "    def __init__(self,s=100):\n",
      "        self.size = s\n",
      "        self.top = -1\n",
      "        self.data = []\n",
      "    def push(self,x):\n",
      "        self.data.append( x)\n",
      "        self.top += 1\n",
      "    def pop(self):\n",
      "        d = self.data[self.top]\n",
      "        self.data.pop(self.top)\n",
      "        self.top -= 1\n",
      "        return d        \n",
      "    def isEmpty(self):\n",
      "        return self.top == -1\n",
      "    def isFull(self):\n",
      "        return self.top==self.size-1\n",
      "        \n",
      "intStack1 = Stack(5)\n",
      "intStack2 = Stack(10)\n",
      "charStack = Stack(8)\n",
      "intStack1.push(77)\n",
      "charStack.push('A')\n",
      "intStack2.push(22)\n",
      "charStack.push('E')\n",
      "charStack.push('K')\n",
      "intStack2.push(44)\n",
      "print intStack2.pop() \n",
      "print intStack2.pop() \n",
      "if (intStack2.isEmpty()):\n",
      "    print \"intStack2 is empty.\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "44\n",
        "22\n",
        "intStack2 is empty.\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.4, Page no: 305"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "= operator does work in python by defaultly.\n",
      "'''\n",
      "class Vector:\n",
      "    def __init__(self,n=None):\n",
      "        if type(n) == int :\n",
      "            self.size = n\n",
      "            self.data = []\n",
      "        else:\n",
      "            self.size = 8\n",
      "            self.data = []\n",
      "        for i in range(self.size):\n",
      "            self.data.append(0)\n",
      "v = Vector()\n",
      "v.data[5] = 127\n",
      "w = v\n",
      "x = Vector(3)\n",
      "print w.size\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.5, Page no: 306"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Vector:\n",
      "    def __init__(self,n=None):\n",
      "        if type(n) == int :\n",
      "            self.size = n\n",
      "            self.data = []\n",
      "        else:\n",
      "            self.size = 8\n",
      "            self.data = []\n",
      "        for i in range(self.size):\n",
      "            self.data.append(0)\n",
      "\n",
      "class Array(Vector):\n",
      "    def __init__(self,i,j):\n",
      "        Vector.__init__(self,j-i+1)\n",
      "        self.i0= i\n",
      "    def __setitem__(self,k,v):\n",
      "        self.data[k-self.i0] = v\n",
      "    def __getitem__(self,i):\n",
      "        return self.data[self.i0-i]\n",
      "    def firstSubscript(self):\n",
      "        return self.i0\n",
      "    def lastSubscript(self):\n",
      "        return self.i0+self.size-1\n",
      "\n",
      "x = Array(1,3)\n",
      "x.data[0] = 3.14159\n",
      "x.data[1] = 0.08516\n",
      "x.data[2] = 5041.92\n",
      "print \"x.size() = \" , x.size \n",
      "print \"x.firstSubscript() = \" , x.firstSubscript() \n",
      "print \"x.lastSubscript() = \" , x.lastSubscript()\n",
      "for i in range(0,3):\n",
      "    print \"x[\" , i + 1 , \"] = \" , x.data[i]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "x.size() =  3\n",
        "x.firstSubscript() =  1\n",
        "x.lastSubscript() =  3\n",
        "x[ 1 ] =  3.14159\n",
        "x[ 2 ] =  0.08516\n",
        "x[ 3 ] =  5041.92\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.6, Page no: 308"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class Matrix:\n",
      "    def __init__(self,r=1,c=1):\n",
      "        self.rows = r\n",
      "        self.columns = c\n",
      "        self.vector = []\n",
      "        for i in range(r):\n",
      "            a = []\n",
      "            for j in range(c):\n",
      "                a.append(0)\n",
      "            self.vector.append(a)\n",
      "\n",
      "a = Matrix(2,3)\n",
      "a.vector[0][0] = 0.0\n",
      "a.vector[0][1] = 0.1\n",
      "a.vector[0][2] = 0.2\n",
      "a.vector[1][0] = 1.0\n",
      "a.vector[1][1] = 1.1\n",
      "a.vector[1][2] = 1.2\n",
      "\n",
      "print \"The matrix a has \" , a.rows , \" rows and \", a.columns , \" columns:\"\n",
      "for i in range(2):\n",
      "    for j in range(3):\n",
      "        print a.vector[i][j] ,\n",
      "    print \"\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The matrix a has  2  rows and  3  columns:\n",
        "0.0 0.1 0.2 \n",
        "1.0 1.1 1.2 \n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.7, Page no: 309"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "friends = []\n",
      "\n",
      "\n",
      "friends.insert(0,\"Bowen, Van\")\n",
      "friends.insert(0,\"Dixon, Tom\")\n",
      "friends.insert(0,\"Mason, Joe\")\n",
      "friends.insert(0,\"White, Ann\")\n",
      "\n",
      "for i in range(len(friends)):\n",
      "    print friends[i], '->' ,\n",
      "print '*'\n",
      "friends.remove('White, Ann')\n",
      "print \"Removed: \" , 'White, Ann'\n",
      "for i in range(len(friends)):\n",
      "    print friends[i], '->' ,\n",
      "print '*'\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "White, Ann -> Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\n",
        "Removed:  White, Ann\n",
        "Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13.8, Page no: 313"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "friends = []\n",
      "friends.append(\"Bowen, Van\")\n",
      "friends.append(\"Dixon, Tom\")\n",
      "friends.append(\"Mason, Joe\")\n",
      "friends.append(\"White, Ann\")\n",
      "for i in range(len(friends)):\n",
      "    print friends[i], '->' ,\n",
      "print '*'\n",
      "\n",
      "friends.remove(\"Mason, Joe\")\n",
      "friends[1] = \"Davis, Jim\"\n",
      "for i in range(len(friends)):\n",
      "    print friends[i], '->' ,\n",
      "print '*'\n",
      "\n",
      "friends.insert(2,\"Morse, Sam\")\n",
      "for i in range(len(friends)):\n",
      "    print friends[i], '->' ,\n",
      "print '*'\n",
      "\n",
      "for i in range(len(friends)):\n",
      "    print \"[\" ,friends[i] , \"]\" , '->' ,\n",
      "print '*'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Bowen, Van -> Dixon, Tom -> Mason, Joe -> White, Ann -> *\n",
        "Bowen, Van -> Davis, Jim -> White, Ann -> *\n",
        "Bowen, Van -> Davis, Jim -> Morse, Sam -> White, Ann -> *\n",
        "[ Bowen, Van ] -> [ Davis, Jim ] -> [ Morse, Sam ] -> [ White, Ann ] -> *\n"
       ]
      }
     ],
     "prompt_number": 8
    }
   ],
   "metadata": {}
  }
 ]
}