{
 "metadata": {
  "name": "ch10"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 10: Classes"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.1, Page no: 233"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Ratio:\n",
      "    def __init__(self):\n",
      "        self.num = 0\n",
      "        self.den = 0\n",
      "    def assign(self,n,d):\n",
      "        self.num = n\n",
      "        self.den = d\n",
      "    def convert(self):\n",
      "        return float(self.num)/self.den\n",
      "    def invert(self):\n",
      "        self.num,self.den = self.den,self.num\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "    \n",
      "x = Ratio()\n",
      "x.assign(22,7)\n",
      "print \"x = \",\n",
      "x.print_()\n",
      "print \" = \" , x.convert() \n",
      "x.invert()\n",
      "print  \"1/x = \",\n",
      "x.print_()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "x =  22 / 7  =  3.14285714286\n",
        "1/x =  7 / 22\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.2, Page no: 234"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Ratio:\n",
      "    def __init__(self):\n",
      "        self.num = 0\n",
      "        self.den = 0\n",
      "    def assign(self,n,d):\n",
      "        self.num = n\n",
      "        self.den = d\n",
      "    def convert(self):\n",
      "        return float(self.num)/self.den\n",
      "    def invert(self):\n",
      "        self.num,self.den = self.den,self.num\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.3, Page no: 235"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n,d):\n",
      "        self.num = n\n",
      "        self.den = d\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n",
      "x = Ratio(-1,3)\n",
      "y = Ratio(22,7)\n",
      "print \"x = \",\n",
      "x.print_()\n",
      "print \" and y = \",\n",
      "y.print_()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "x =  -1 / 3  and y =  22 / 7\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.4, Page no: 236"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n=None,d=None):\n",
      "        if n==None:\n",
      "            self.num = 0\n",
      "            self.den = 1\n",
      "        elif d==None:\n",
      "            self.num = n\n",
      "            self.den = 1\n",
      "        else:\n",
      "            self.num = n\n",
      "            self.den = d\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n",
      "x = Ratio()\n",
      "y = Ratio(4)\n",
      "z = Ratio(22,7)\n",
      "print \"x = \",\n",
      "x.print_()\n",
      "print \"\\ny = \",\n",
      "y.print_()\n",
      "print \"\\nz = \",\n",
      "z.print_()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "x =  0 / 1 \n",
        "y =  4 / 1 \n",
        "z =  22 / 7\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.5, Page no: 237"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n=None,d=None):\n",
      "        if n==None:\n",
      "            self.num = 0\n",
      "            self.den = 1\n",
      "        elif d==None:\n",
      "            self.num = n\n",
      "            self.den = 1\n",
      "        else:\n",
      "            self.num = n\n",
      "            self.den = d\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.6, Page no: 237"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n=0,d=1):\n",
      "        self.num = n\n",
      "        self.den = d\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n",
      "\n",
      "x = Ratio()\n",
      "y = Ratio(4)\n",
      "z = Ratio(22,7)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.7, Page no: 238"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n=0,d=1):\n",
      "        self.num = n\n",
      "        self.den = d\n",
      "    def numerator(self):\n",
      "        return self.num\n",
      "    def denominator(self):\n",
      "        return self.den\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n",
      "\n",
      "x = Ratio(22,7)\n",
      "print x.numerator() , '/' , x.denominator() "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "22 / 7\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.8, Page no: 238"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def gcd(m,n):\n",
      "    # returns the greatest common divisor of m and n:\n",
      "    if (m<n):\n",
      "        m,n = n,m\n",
      "    while (n>0):\n",
      "        r = m % n\n",
      "        m = n\n",
      "        n = r\n",
      "    return m\n",
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n=0,d=1):\n",
      "        self.num = n\n",
      "        self.den = d\n",
      "        self.reduce()\n",
      "    def numerator(self):\n",
      "        return self.num\n",
      "    def denominator(self):\n",
      "        return self.den\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n",
      "    def reduce(self):\n",
      "        # enforce invariant(den > 0):\n",
      "        if (self.num == 0 or self.den == 0):\n",
      "            self.num = 0\n",
      "            self.den = 1\n",
      "            return\n",
      "        if (self.den < 0):\n",
      "            self.den *= -1\n",
      "            self.num *= -1\n",
      "        # enforce invariant(gcd(num,den) == 1):\n",
      "        if (self.den == 1):\n",
      "            return\n",
      "        # it's already reduced\n",
      "        sgn = 0\n",
      "        if self.num < 0:\n",
      "            sgn = -1\n",
      "        else:\n",
      "            sgn = 1\n",
      "        g = gcd(sgn*self.num,self.den)\n",
      "        self.num /= g\n",
      "        self.den /= g\n",
      "\n",
      "x = Ratio(100,-360)\n",
      "x.print_()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-5 / 18\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.9, Page no: 240"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def gcd(m,n):\n",
      "    # returns the greatest common divisor of m and n:\n",
      "    if (m<n):\n",
      "        m,n = n,m\n",
      "    while (n>0):\n",
      "        r = m % n\n",
      "        m = n\n",
      "        n = r\n",
      "    return m\n",
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n=0,d=None):\n",
      "        if d == None:\n",
      "            self.num = n.num\n",
      "            self.den = n.den\n",
      "        else:   \n",
      "            self.num = n\n",
      "            self.den = d\n",
      "        self.reduce()\n",
      "    def numerator(self):\n",
      "        return self.num\n",
      "    def denominator(self):\n",
      "        return self.den\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n",
      "    def reduce(self):\n",
      "        # enforce invariant(den > 0):\n",
      "        if (self.num == 0 or self.den == 0):\n",
      "            self.num = 0\n",
      "            self.den = 1\n",
      "            return\n",
      "        if (self.den < 0):\n",
      "            self.den *= -1\n",
      "            self.num *= -1\n",
      "        # enforce invariant(gcd(num,den) == 1):\n",
      "        if (self.den == 1):\n",
      "            return\n",
      "        # it's already reduced\n",
      "        sgn = 0\n",
      "        if self.num < 0:\n",
      "            sgn = -1\n",
      "        else:\n",
      "            sgn = 1\n",
      "        g = gcd(sgn*self.num,self.den)\n",
      "        self.num /= g\n",
      "        self.den /= g\n",
      "\n",
      "x = Ratio(100,360)\n",
      "y = Ratio(x)\n",
      "print \"x = \",\n",
      "x.print_()\n",
      "print \"y = \",\n",
      "y.print_()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "x =  5 / 18 y =  5 / 18\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.10, Page no: 241"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def gcd(m,n):\n",
      "    # returns the greatest common divisor of m and n:\n",
      "    if (m<n):\n",
      "        m,n = n,m\n",
      "    while (n>0):\n",
      "        r = m % n\n",
      "        m = n\n",
      "        n = r\n",
      "    return m\n",
      "\n",
      "class Ratio:\n",
      "    def __init__(self,n=0,d=None):\n",
      "        if d == None:\n",
      "            print \"COPY CONSTRUCTOR CALLED\"\n",
      "            self.num = n.num\n",
      "            self.den = n.den\n",
      "        else:   \n",
      "            self.num = n\n",
      "            self.den = d\n",
      "        self.reduce()\n",
      "    def numerator(self):\n",
      "        return self.num\n",
      "    def denominator(self):\n",
      "        return self.den\n",
      "    def print_(self):\n",
      "        print self.num , '/' , self.den ,\n",
      "\n",
      "    def reduce(self):\n",
      "        # enforce invariant(den > 0):\n",
      "        if (self.num == 0 or self.den == 0):\n",
      "            self.num = 0\n",
      "            self.den = 1\n",
      "            return\n",
      "        if (self.den < 0):\n",
      "            self.den *= -1\n",
      "            self.num *= -1\n",
      "        # enforce invariant(gcd(num,den) == 1):\n",
      "        if (self.den == 1):\n",
      "            return\n",
      "        # it's already reduced\n",
      "        sgn = 0\n",
      "        if self.num < 0:\n",
      "            sgn = -1\n",
      "        else:\n",
      "            sgn = 1\n",
      "        g = gcd(sgn*self.num,self.den)\n",
      "        self.num /= g\n",
      "        self.den /= g\n",
      "\n",
      "def f(r):\n",
      "    s = Ratio(r)\n",
      "\n",
      "x = Ratio(22,7)\n",
      "y = Ratio(x) #calls the copy constructor, copying x to y\n",
      "f(y)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "COPY CONSTRUCTOR CALLED\n",
        "COPY CONSTRUCTOR CALLED\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.11, Page no: 242"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Note : Python objects die when program gets exit.\n",
      "'''\n",
      "\n",
      "class Ratio:\n",
      "    def __init__(self):\n",
      "        print \"OBJECT IS BORN.\"\n",
      "    def __del__(self):\n",
      "        print \"OBJECT DIES.\"\n",
      "\n",
      "x =  Ratio()\n",
      "print \"Now x is alive.\"\n",
      "print \"Now between blocks.\"\n",
      "y = Ratio()\n",
      "print \"Now y is alive.\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "OBJECT IS BORN.\n",
        "Now x is alive.\n",
        "Now between blocks.\n",
        "OBJECT IS BORN.\n",
        "Now y is alive.\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.12, Page no: 244"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class X:\n",
      "    def __init(self):\n",
      "        data = 0\n",
      "\n",
      "p = X()\n",
      "p.data = 22\n",
      "print \"p.data = \" , p.data , \" = \" , p.data\n",
      "p.data = 44\n",
      "print \" p.data = \" , p.data , \" = \" , p.data "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "p.data =  22  =  22\n",
        " p.data =  44  =  44\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.13, Page no: 244"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "class Node:\n",
      "    def __init__(self,d,q=None):\n",
      "        self.data = d\n",
      "        self.next = q\n",
      "\n",
      "n = int(raw_input())\n",
      "q = Node(n)\n",
      "while True:\n",
      "    n = int(raw_input())\n",
      "    if n<=0:\n",
      "        break\n",
      "    p = Node(n, q)\n",
      "    q = p\n",
      "k = p\n",
      "while k != None:\n",
      "    print k.data , '->' , \n",
      "    k = k.next\n",
      "print '*'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "22\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "33\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "44\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "55\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "66\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "77\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.14, Page no: 246"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Python does not support static data type.\n",
      "Python automatically handles local variable so we need not to delete it.\n",
      "'''\n",
      "count = 0\n",
      "class Widget:\n",
      "    def __init__(self):\n",
      "        global count\n",
      "        count += 1\n",
      "        \n",
      "w = Widget()\n",
      "x = Widget()\n",
      "print  \"Now there are \" , count , 'widgets'\n",
      "if True:\n",
      "    w = Widget()\n",
      "    x = Widget()\n",
      "    y = Widget()\n",
      "    z = Widget()\n",
      "    print \"Now there are\" , count , 'widgets' \n",
      "print \"Now there are \" , count , 'widgets'\n",
      "y = Widget()\n",
      "print \"Now there are \" , count , 'widgets'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "OBJECT DIES.\n",
        "Now there are  2 widgets\n",
        "OBJECT DIES.\n",
        "Now there are 6 widgets\n",
        "Now there are  6 widgets\n",
        "Now there are  7 widgets\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.15, Page no: 246"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "count = 0\n",
      "class Widget:\n",
      "    def __init__(self):\n",
      "        global count\n",
      "        count += 1\n",
      "    def numWidgets(self):\n",
      "        global count\n",
      "        return count\n",
      "        \n",
      "w = Widget()\n",
      "x = Widget()\n",
      "print  \"Now there are \" , w.numWidgets() , 'widgets'\n",
      "if True:\n",
      "    w = Widget()\n",
      "    x = Widget()\n",
      "    y = Widget()\n",
      "    z = Widget()\n",
      "    print \"Now there are\" , w.numWidgets() , 'widgets' \n",
      "print \"Now there are \" , w.numWidgets() , 'widgets'\n",
      "y = Widget()\n",
      "print \"Now there are \" , w.numWidgets() , 'widgets'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Now there are  2 widgets\n",
        "Now there are 6 widgets\n",
        "Now there are  6 widgets\n",
        "Now there are  7 widgets\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.16, Page no: 247"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "count = 0\n",
      "class Widget:\n",
      "    def __init__(self):\n",
      "        global count\n",
      "        count += 1\n",
      "    def numWidgets(self):\n",
      "        global count\n",
      "        return count\n",
      "        \n",
      "w = Widget()\n",
      "x = Widget()\n",
      "print  \"Now there are \" , w.numWidgets() , 'widgets'\n",
      "if True:\n",
      "    w = Widget()\n",
      "    x = Widget()\n",
      "    y = Widget()\n",
      "    z = Widget()\n",
      "    print \"Now there are\" , w.numWidgets() , 'widgets' \n",
      "print \"Now there are \" , w.numWidgets() , 'widgets'\n",
      "y = Widget()\n",
      "print \"Now there are \" , w.numWidgets() , 'widgets'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Now there are  2 widgets\n",
        "Now there are 6 widgets\n",
        "Now there are  6 widgets\n",
        "Now there are  7 widgets\n"
       ]
      }
     ],
     "prompt_number": 18
    }
   ],
   "metadata": {}
  }
 ]
}