{ "metadata": { "name": "ch11" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 11: Overloading Operators" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.1, Page no: 256" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " if d==None:\n", " self.num = n.num\n", " self.den = n.den\n", " elif n==None:\n", " self.num = 0\n", " self.den = 1\n", " else:\n", " self.num = n\n", " self.den = d\n", " \n", " def equals(self):\n", " return self # retuns calling object." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.2, Page no: 257" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " pass\n", " \n", " def equals(self):\n", " pass\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.3, Page no: 256" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " if d==None:\n", " self.num = n.num\n", " self.den = n.den\n", " elif n==None:\n", " self.num = 0\n", " self.den = 1\n", " else:\n", " self.num = n\n", " self.den = d\n", " \n", "\n", "z = Ratio(22,7)\n", "y = z\n", "x = z\n", "\n", "x = Ratio(22,7)\n", "y = Ratio(x)\n", "z = x\n", "w = x" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.4, Page no: 259" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " self.num = n\n", " self.den = d\n", " def __mul__(self,y):\n", " pass\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.5, Page no: 259" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def gcd(m,n):\n", " # returns the greatest common divisor of m and n:\n", " if (m0):\n", " r = m % n\n", " m = n\n", " n = r\n", " return m\n", "\n", "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __mul__(self,y):\n", " z = Ratio(self.num * y.num, self.den * y.den)\n", " return z\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", "x = Ratio(22,7)\n", "y = Ratio(-3,8)\n", "z = x\n", "z.print_()\n", "x = y*z\n", "x.print_()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "22 / 7\n", "-33 / 28\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.6, Page no: 260" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def gcd(m,n):\n", " # returns the greatest common divisor of m and n:\n", " if (m0):\n", " r = m % n\n", " m = n\n", " n = r\n", " return m\n", "\n", "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __imul__(self,y):\n", " self.num = self.num * y.num\n", " self.den = self.den * y.den\n", " def print_(self):\n", " print self.num , '/', self.den" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.7, Page no: 260" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __imul__(self,y):\n", " self.num = self.num * y.num\n", " self.den = self.den * y.den\n", " def __eq__(self,y):\n", " return (x.num * y.den == y.num * x.den)\n", " def print_(self):\n", " print self.num , '/', self.den\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.8, Page no: 261" ] }, { "cell_type": "code", "collapsed": false, "input": [ "''' \n", "Python does not use << operator for printing. So here we are just declaring function name as print_.\n", "'''\n", "\n", "class Ratio:\n", " def __init__(self,n=None,d=None):\n", " self.num = n\n", " self.den = d\n", " def __imul__(self,y):\n", " self.num = self.num * y.num\n", " self.den = self.den * y.den\n", " def __eq__(self,y):\n", " return (x.num * y.den == y.num * x.den)\n", " def print_(self):\n", " print self.num , '/', self.den\n", "\n", "\n", "x = Ratio(22,7)\n", "y = Ratio(-3,8)\n", "x.print_() , y.print_()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "22 / 7\n", "-3 / 8\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "(None, None)" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.9, Page no: 262" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "Python does not have >> for input. so we will use input function.\n", "'''\n", "def gcd(m,n):\n", " # returns the greatest common divisor of m and n:\n", " if (m0):\n", " r = m % n\n", " m = n\n", " n = r\n", " return m\n", "class Ratio:\n", " def __init__(self,n=0,d=1):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __mul__(self,y):\n", " z = Ratio(self.num * y.num, self.den * y.den)\n", " return z\n", " def print_(self):\n", " print self.num , '/', self.den\n", " \n", " def input(self):\n", " self.num = int(raw_input('Numerator : '))\n", " self.den = int(raw_input('Denominator : '))\n", " self.reduce()\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", "\n", "x = Ratio()\n", "y = Ratio()\n", "x.input()\n", "y.input()\n", "x.print_()\n", "y.print_()\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Numerator : -10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Denominator : -24\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Numerator : 36\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Denominator : -20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "5 / 12\n", "-9 / 5\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.10, Page no: 263" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def gcd(m,n):\n", " # returns the greatest common divisor of m and n:\n", " if (m0):\n", " r = m % n\n", " m = n\n", " n = r\n", " return m\n", "class Ratio:\n", " def __init__(self,n=0,d=1):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __mul__(self,y):\n", " z = Ratio(self.num * y.num, self.den * y.den)\n", " return z\n", " def print_(self):\n", " print self.num , '/', self.den\n", " \n", " def input(self):\n", " self.num = int(raw_input('Numerator : '))\n", " self.den = int(raw_input('Denominator : '))\n", " self.reduce()\n", " def __float__(self):\n", " return float(self.num)/self.den\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 = 15.py\n", " g = gcd(sgn*self.num,self.den)\n", " self.num /= g\n", " self.den /= g\n", "\n", "x = Ratio(-5,8)\n", "print \"x = \" , \n", "x.print_() \n", "print \", float(x) = \" , float(x) \n", "P = Ratio(22,7)\n", "PI = float(P)\n", "print \"P = \" ,\n", "P.print_() \n", "print \", PI = \" , PI\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = -5 / 8\n", ", float(x) = -0.625\n", "P = 22 / 7\n", ", PI = 3.14285714286\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.11, Page no: 264" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def gcd(m,n):\n", " # returns the greatest common divisor of m and n:\n", " if (m0):\n", " r = m % n\n", " m = n\n", " n = r\n", " return m\n", "class Ratio:\n", " def __init__(self,n=0,d=1):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __mul__(self,y):\n", " z = Ratio(self.num * y.num, self.den * y.den)\n", " return z\n", " def print_(self):\n", " print self.num , '/', self.den\n", " \n", " def __iadd__(self,n):\n", " self.num += self.den\n", " return self\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(22,7)\n", "x += 1\n", "y = x\n", "print \"y = \" ,\n", "y.print_()\n", "print \", x = \",\n", "x.print_()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "y = 29 / 7\n", ", x = 29 / 7\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.12, Page no: 265" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def gcd(m,n):\n", " # returns the greatest common divisor of m and n:\n", " if (m0):\n", " r = m % n\n", " m = n\n", " n = r\n", " return m\n", "class Ratio:\n", " def __init__(self,n=0,d=1):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __mul__(self,y):\n", " z = Ratio(self.num * y.num, self.den * y.den)\n", " return z\n", " def print_(self):\n", " print self.num , '/', self.den\n", " \n", " def __iadd__(self,n):\n", " self.num += self.den\n", " return self\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(22,7)\n", "y = Ratio(x.num,x.den)\n", "x += 1\n", "print \"y = \" ,\n", "y.print_()\n", "print \", x = \",\n", "x.print_()\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "y = 22 / 7\n", ", x = 29 / 7\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.13, Page no:266" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def gcd(m,n):\n", " # returns the greatest common divisor of m and n:\n", " if (m0):\n", " r = m % n\n", " m = n\n", " n = r\n", " return m\n", "class Ratio:\n", " def __init__(self,n=0,d=1):\n", " self.num = n\n", " self.den = d\n", " self.reduce()\n", " def __mul__(self,y):\n", " z = Ratio(self.num * y.num, self.den * y.den)\n", " return z\n", " def print_(self):\n", " print self.num , '/', self.den\n", " \n", " def __getitem__(self,k):\n", " if k == 1:\n", " return self.num\n", " else:\n", " return self.den\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(22,7)\n", "print \"x = \" ,\n", "x.print_()\n", "print \"x[1] = \" , x[1] , \", x[2] = \" , x[2]\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = 22 / 7\n", "x[1] = 22 , x[2] = 7\n" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }