diff options
Diffstat (limited to 'Schaum's_Outlines:_Programming_with_C++')
18 files changed, 12825 insertions, 0 deletions
diff --git a/Schaum's_Outlines:_Programming_with_C++/README.txt b/Schaum's_Outlines:_Programming_with_C++/README.txt new file mode 100755 index 00000000..272f5329 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/README.txt @@ -0,0 +1,10 @@ +Contributed By: Hiren Shah +Course: mca +College/Institute/Organization: Financial Technology +Department/Designation: Developer +Book Title: Schaum's Outlines: Programming with C++ +Author: John R. Hubbard +Publisher: McGRAW-HILL +Year of publication: 2000 +Isbn: 0-07-135346-1. +Edition: 2nd
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch1.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch1.ipynb new file mode 100755 index 00000000..175e4501 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch1.ipynb @@ -0,0 +1,448 @@ +{
+ "metadata": {
+ "name": "ch1"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 1: Elementary C++ Programming"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.1, page no. 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Hello, World!\\n\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello, World!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.2, page no. 3"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "print \"Hello, World!\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello, World!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.3, page no. 4"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "print \"Hel\" + \"lo, Wo\" + \"rld!\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello, World!\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.4, page no. 5"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "print \"Hello, W\" + 'o' + \"rld\" + '!' "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello, World!\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.5, page no. 5"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "print \"The Millennium ends Dec %d %d \" %(31,2000)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Millennium ends Dec 31 2000 \n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.6, page no. 5"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "m = 44 # assigns the value 44 to the variable m\n",
+ "print \"m = %d \" % m,\n",
+ "n = m + 33 # assigns the value 77 to the variable n\n",
+ "print \"and n = %d \" % n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "m = 44 and n = 77 \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.7, page no. 6"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "n=44\n",
+ "print \"n = %d\" % n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = 44\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.8, page no. 7"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Python does not have semicolons so wont give any errors.\n",
+ "n=44\n",
+ "print \"n = %d\" % n "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = 44\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.9, page no. 7"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "m = 0 #In python we do not have declaration of variables, we just initialize it and use it.\n",
+ "n=44\n",
+ "print \"m = %d and n = %d\" %(m,n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "m = 0 and n = 44\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.10, page no. 8"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "BEEP = '\\b'\n",
+ "MAXINT = 2147483647\n",
+ "N = MAXINT/2\n",
+ "KM_PER_MI = 1.60934\n",
+ "PI = 3.14159265358979323846"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.11, page no. 8"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "print \"Enter two integers: \"\n",
+ "m = int(raw_input())\n",
+ "n = int(raw_input())\n",
+ "print \"m = %d , n = %d \" %(m,n)\n",
+ "\n",
+ "print \"Enter three decimal numbers: \"\n",
+ "x = float(raw_input())\n",
+ "y = float(raw_input())\n",
+ "z = float(raw_input())\n",
+ "\n",
+ "print \"x = %f , y = %f , z = %f\" %(x,y,z)\n",
+ "\n",
+ "print \"Enter four characters: \";\n",
+ "c1 = raw_input()\n",
+ "c2 = raw_input()\n",
+ "c3 = raw_input()\n",
+ "c4 = raw_input()\n",
+ "print \"c1 = \" + c1 + \", c2 = \" + c2 + \", c3 = \" + c3 + \", c4 = \" + c4 "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter two integers: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "22\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "44\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "m = 22 , n = 44 \n",
+ "Enter three decimal numbers: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2.2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4.4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6.6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 2.200000 , y = 4.400000 , z = 6.600000\n",
+ "Enter four characters: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "B\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "D\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "c1 = A, c2 = B, c3 = C, c4 = D\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch10.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch10.ipynb new file mode 100755 index 00000000..eb3d1761 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch10.ipynb @@ -0,0 +1,877 @@ +{
+ "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": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch11.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch11.ipynb new file mode 100755 index 00000000..321fb35b --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch11.ipynb @@ -0,0 +1,793 @@ +{
+ "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 (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",
+ "\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 (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",
+ "\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 (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",
+ "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 (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",
+ "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 (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",
+ "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 (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",
+ "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 (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",
+ "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": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch12.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch12.ipynb new file mode 100755 index 00000000..2743b6a5 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch12.ipynb @@ -0,0 +1,869 @@ +{
+ "metadata": {
+ "name": "ch12"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 12: Composition and Inheritance"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.1, Page no: 273"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",nat=\"U.S.A.\",s=1):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ "\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " \n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ "\n",
+ "creator = Person(\"Bjarne Stroustrup\", \"Denmark\")\n",
+ "print \"The creator of C++ was \" ,\n",
+ "creator.printName() \n",
+ "print \", who was born in \" ,\n",
+ "creator.printNationality() \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The creator of C++ was Bjarne Stroustrup , who was born in Denmark\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.2, Page no: 274"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "peace = Date(11,11,1918)\n",
+ "print \"World War I ended on \" ,\n",
+ "peace.print_()\n",
+ "peace.setDate(8,14,1945)\n",
+ "print \"World War II ended on \" ,\n",
+ "peace.print_()\n",
+ "print \"Enter month, day, and year: \"\n",
+ "date = Date()\n",
+ "date.input()\n",
+ "print \"The date is \" , \n",
+ "date.print_()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "World War I ended on November 11 , 1918\n",
+ "World War II ended on August 14 , 1945\n",
+ "Enter month, day, and year: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1976\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The date is July 4 , 1976\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.3, Page no: 274"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "author = Person(\"Thomas Jefferson\", 1)\n",
+ "author.setDOB(4,13,1743)\n",
+ "author.setDOD(7,4,1826)\n",
+ "print \"The author of the Declaration of Independence was \",\n",
+ "author.printName()\n",
+ "print \".\\nHe was born on \",\n",
+ "author.printDOB()\n",
+ "print \" and died on \",\n",
+ "author.printDOD()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The author of the Declaration of Independence was Thomas Jefferson .\n",
+ "He was born on April 13 , 1743\n",
+ " and died on July 4 , 1826\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.4, Page no: 276"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,n,s=0,i=\"\"):\n",
+ " Person.__init__(self,n,s)\n",
+ " self.id = i\n",
+ " self.credits = 0\n",
+ " self.gpa = 0\n",
+ " self.dom = Date()\n",
+ "\n",
+ " def setDOM(self,m,d,y):\n",
+ " self.dom.setDate(m, d, y)\n",
+ " def printDOM(self):\n",
+ " self.dom.print_()\n",
+ "\n",
+ "x = Student(\"Ann Jones\", 0, \"219360061\")\n",
+ "x.setDOB(5, 13, 1977)\n",
+ "x.setDOM(8, 29, 1995)\n",
+ "x.printName()\n",
+ "print \"\\n\\t Born: \" ,\n",
+ "x.printDOB()\n",
+ "print \"\\n\\tMatriculated: \",\n",
+ "x.printDOM()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Ann Jones \n",
+ "\t Born: May 13 , 1977\n",
+ "\n",
+ "\tMatriculated: August 29 , 1995\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.5, Page no: 276"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,n,s=0,i=\"\"):\n",
+ " Person.__init__(self,n,s)\n",
+ " self.id = i\n",
+ " self.credits = 0\n",
+ " self.gpa = 0\n",
+ " self.dom = Date()\n",
+ "\n",
+ " def setDOM(self,m,d,y):\n",
+ " self.dom.setDate(m, d, y)\n",
+ " def printDOM(self):\n",
+ " self.dom.print_()\n",
+ " def printSex(self):\n",
+ " if self.sex == 1:\n",
+ " print \"male\"\n",
+ " else:\n",
+ " print 'female'\n",
+ "\n",
+ "x = Student(\"Ann Jones\", 0, \"219360061\")\n",
+ "x.setDOB(5, 13, 1977)\n",
+ "x.setDOM(8, 29, 1995)\n",
+ "x.setDOD(7,4,1826)\n",
+ "x.printName()\n",
+ "print \"\\n\\t Born: \" , \n",
+ "x.printDOB()\n",
+ "print \"\\n\\t Sex: \" ,\n",
+ "x.printSex()\n",
+ "print \"\\n\\tMatriculated: \",\n",
+ "x.printDOM()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Ann Jones \n",
+ "\t Born: May 13 , 1977\n",
+ "\n",
+ "\t Sex: female\n",
+ "\n",
+ "\tMatriculated: August 29 , 1995\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.6, Page no: 279"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " self.a = 0\n",
+ " def f(self):\n",
+ " print \"X::f() executing\"\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " self.a = 0\n",
+ " def f(self):\n",
+ " print \"Y::f() executing\"\n",
+ "x = X()\n",
+ "x.a = 22\n",
+ "x.f()\n",
+ "print \"x.a = \" , x.a\n",
+ "y = Y()\n",
+ "y.a = 44\n",
+ "# assigns 44 to the a defined in Y\n",
+ "y._X__a = 66\n",
+ "# assigns 66 to the a defined in X\n",
+ "y.f()\n",
+ "# invokes the f() defined in Y\n",
+ "X.f(x)\n",
+ "# invokes the f() defined in X\n",
+ "print \"y.a = \" , y.a \n",
+ "print \"y._X__a = \" , y._X__a \n",
+ "z = y\n",
+ "print \"z.a = \" , z._X__a \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X::f() executing\n",
+ "x.a = 22\n",
+ "Y::f() executing\n",
+ "X::f() executing\n",
+ "y.a = 44\n",
+ "y._X__a = 66\n",
+ "z.a = 66\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.7, Page no: 280"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : Python destuctor is called when program goes exit. So output may be differ than c.\n",
+ "'''\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " print \"X::X() constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"X::X() destructor executing \"\n",
+ "\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " X.__init__(self)\n",
+ " print \"Y::Y() constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"Y::Y() destructor executing \"\n",
+ "\n",
+ "class Z(Y):\n",
+ " def __init__(self,i):\n",
+ " Y.__init__(self)\n",
+ " print \"Z::Z(\" , i , \") constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"Z::Z() destructor executing \"\n",
+ " \n",
+ "\n",
+ "Z = Z(44)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X::X() constructor executing \n",
+ "Y::Y() constructor executing \n",
+ "Z::Z( 44 ) constructor executing \n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.8, Page no: 281"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : Python destuctor is called when program goes exit. So output may be differ than c.\n",
+ "'''\n",
+ "class Person:\n",
+ " def __init__(self,s):\n",
+ " self.name = s\n",
+ " def __del__(self):\n",
+ " pass\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,s,m):\n",
+ " Person.__init__(self,s)\n",
+ " self.major = m\n",
+ " def __del__(self):\n",
+ " pass\n",
+ "x = Person(\"Bob\")\n",
+ "y = Student(\"Sarah\", \"Biology\")\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.9, Page no: 282"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ " def setHSgraduate(self,g):\n",
+ " self.hs = g\n",
+ " def isHSgraduate(self):\n",
+ " return hs\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.10, Page no: 283"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : By default all methods in python are virtual. so output would be differ than c.\n",
+ "'''\n",
+ "class X:\n",
+ " def f(self):\n",
+ " print \"X::f() executing\"\n",
+ "class Y(X):\n",
+ " def f(self):\n",
+ " print \"Y::f() executing\"\n",
+ "x = X()\n",
+ "y = Y()\n",
+ "p = x\n",
+ "p.f()\n",
+ "p = y\n",
+ "p.f()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X::f() executing\n",
+ "Y::f() executing\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.11, Page no: 284"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n):\n",
+ " self.name = n\n",
+ " def print_(self):\n",
+ " print 'My name is' , self.name\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,s,g):\n",
+ " Person.__init__(self,s)\n",
+ " self.gpa = g\n",
+ " def print_(self):\n",
+ " print 'My name is ',self.name, ' and my G.P.A. is', self.gpa\n",
+ "\n",
+ "class Professor(Person):\n",
+ " def __init__(self,s,n):\n",
+ " Person.__init__(self,s)\n",
+ " self.publs = n\n",
+ " def print_(self):\n",
+ " print 'My name is ', self.name,' and i have ' , self.publs,' publications.'\n",
+ "\n",
+ "x = Person(\"Bob\")\n",
+ "p = x\n",
+ "p.print_()\n",
+ "y = Student(\"Tom\", 3.47)\n",
+ "p = y\n",
+ "p.print_()\n",
+ "z = Professor(\"Ann\", 7)\n",
+ "p = z\n",
+ "p.print_()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "My name is Bob\n",
+ "My name is Tom and my G.P.A. is 3.47\n",
+ "My name is Ann and i have 7 publications.\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.12, Page no: 285"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "This program is similar to Example 12.6:\n",
+ "'''\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " self.p = [0,0]\n",
+ " print 'X().',\n",
+ " def __del__(self):\n",
+ " print '~X().'\n",
+ "\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " X.__init__(self)\n",
+ " self.q = []\n",
+ " for i in range(1023):\n",
+ " self.q.append(0)\n",
+ " print 'Y() : Y::q = ', hex(id(self.q)) ,'.',\n",
+ " def __del__(self):\n",
+ " print '~Y().'\n",
+ "\n",
+ "for i in range(8):\n",
+ " r = Y()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.13, Page no: 288"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class Media:\n",
+ " def __init__(self):\n",
+ " self.title = ''\n",
+ " def print_(self):\n",
+ " pass\n",
+ " def id(self):\n",
+ " pass\n",
+ "\n",
+ "class Book(Media):\n",
+ " def __init__(self,a='',t=\"\",p=\"\",i=\"\"):\n",
+ " self.author = a\n",
+ " self.publisher = p\n",
+ " self.isbn = i\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \" by \" , self.author\n",
+ " def id(self):\n",
+ " return self.isbn\n",
+ "\n",
+ "class CD(Media):\n",
+ " def __init__(self,t=\"\",c=\"\",m=\"\",n=\"\"):\n",
+ " self.composer = c\n",
+ " self.make = m\n",
+ " self.number = n\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \", \" , self.composer\n",
+ " def id(self):\n",
+ " s = str(self.make) + ' ' + str(self.number)\n",
+ " return s\n",
+ "\n",
+ "class Magazine(Media):\n",
+ " def __init__(self,t=\"\",i=\"\",v=0, n=0):\n",
+ " self.issn = i\n",
+ " self.volume = v\n",
+ " self.number = n\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \" Magazine, Vol. \", self.volume , \", No.\" , self.number\n",
+ " def id(self):\n",
+ " return self.issn\n",
+ "\n",
+ "book = Book(\"Bjarne Stroustrup\", \"The C++ Programming Language\",\"Addison-Wesley\", \"0-201-53992-6\")\n",
+ "magazine = Magazine(\"TIME\", \"0040-781X\", 145, 23)\n",
+ "cd = CD(\"BACH CANTATAS\", \"Johann Sebastian Bach\",\"ARCHIV\", \"D120541\")\n",
+ "book.print_()\n",
+ "print \"\\tid: \" , book.id() \n",
+ "magazine.print_()\n",
+ "print \"\\tid: \" , magazine.id() \n",
+ "cd.print_()\n",
+ "print \"\\tid: \" , cd.id()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The C++ Programming Language by Bjarne Stroustrup\n",
+ "\tid: 0-201-53992-6\n",
+ "TIME Magazine, Vol. 145 , No. 23\n",
+ "\tid: 0040-781X\n",
+ "BACH CANTATAS , Johann Sebastian Bach\n",
+ "\tid: ARCHIV D120541\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch13.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch13.ipynb new file mode 100755 index 00000000..eea108dd --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch13.ipynb @@ -0,0 +1,426 @@ +{
+ "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": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch14.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch14.ipynb new file mode 100755 index 00000000..457669dc --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch14.ipynb @@ -0,0 +1,616 @@ +{
+ "metadata": {
+ "name": "ch14"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 14: Standard C++ Vectors"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.1, Page no: 324"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.2, Page no: 325"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.3, Page no: 326"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in range(len(v)):\n",
+ " print v[i]\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.4, Page no: 326"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.5, Page no: 327"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "w = v\n",
+ "print_(v)\n",
+ "print_(w)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n",
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.6, Page no: 327"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n",
+ "print \"v.front() = \" + v[0]\n",
+ "print \"v.back() = \" + v.pop(-1) \n",
+ "print \"v.back() = \" + v.pop(-1) \n",
+ "print \"v.back() = \" + v[-1] \n",
+ "print_(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n",
+ "v.front() = Chile\n",
+ "v.back() = Zaire\n",
+ "v.back() = Spain\n",
+ "v.back() = Nepal\n",
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.7, Page no: 328"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n",
+ "v.pop(2) # removes Italy\n",
+ "v.pop(-2) # removes Spain\n",
+ "print_(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n",
+ "Chile\n",
+ "Egypt\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Zaire\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.8, Page no: 329"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n",
+ "r = []\n",
+ "for i in range(2,len(v)-2):\n",
+ " r.append(v[i]) #removes the segment Italy..Nepal\n",
+ " \n",
+ "for i in r:\n",
+ " v.remove(i)\n",
+ "print_(v)\n",
+ "v.insert(2,\"India\")\n",
+ "print_(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n",
+ "Chile\n",
+ "Egypt\n",
+ "Spain\n",
+ "Zaire\n",
+ "Chile\n",
+ "Egypt\n",
+ "India\n",
+ "Spain\n",
+ "Zaire\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.9, Page no: 329"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ " v.append(\"India\")\n",
+ " v.append(\"China\")\n",
+ " v.append(\"Malta\")\n",
+ " v.append(\"Syria\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)\n",
+ "egypt = v.index('Egypt')\n",
+ "malta = v.index('Malta')\n",
+ "w = v[egypt:malta+1]\n",
+ "w.sort()\n",
+ "print_(w)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n",
+ "India\n",
+ "China\n",
+ "Malta\n",
+ "Syria\n",
+ "Chile\n",
+ "China\n",
+ "Egypt\n",
+ "India\n",
+ "Kenya\n",
+ "Malta\n",
+ "Nepal\n",
+ "Zaire\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.10, Page no: 329"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def copy(v,x,n):\n",
+ " for i in x:\n",
+ " v.append(i)\n",
+ "\n",
+ "def projection(v,b):\n",
+ " v_size = len(v)\n",
+ " w = []\n",
+ " for i in range(0,v_size):\n",
+ " if b[i]:\n",
+ " w.append(v[i])\n",
+ " return w\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i,\n",
+ " print ''\n",
+ "\n",
+ "x = [ 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 ]\n",
+ "v = []\n",
+ "copy(v, x, 8)\n",
+ "y = [ False, True, False, True, True, True, False, True ]\n",
+ "b = []\n",
+ "copy(b, y, 8)\n",
+ "w = projection(v, b)\n",
+ "print_(v)\n",
+ "print_(w)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n",
+ "33.3 55.5 66.6 77.7 99.9 \n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch2.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch2.ipynb new file mode 100755 index 00000000..8697f28c --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch2.ipynb @@ -0,0 +1,740 @@ +{
+ "metadata": {
+ "name": "ch2"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Chapter 2: Fundamental Types"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.1, page no. 17"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "flag=False\n",
+ "print \"flag = %r\" % flag\n",
+ "flag = True\n",
+ "print \"flag = %r\" % flag"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "flag = False\n",
+ "flag = True\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.2, page no. 19"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "c='A'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='t'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='\\t' # the tab character\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='!'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "c = A, int(c) = 65\n",
+ "c = t, int(c) = 116\n",
+ "c = \t, int(c) = 9\n",
+ "c = !, int(c) = 33\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.3, page no. 19"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "# defines the constants SHRT_MIN, etc.\n",
+ "print 'maximum limit int : ',\n",
+ "print sys.maxint\n",
+ "print 'float info'\n",
+ "print sys.float_info"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "maximum limit int : 2147483647\n",
+ "float info\n",
+ "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.4, page no. 21"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "m=54\n",
+ "n=20\n",
+ "print \"m = %d and n = %d\" %(m,n)\n",
+ "print \"m+n = %d\" % (m+n) # 54+20 = 74\n",
+ "print \"m-n = %d\" % (m-n) # 54-20 = 34\n",
+ "print \"m*n = %d\" % (m*n)# 54*20 = 1080\n",
+ "print \"m/n = %d\" % (m/n) # 54/20 = 2\n",
+ "print \"m modulo by n = %d\" % (m%n) # 54%20 = 14"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "m = 54 and n = 20\n",
+ "m+n = 74\n",
+ "m-n = 34\n",
+ "m*n = 1080\n",
+ "m/n = 2\n",
+ "m modulo by n = 14\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.5, page no. 21"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "m = 44\n",
+ "m += 1\n",
+ "n = m\n",
+ "print \"m = %d , n = %d\" %(m,n)\n",
+ "m = 44\n",
+ "n = m # the post-increment operator is applied to m\n",
+ "m += 1\n",
+ "print \"m = %d , n = %d\" %(m,n)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "m = 45 , n = 45\n",
+ "m = 45 , n = 44\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.6, page no. 22"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "n=22\n",
+ "print \"n = %d\" % n\n",
+ "n += 9 # adds 9 to n\n",
+ "print \"After n += 9, n = %d\" % n\n",
+ "n -= 5 # subtracts 5 from n\n",
+ "print \"After n -= 5, n = %d\" % n\n",
+ "n *= 2 # multiplies n by 3\n",
+ "print \"After n *= 2, n = %d\" % n \n",
+ "n /= 3 # divides n by 9\n",
+ "print \"After n /= 3, n = %d\" % n \n",
+ "n %= 7 # reduces n to the remainder from dividing by 4\n",
+ "print 'After n modulo by 7 n = %d' %n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = 22\n",
+ "After n += 9, n = 31\n",
+ "After n -= 5, n = 26\n",
+ "After n *= 2, n = 52\n",
+ "After n /= 3, n = 17\n",
+ "After n modulo by 7 n = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.7, page no. 23"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "x=54.0\n",
+ "y=20.0\n",
+ "print \"x = %f and y = %f\" %(x,y)\n",
+ "print \"x+y = %f\" %(x+y) # 54.0+20.0 = 74.0\n",
+ "print \"x-y = %f\" % (x-y) # 54.0-20.0 = 34.0\n",
+ "print \"x*y = %f\" %( x*y) # 54.0*20.0 = 1080.0\n",
+ "print \"x/y = %f\" % (x/y) # 54.0/20.0 = 2.7\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 54.000000 and y = 20.000000\n",
+ "x+y = 74.000000\n",
+ "x-y = 34.000000\n",
+ "x*y = 1080.000000\n",
+ "x/y = 2.700000\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.8, page no. 23s"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "print \"Number of bytes used:\\n\"\n",
+ "\n",
+ "print \" char: %d \" % sys.getsizeof('a')\n",
+ "print \" int : %d \" % sys.getsizeof(int(1))\n",
+ "print \" string : %d \" % sys.getsizeof(str('hellololdei'))\n",
+ "print \"float : %d\" % sys.getsizeof(float(1.1))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of bytes used:\n",
+ "\n",
+ " char: 22 \n",
+ " int : 12 \n",
+ " string : 32 \n",
+ "float : 16\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.9, page no. 24"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "fbits = 8*sys.getsizeof(float(123))\n",
+ "\n",
+ "# each byte contains 8 bits\n",
+ "print \"float uses : %d bits:\\n\\t\" % fbits "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "float uses : 128 bits:\n",
+ "\t\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.10, page no. 25"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "v = 1234.56789\n",
+ "n = int(v);\n",
+ "print \"v = %f, n = %d\" %(v,n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "v = 1234.567890, n = 1234\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.11, page no. 26"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "c='A'\n",
+ "print \"char c = \" + c\n",
+ "k=c;\n",
+ "print \"k = \" + k \n",
+ "m=k;\n",
+ "print \"m = \" + m \n",
+ "n=m\n",
+ "print \"n = \" + n\n",
+ "x=m\n",
+ "print \"x = \" + x \n",
+ "y=x\n",
+ "print \"y = \" + y"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "char c = A\n",
+ "k = A\n",
+ "m = A\n",
+ "n = A\n",
+ "x = A\n",
+ "y = A\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.12, page no. 27"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "n=1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = 1000\n",
+ "n = 1000000\n",
+ "n = 1000000000\n",
+ "n = 1000000000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.13, page no. 27"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "x=1000.0\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1000.000000\n",
+ "x = 1000000.000000\n",
+ "x = 1000000000000.000000\n",
+ "x = 999999999999999983222784.000000\n",
+ "x = 1000000000000000043845843045076197354634047651840.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.14, page no. 28"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "x = 1000/3.0\n",
+ "print \"x = %f\" %x # x = 1000/3\n",
+ "y = x - 333.0\n",
+ "print \"y = %f\" % y # y = 1/3\n",
+ "z = 3*y - 1.0\n",
+ "print \"z = %f\" %z # z = 3(1/3) - 1\n",
+ "if (z == 0):\n",
+ " print \"z == 0.\\n\"\n",
+ "else:\n",
+ " print \"z does not equal 0.\\n\" # z != 0\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 333.333333\n",
+ "y = 0.333333\n",
+ "z = -0.000000\n",
+ "z does not equal 0.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.15, page no. 28"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "\n",
+ "\n",
+ "a = float(raw_input(\"Enter the coefficients of a quadratic equation:\\n a : \"))\n",
+ "b = float(raw_input('b : '))\n",
+ "c = float(raw_input('c : '))\n",
+ "\n",
+ "print \"The equation is: \",\n",
+ "print a,\n",
+ "print \"*x*x + \",\n",
+ "print b,\n",
+ "print \"*x + \" ,\n",
+ "print c,\n",
+ "print \" = 0\" \n",
+ "\n",
+ "d = b*b - 4*a*c # discriminant\n",
+ "sqrtd = math.sqrt(d)\n",
+ "x1 = (-b + sqrtd)/(2*a)\n",
+ "x2 = (-b - sqrtd)/(2*a)\n",
+ "print \"The solutions are:\"\n",
+ "print \"\\tx1 = %f\" % x1\n",
+ "print \"\\tx2 = %f\" % x2\n",
+ "print \"Check:\" \n",
+ "print \"\\ta*x1*x1 + b*x1 + c = %f\" %( a*x1*x1 + b*x1 + c)\n",
+ "print \"\\ta*x2*x2 + b*x2 + c = %f\" %( a*x2*x2 + b*x2 + c)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the coefficients of a quadratic equation:\n",
+ " a : 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "b : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "c : -3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The equation is: 2.0 *x*x + 1.0 *x + -3.0 = 0\n",
+ "The solutions are:\n",
+ "\tx1 = 1.000000\n",
+ "\tx2 = -1.500000\n",
+ "Check:\n",
+ "\ta*x1*x1 + b*x1 + c = 0.000000\n",
+ "\ta*x2*x2 + b*x2 + c = 0.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.17, page no. 31"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "x = float(raw_input(\"Enter float: \"))\n",
+ "print \"Its reciprocal is: \",\n",
+ "print 1/x "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter float: 234.567e89\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Its reciprocal is: 4.2631742743e-92\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.18, page no. 31"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "x = 11\n",
+ "# ERROR: this is not in the scope of x\n",
+ "if True:\n",
+ " x = 22 # OK: this is in the scope of x\n",
+ " y = 33 # ERROR: this is not in the scope of y\n",
+ " x = 44 # OK: this is in the scope of x\n",
+ " y = 55 # OK: this is in the scope of y\n",
+ "x = 66 # OK: this is in the scope of x\n",
+ "y = 77 # ERROR: this is not in the scope of y\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.19, page no. 32"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# this x is global\n",
+ "x = 11\n",
+ "\n",
+ "if True:\n",
+ " # illustrates the nested and parallel scopes:\n",
+ " x = 22\n",
+ " # begin scope of internal block\n",
+ " if True:\n",
+ " x = 33\n",
+ " print \"In block inside main(): x = %d \" % x\n",
+ " # end scope of internal block\n",
+ "print \"In main(): x = %d\" %x \n",
+ "print \"In main(): x = %d \"% x"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In block inside main(): x = 33 \n",
+ "In main(): x = 33\n",
+ "In main(): x = 33 \n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch3.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch3.ipynb new file mode 100644 index 00000000..4c034b7e --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch3.ipynb @@ -0,0 +1,1167 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3: Selection" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.1, page no. 36" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter two positive integers: \";\n", + "n = int(raw_input())\n", + "d = int(raw_input())\n", + "if (n%d):\n", + " print \"%d is not divisible by %d\" %(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two positive integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "66\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "66 is not divisible by 7\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.2, page no. 37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter two positive integers: \";\n", + "n = int(raw_input())\n", + "d = int(raw_input())\n", + "if (n%d):\n", + " print \"%d is not divisible by %d\" %(n,d)\n", + "else:\n", + " print \"%d is divisible by %d\" %(n,d)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two positive integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "56\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "56 is divisible by 7\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.3, page no. 38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter two integers: \"\n", + "m = int(raw_input())\n", + "n = int(raw_input())\n", + "\n", + "if (m < n):\n", + " print \"%d is the minimum.\" %m\n", + "else:\n", + " print \"%d is the minimum.\" %n\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "55 is the minimum.\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.4, page no. 38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter an integer: \"\n", + "n = int(raw_input())\n", + "if (n == 22):\n", + " print \"%d = 22\" %n\n", + "else: \n", + " print \"%d != 22\" %n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "22 = 22\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.5, page no. 39" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter three integers: \"\n", + "n1 = int(raw_input())\n", + "n2 = int(raw_input())\n", + "n3 = int(raw_input())\n", + "\n", + "m=n1\n", + "# now min <= n1\n", + "if (n2 < m):\n", + " m = n2 # now min <= n1 and min <= n2\n", + "if (n3 < m):\n", + " m = n3 # now min <= n1, min <= n2, and min <= n3\n", + "print \"Their minimum is %d\" % m" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Their minimum is 33\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.6, page no. 39" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter two integers: \"\n", + "x = int(raw_input())\n", + "y = int(raw_input())\n", + "\n", + "if (x > y):\n", + " temp=x\n", + " x = y\n", + " y = temp\n", + " \n", + "\n", + "print \"%d <= %d\" %(x,y)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "66\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "44\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "44 <= 66\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.7, page no. 40" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "n=44\n", + "print \"n = %d\" % n \n", + "if True:\n", + " # scope extends over 4 lines\n", + " print \"Enter an integer: \"\n", + " n = int(raw_input())\n", + " print \"n = %d\" % n \n", + "\n", + "if True:\n", + " print \"n = %d\" % n \n", + " # the n that was declared first\n", + "if True:\n", + " print \"n = %d\" % n \n", + "\n", + "print \"n = %d\" % n " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "n = 44\n", + "Enter an integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "n = 77\n", + "n = 77\n", + "n = 77\n", + "n = 77\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.8, page no. 41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter three integers: \"\n", + "n1 = int(raw_input())\n", + "n2 = int(raw_input())\n", + "n3 = int(raw_input())\n", + "if (n1 <= n2 and n1 <= n3):\n", + " print \"Their minimum is %d\" % n1\n", + " \n", + "if (n2 <= n1 and n2 <= n3):\n", + " print \"Their minimum is %d \" % n2 \n", + "if (n3 <= n1 and n3 <= n2):\n", + " print \"Their minimum is %d\" % n3 \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Their minimum is 33 \n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.9, page no. 41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Are you enrolled (y/n): \"\n", + "ans = raw_input()\n", + "if (ans == 'Y' or ans == 'y'):\n", + " print \"You are enrolled.\\n\"\n", + "else: \n", + " print \"You are not enrolled.\\n\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Are you enrolled (y/n): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You are enrolled.\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.10, page no. 42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Enter two positive integers: \";\n", + "n = int(raw_input())\n", + "d = int(raw_input())\n", + "\n", + "if (d != 0 and n%d == 0): \n", + " print \"%d divides %d\" %(d,n)\n", + "else:\n", + " print \"%d does not divide %d\"% (d,n)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two positive integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "6 does not divide 33\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.11, page no. 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter three integers: \"\n", + "n1 = int(raw_input())\n", + "n2 = int(raw_input())\n", + "n3 = int(raw_input())\n", + "\n", + "if (n1 >= n2 >= n3):\n", + " print \"max = x\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.12, page no. 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter two positive integers: \"\n", + "n = int(raw_input())\n", + "d = int(raw_input())\n", + "\n", + "if (d != 0):\n", + " if (n%d == 0):\n", + " print d,\n", + " print \" divides %d\" % n \n", + " else:\n", + " print \"%d does not divide %d\" %(d,n)\n", + "else:\n", + " print '%d does not divide %d '%(d,n)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two positive integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "44\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "44 does not divide 55\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.13, page no. 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter three integers: \"\n", + "n1 = int(raw_input())\n", + "n2 = int(raw_input())\n", + "n3 = int(raw_input())\n", + "if (n1 < n2):\n", + " if (n1 < n3):\n", + " print \"Their minimum is : %d\" % n1\n", + " else:\n", + " print \"Their minimum is : %d\" % n3\n", + "else: # n1 >= n2\n", + " if (n2 < n3):\n", + " print \"Their minimum is : %d\" % n2\n", + " else:\n", + " print \"Their minimum is %d\" % n3" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Their minimum is : 33\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.14, page no. 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Pick a number from 1 to 8.\" \n", + "answer = int(raw_input())\n", + "print \"Is it less than 5? (y|n): \"\n", + "answer = raw_input()\n", + "if (answer == 'y'): # 1 <= n <= 4\n", + " print \"Is it less than 3? (y|n): \"\n", + " answer = raw_input() \n", + " if (answer == 'y'): # 1 <= n <= 2\n", + " print \"Is it less than 2? (y|n): \"\n", + " answer = raw_input()\n", + " if (answer == 'y'):\n", + " print \"Your number is 1.\"\n", + " else:\n", + " print \"Your number is 2.\"\n", + " else: # 3 <= n <= 4\n", + " print \"Is it less than 4? (y|n): \"\n", + " answer = raw_input()\n", + " if (answer == 'y'):\n", + " print \"Your number is 3.\"\n", + " else:\n", + " print \"Your number is 4.\"\n", + "else: # 5 <= n <= 8\n", + " print \"Is it less than 7? (y|n): \"\n", + " answer = raw_input()\n", + " if (answer == 'y'): # 5 <= n <= 6\n", + " print \"Is it less than 6? (y|n): \"\n", + " answer = raw_input()\n", + " if (answer == 'y'):\n", + " print \"Your number is 5.\"\n", + " else:\n", + " print \"Your number is 6.\" \n", + " else: # 7 <= n <= 8\n", + " print \"Is it less than 8? (y|n): \"\n", + " answer = raw_input()\n", + " if (answer == 'y'):\n", + " print \"Your number is 7.\" \n", + " else:\n", + " print \"Your number is 8.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Pick a number from 1 to 8.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Is it less than 5? (y|n): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Is it less than 7? (y|n): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Is it less than 6? (y|n): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your number is 6.\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.15, page no. 46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "language = raw_input(\"Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): \")\n", + "\n", + "if (language == 'e'): \n", + " print \"Welcome to ProjectEuclid.\"\n", + "elif (language == 'f'):\n", + " print \"Bon jour, ProjectEuclid.\"\n", + "elif (language == 'g'):\n", + " print \"Guten tag, ProjectEuclid.\"\n", + "elif (language == 'i'):\n", + " print \"Bon giorno, ProjectEuclid.\"\n", + "elif (language == 'r'):\n", + " print \"Dobre utre, ProjectEuclid.\"\n", + "else:\n", + " print \"Sorry; we don't speak your language.\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bon giorno, ProjectEuclid.\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.16, page no. 47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "score = int(raw_input(\"Enter your test score: \"))\n", + "a = int(score/10)\n", + "if a == 10 or a == 9:\n", + " print \"Your grade is an A.\"\n", + "elif a == 8:\n", + " print \"Your grade is a B.\" \n", + "elif a == 7:\n", + " print \"Your grade is a C.\" \n", + "elif a == 6:\n", + " print \"Your grade is a D.\"\n", + "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n", + " print \"Your grade is an F.\" \n", + "else:\n", + " print \"Error: score is out of range.\\n\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your test score: 83\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your grade is a B.\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.17, page no. 47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "score = int(raw_input(\"Enter your test score: \"))\n", + "a = int(score/10)\n", + "if a == 10 or a == 9:\n", + " print \"Your grade is an A.\"\n", + "elif a == 8:\n", + " print \"Your grade is a B.\" \n", + "elif a == 7:\n", + " print \"Your grade is a C.\" \n", + "elif a == 6:\n", + " print \"Your grade is a D.\"\n", + "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n", + " print \"Your grade is an F.\" \n", + "else:\n", + " print \"Error: score is out of range.\\n\"\n", + "\n", + "print \"Goodbye.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your test score: 83\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your grade is a B.\n", + "Goodbye.\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.18, page no. 48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "score = int(raw_input(\"Enter your test score: \"))\n", + "a = int(score/10)\n", + "if a == 10 or a == 9:\n", + " print \"Your grade is an A.\"\n", + "elif a == 8:\n", + " print \"Your grade is a B.\" \n", + "elif a == 7:\n", + " print \"Your grade is a C.\" \n", + "elif a == 6:\n", + " print \"Your grade is a D.\"\n", + "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n", + " print \"Your grade is an F.\" \n", + "else:\n", + " print \"Error: score is out of range.\\n\"\n", + "\n", + "print \"Goodbye.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your test score: 83\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your grade is a B.\n", + "Goodbye.\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.19, page no. 49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter two integers: \"\n", + "m = int(raw_input())\n", + "n = int(raw_input())\n", + "print min(m,n),\n", + "print 'is the minimum'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "33 is the minimum\n" + ] + } + ], + "prompt_number": 19 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch4.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch4.ipynb new file mode 100644 index 00000000..35a47817 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch4.ipynb @@ -0,0 +1,1633 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Iteration" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.1, page no. 60" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i=1\n", + "print \"Enter a positive integer: \"\n", + "n = int(raw_input())\n", + "s=0\n", + "while (i <= n):\n", + " s += i\n", + " i += 1\n", + "print \"The sum of the first %d integers is %d\" %(i,s)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the first 6 integers is 15\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.2, page no. 61" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter a positive integer: \"\n", + "bound = int(raw_input())\n", + "s=0.0\n", + "i=0\n", + "while (s < bound):\n", + " i += 1\n", + " s += 1.0/i\n", + "\n", + "print \"The sum of the first %d reciprocals is %f\" %(i,s)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the first 83 reciprocals is 5.002068\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.3, page no. 61" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "print \"Enter a positive number: \"\n", + "x = float(raw_input())\n", + "while (x > 0):\n", + " print \"sqrt(%d) = %f \"%(x,math.sqrt(x))\n", + " print \"Enter another positive number (or 0 to quit): \"\n", + " x = float(raw_input())\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive number: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sqrt(5) = 2.236068 \n", + "Enter another positive number (or 0 to quit): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sqrt(3) = 1.732051 \n", + "Enter another positive number (or 0 to quit): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.4, page no. 62" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i=1\n", + "print \"Enter a positive integer: \";\n", + "n = int(raw_input())\n", + "s=0\n", + "while(True):\n", + " if (i > n):\n", + " break # terminates the loop immediately\n", + " s += i\n", + " i += 1\n", + "print \"The sum of the first %d integers is %d\" %(n,s)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the first 5 integers is 15\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.5, page no. 62\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter a positive integer: \"\n", + "bound = int(raw_input())\n", + "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n", + "f0=0\n", + "f1=1\n", + "while (True):\n", + " f2 = f0 + f1\n", + " if (f2 > bound):\n", + " break\n", + " print \", %d\" % f2,\n", + " f0 = f1\n", + " f1 = f2\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fibonacci numbers < 10:\n", + "0, 1 , 1 , 2 , 3 , 5 , 8\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.6, page no. 63" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "print \"Enter a positive integer: \"\n", + "bound = int(raw_input())\n", + "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n", + "f0=0\n", + "f1=1\n", + "while (True):\n", + " f2 = f0 + f1\n", + " if (f2 > bound):\n", + " sys.exit(0)\n", + " print \", %d\" % f2,\n", + " f0 = f1\n", + " f1 = f2\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "ename": "SystemExit", + "evalue": "0", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fibonacci numbers < 10:\n", + "0, 1 , 1 , 2 , 3 , 5 , 8" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D.\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.7, page no. 63" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter a positive integer: \"\n", + "bound = int(raw_input())\n", + "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n", + "f0=0\n", + "f1=1\n", + "# Error : infinite loop !\n", + "while (True):\n", + " f2 = f0 + f1\n", + " # By commenting the below if statement, it goes to infinite.\n", + " if (f2 > bound):\n", + " break\n", + " print \", %d\" % f2,\n", + " f0 = f1\n", + " f1 = f2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n", + "Fibonacci numbers < 10:\n", + "0, 1 , 1 , 2 , 3 , 5 , 8\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.8, page no. 64" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i=0\n", + "print \"Enter a positive integer: \"\n", + "n = int(raw_input())\n", + "s=0\n", + "while i<=n:\n", + " s += i\n", + " i += 1\n", + "print \"The sum of the first %d integers is %d\" %(n,s)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the first 10 integers is 55\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.9, page no. 65" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter a positive integer: \"\n", + "bound = int(raw_input())\n", + "print \"Factorial numbers < %d:\\n1, 1\" %bound,\n", + "f=1\n", + "i=1\n", + "while f < bound:\n", + " i += 1\n", + " f *= i\n", + " print \", %d\" %f,\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial numbers < 10:\n", + "1, 1 , 2 , 6 , 24\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.10, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter a positive integer: \"\n", + "n = int(raw_input())\n", + "s=0;\n", + "for i in range(0,n+1):\n", + " s += i\n", + "print \"The sum of the first %d integers is %d\" %(n,s)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the first 10 integers is 55\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.11, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter a positive integer: \"\n", + "n = int(raw_input())\n", + "s=0\n", + "for i in range(1,n/2): # the scope of this i is this loop\n", + " s += i\n", + "\n", + "for i in range(n/2,n+1): # the scope of this i is this loop\n", + " s += i\n", + "print \"The sum of the first %d integers is %d\" % (n,s)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the first 10 integers is 55\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.12, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter a positive integer: \"\n", + "bound = int(raw_input())\n", + "\n", + "print \"Factorial numbers that are <= %d:\\n1, 1\" %bound,\n", + "f=1\n", + "for i in range(2,bound+1):\n", + " f *= i\n", + " print \", %d\" % f,\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial numbers that are <= 10:\n", + "1, 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 , 3628800\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.13, page no. 67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "for i in range(10,0,-1):\n", + " print i,\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10 9 8 7 6 5 4 3 2 1\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.14, page no. 67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "prime = True\n", + "print \"Enter a positive integer: \"\n", + "n = int(raw_input())\n", + "if (n < 2):\n", + " print \"%d is not prime.\" %n\n", + " prime = False\n", + "elif (n < 4):\n", + " print \"%d is prime.\" %n\n", + " prime = False\n", + "elif (n%2 == 0):\n", + " print \"%d = 2* %d\" %(n,n/2)\n", + " prime = False\n", + "else:\n", + " for d in range(3,n/2+1):\n", + " if (n%d == 0):\n", + " print \"%d = %d * %d\" %(n,d,n/d)\n", + " prime = False\n", + "if prime: \n", + " print \"%d is prime.\"%n\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "11 is prime.\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.15, page no. 68" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter positive integers (0 to quit): \";\n", + "n = int(raw_input())\n", + "m = n\n", + "while n > 0:\n", + " n = int(raw_input())\n", + " if n > m :\n", + " m = n\n", + "\n", + "print \"max = %d\" % m" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter positive integers (0 to quit): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "19\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "42\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "max = 42\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.16, page no. 68" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter positive integers (0 to quit): \";\n", + "n = int(raw_input())\n", + "m = n\n", + "while n > 0: \n", + " if n < m :\n", + " m = n\n", + " n = int(raw_input())\n", + "\n", + "print \"min = %d\" % m\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter positive integers (0 to quit): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "19\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "42\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "min = 1\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.17, page no. 69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "m = 95\n", + "n = 11\n", + "while m%n > 0:\n", + " print \"%d modulo %d = %d\" %(m,n,m%n)\n", + " m -= 3\n", + " n += 1\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "95 modulo 11 = 7\n", + "92 modulo 12 = 8\n", + "89 modulo 13 = 11\n", + "86 modulo 14 = 2\n", + "83 modulo 15 = 8\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.18, page no. 69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for x in range(1,13):\n", + " for y in range(1,13):\n", + " print \"%4d\" % (x*y),\n", + " print \"\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1 2 3 4 5 6 7 8 9 10 11 12 \n", + " 2 4 6 8 10 12 14 16 18 20 22 24 \n", + " 3 6 9 12 15 18 21 24 27 30 33 36 \n", + " 4 8 12 16 20 24 28 32 36 40 44 48 \n", + " 5 10 15 20 25 30 35 40 45 50 55 60 \n", + " 6 12 18 24 30 36 42 48 54 60 66 72 \n", + " 7 14 21 28 35 42 49 56 63 70 77 84 \n", + " 8 16 24 32 40 48 56 64 72 80 88 96 \n", + " 9 18 27 36 45 54 63 72 81 90 99 108 \n", + " 10 20 30 40 50 60 70 80 90 100 110 120 \n", + " 11 22 33 44 55 66 77 88 99 110 121 132 \n", + " 12 24 36 48 60 72 84 96 108 120 132 144 \n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.19, page no. 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "# defines pow() and log()\n", + "\n", + "print \"Enter a positive integer: \"\n", + "n = int(raw_input())\n", + "d=0 # the discrete binary logarithm of n\n", + "p2d=1 # = 2^d\n", + "i = n\n", + "while i > 1:\n", + " # INVARIANT: 2^d <= n/i < 2*2^d\n", + " p2d=math.pow(2,d) # = 2^d\n", + " print \"%2d <= %2d\" %(p2d,2*p2d)\n", + " i /= 2\n", + " d += 1\n", + "\n", + "p2d=math.pow(2,d) # = 2^d\n", + "print \"%2d <= %2d < %2d\" %(p2d,n,2*p2d)\n", + "print \" The discrete binary logarithm of is %d\" % d \n", + "lgn = math.log(n)/math.log(2) # base 2 logarithm\n", + "print \"The continuous binary logarithm of is %f\" % lgn" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "17\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1 <= 2\n", + " 2 <= 4\n", + " 4 <= 8\n", + " 8 <= 16\n", + "16 <= 17 < 32\n", + " The discrete binary logarithm of is 4\n", + "The continuous binary logarithm of is 4.087463\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.20, page no. 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i=1\n", + "print \"Enter a positive integer: \"\n", + "n = int(raw_input())\n", + "s=0\n", + "while (True):\n", + " if (i > n):\n", + " break\n", + " s += i\n", + " i += 1\n", + "\n", + "print \"The sum of the first %d integers is %d\" %(i,s)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive integer: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the first 11 integers is 55\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.21, page no. 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "count=0\n", + "s=0\n", + "print \"Enter positive integers (0 to quit):\" \n", + "while True: # \"forever\"\n", + " print \"\\t %d :\" %(count + 1),\n", + " n = int(raw_input())\n", + " if (n <= 0):\n", + " break\n", + " count += 1\n", + " s += n\n", + "\n", + "print \"The average of those %d positive numbers is \" %count,\n", + "print float(s)/count\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter positive integers (0 to quit):\n", + "\t 1 :" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \t 2 :" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \t 3 :" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \t 4 :" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The average of those 3 positive numbers is 18.3333333333\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.22, page no. 72" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for x in range(1,13):\n", + " for y in range(1,13):\n", + " if y>x:\n", + " break\n", + " else:\n", + " print '%4d' %(x*y),\n", + " print ''\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1 \n", + " 2 4 \n", + " 3 6 9 \n", + " 4 8 12 16 \n", + " 5 10 15 20 25 \n", + " 6 12 18 24 30 36 \n", + " 7 14 21 28 35 42 49 \n", + " 8 16 24 32 40 48 56 64 \n", + " 9 18 27 36 45 54 63 72 81 \n", + " 10 20 30 40 50 60 70 80 90 100 \n", + " 11 22 33 44 55 66 77 88 99 110 121 \n", + " 12 24 36 48 60 72 84 96 108 120 132 144 \n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.23, page no. 73" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "while True:\n", + " n = int(raw_input('Enter int : '))\n", + " if (n%2 == 0):\n", + " continue\n", + " if (n%3 == 0):\n", + " break\n", + " print \"\\tBottom of loop.\\n\"\n", + "print \"\\tOutside of loop.\\n\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter int : 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tBottom of loop.\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter int : 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter int : 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter int : 9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tOutside of loop.\n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.24, page no. 74" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "N=5\n", + "done=False\n", + "for i in range(N):\n", + " for j in range(N):\n", + " if done:\n", + " break\n", + " for k in range(N):\n", + " if done:\n", + " break\n", + " if (i+j+k>N):\n", + " done = True\n", + " else:\n", + " print i+j+k,\n", + " print \" \",\n", + " print \"* \"\n", + " print \".\" \n", + " done = False\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 1 2 3 4 * \n", + "1 2 3 4 5 * \n", + "2 3 4 5 * \n", + ".\n", + "1 2 3 4 5 * \n", + "2 3 4 5 * \n", + ".\n", + "2 3 4 5 * \n", + ".\n", + "3 4 5 * \n", + ".\n", + "4 5 * \n", + ".\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.25, page no. 75" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "N=5\n", + "done=False\n", + "for i in range(N):\n", + " for j in range(N):\n", + " if done:\n", + " break\n", + " for k in range(N):\n", + " if done:\n", + " break\n", + " if (i+j+k>N):\n", + " done = True\n", + " else:\n", + " print i+j+k,\n", + " print \" \",\n", + " print \"* \"\n", + " print \".\" \n", + " done = False\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 1 2 3 4 * \n", + "1 2 3 4 5 * \n", + "2 3 4 5 * \n", + ".\n", + "1 2 3 4 5 * \n", + "2 3 4 5 * \n", + ".\n", + "2 3 4 5 * \n", + ".\n", + "3 4 5 * \n", + ".\n", + "4 5 * \n", + ".\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.26, page no. 76" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import random\n", + "\n", + "# prints pseudo-random numbers:\n", + "\n", + "for i in range(0,8):\n", + " print random.random()\n", + "\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.702115758628\n", + "0.969460447904\n", + "0.409934401112\n", + "0.700339443791\n", + "0.093528851602\n", + "0.132172955687\n", + "0.0162887279366\n", + "0.943010713478\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.27, page no. 77" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import random\n", + "# prints pseudo-random numbers:\n", + "print \"Enter seed: \"\n", + "seed = int(raw_input())\n", + "random.seed(seed);\n", + "for i in range(0,8):\n", + " print random.random()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter seed: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.62290169489\n", + "0.741786989261\n", + "0.795193565566\n", + "0.942450283777\n", + "0.73989857474\n", + "0.922324996665\n", + "0.0290052282836\n", + "0.465622654378\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.28, page no. 78" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import random\n", + "for i in range(0,8):\n", + " print random.random()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.943356716998\n", + "0.648974553137\n", + "0.900900491751\n", + "0.113205964653\n", + "0.469069047782\n", + "0.24657283262\n", + "0.543760859236\n", + "0.573941187928\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.29, page no. 79" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import random\n", + "print \"Enter minimum and maximum: \"\n", + "m = int(raw_input())\n", + "n = int(raw_input())\n", + "# lowest and highest numbers\n", + "r = n - m + 1\n", + "# number of numbers in range\n", + "for i in range(0,20):\n", + " j = int(random.random()*100 % r + m)\n", + " print j,\n", + " print \" \",\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter minimum and maximum: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "6 15 10 8 15 9 7 7 11 6 5 15 14 15 15 15 11 13 14 6 \n" + ] + } + ], + "prompt_number": 24 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch5.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch5.ipynb new file mode 100755 index 00000000..1fddf23f --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch5.ipynb @@ -0,0 +1,1746 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 5: Functions\n" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.1, Page no:87" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import math\n", + "\n", + "# tests the sqrt() function:\n", + "for i in range(0,6):\n", + " print \"\\t %d \\t %f\" %(i,math.sqrt(i))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\t 0 \t 0.000000\n", + "\t 1 \t 1.000000\n", + "\t 2 \t 1.414214\n", + "\t 3 \t 1.732051\n", + "\t 4 \t 2.000000\n", + "\t 5 \t 2.236068\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.2, Page no:88" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import math\n", + "# tests the identity sin 2x = 2 sin x cos x:\n", + "x = 0\n", + "while x < 2:\n", + " print \"%f \\t\\t %f \\t %f\" %(x,math.sin(2*x),2*math.sin(x)*math.cos(x))\n", + " x += 0.2\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.000000 \t\t 0.000000 \t 0.000000\n", + "0.200000 \t\t 0.389418 \t 0.389418\n", + "0.400000 \t\t 0.717356 \t 0.717356\n", + "0.600000 \t\t 0.932039 \t 0.932039\n", + "0.800000 \t\t 0.999574 \t 0.999574\n", + "1.000000 \t\t 0.909297 \t 0.909297\n", + "1.200000 \t\t 0.675463 \t 0.675463\n", + "1.400000 \t\t 0.334988 \t 0.334988\n", + "1.600000 \t\t -0.058374 \t -0.058374\n", + "1.800000 \t\t -0.442520 \t -0.442520\n", + "2.000000 \t\t -0.756802 \t -0.756802\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.3, Page no:90" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def cube(x):\n", + " # returns cube of x:\n", + " return x*x*x\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.4, Page no:91" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def cube(x):\n", + " # returns cube of x:\n", + " return x*x*x\n", + "\n", + "# tests the cube() function:\n", + "n=1\n", + "while (n != 0):\n", + " n = int(raw_input())\n", + " print \"\\tcube( %d ) = %d\" %(n,cube(n))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tcube( 4 ) = 64\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tcube( 2 ) = 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tcube( 9 ) = 729\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tcube( 0 ) = 0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.5, Page no:90" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def maximum(x,y):\n", + " # returns larger of the two given integers:\n", + " if (x < y):\n", + " return y\n", + " else:\n", + " return x\n", + "\n", + "# tests the max() function:\n", + "m = 1\n", + "n = 1\n", + "while m != 0: \n", + " m = int(raw_input())\n", + " n = int(raw_input())\n", + " print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax( 5 , 2 ) = 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax( 0 , 3 ) = 3\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.6, Page no:93" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def maximum(x,y):\n", + " # returns larger of the two given integers:\n", + " if (x < y):\n", + " return y\n", + " else:\n", + " return x\n", + "\n", + "# tests the max() function:\n", + "m = 1\n", + "n = 1\n", + "while m != 0: \n", + " m = int(raw_input())\n", + " n = int(raw_input())\n", + " print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax( 5 , 2 ) = 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax( 0 , 3 ) = 3\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.8, Page no:94" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "# returns larger of the two given integers:\n", + "\n", + "m = 1\n", + "n = 1\n", + "while m!=0:\n", + " m = int(raw_input())\n", + " n = int(raw_input())\n", + " print \"\\tmax(%d,%d) = %d\" %(m,n, max(m,n))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax(5,4) = 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax(4,3) = 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax(8,0) = 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tmax(0,5) = 5\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.9, Page no:95" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def fact(n):\n", + " if (n < 0):\n", + " return 0\n", + " f = 1\n", + " while (n > 1):\n", + " f *= n\n", + " n -= 1\n", + " return f\n", + "\n", + "for i in range(-1,6):\n", + " print fact(i),\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 1 1 2 6 24 120\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.10, Page no:95" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def fact(n):\n", + " if (n < 0):\n", + " return 0\n", + " f = 1\n", + " while (n > 1):\n", + " f *= n\n", + " n -= 1\n", + " return f\n", + "\n", + "\n", + "def perm(n,k):\n", + " # returns P(n,k), the number of permutations of k from n:\n", + " if (n < 0 or k < 0 or k > n):\n", + " return 0\n", + " return fact(n)/fact(n-k)\n", + "\n", + "for i in range(-1,8):\n", + " for j in range(-1,i+2):\n", + " print perm(i,j),\n", + " print ''\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 0 \n", + "0 1 0 \n", + "0 1 1 0 \n", + "0 1 2 2 0 \n", + "0 1 3 6 6 0 \n", + "0 1 4 12 24 24 0 \n", + "0 1 5 20 60 120 120 0 \n", + "0 1 6 30 120 360 720 720 0 \n", + "0 1 7 42 210 840 2520 5040 5040 0 \n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.11, Page no:96" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def printDate(m,d,y):\n", + " # prints the given date in literal form:\n", + " if (m < 1 or m > 12 or d < 1 or d > 31 or y < 0):\n", + " print \"Error: parameter out of range.\\n\"\n", + " return\n", + " if m == 1:\n", + " print \"January \",\n", + " elif m ==2:\n", + " print \"February \",\n", + " elif m==3 :\n", + " print \"March \",\n", + " elif m==4:\n", + " print \"April \",\n", + " elif m==5:\n", + " print \"May \",\n", + " elif m==6:\n", + " print \"June \",\n", + " elif m==7:\n", + " print \"July \",\n", + " elif m==8:\n", + " print \"August \",\n", + " elif m==9:\n", + " print \"September \",\n", + " elif m==10:\n", + " print \"October \",\n", + " elif m==1:\n", + " print \"November \",\n", + " else:\n", + " print \"December \",\n", + " print d , \", \", y \n", + "\n", + "# tests the printDate() function:\n", + "month = 1\n", + "while month > 0:\n", + " month = int(raw_input())\n", + " day = int(raw_input())\n", + " year = int(raw_input())\n", + " printDate(month,day,year)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1989\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "September 12 , 1989\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2001\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Error: parameter out of range.\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.12, Page no:98" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import string\n", + "def ispunct(s):\n", + " return all(c in string.punctuation for c in s)\n", + "def printCharCategory(c):\n", + " # prints the category to which the given character belongs:\n", + " print \"The character [\" + c + \"] is a \",\n", + " if(c.isdigit()):\n", + " print \"digit.\\n\"\n", + " elif (c.islower()):\n", + " print \"lower-case letter.\\n\"\n", + " elif (c.isupper()): \n", + " print \"capital letter.\\n\"\n", + " elif (c.isspace()):\n", + " print \"white space character.\\n\"\n", + " elif (ord(c) >= 10 and ord(c) <= 15 or ord(c) == 0):\n", + " print \"control character.\\n\"\n", + " elif (ispunct(c)):\n", + " print \"punctuation mark.\\n\"\n", + " else:\n", + " print \"Error.\\n\"\n", + "\n", + "# prints the category to which the given character belongs;\n", + "# tests the printCharCategory() function:\n", + "for c in range(128):\n", + " printCharCategory(chr(c))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The character [\u0000] is a control character.\n", + "\n", + "The character [\u0001] is a Error.\n", + "\n", + "The character [\u0002] is a Error.\n", + "\n", + "The character [\u0003] is a Error.\n", + "\n", + "The character [\u0004] is a Error.\n", + "\n", + "The character [\u0005] is a Error.\n", + "\n", + "The character [\u0006] is a Error.\n", + "\n", + "The character [\u0007] is a Error.\n", + "\n", + "The character [\b] is a Error.\n", + "\n", + "The character [\t] is a white space character.\n", + "\n", + "The character [\n", + "] is a white space character.\n", + "\n", + "The character [\u000b", + "] is a white space character.\n", + "\n", + "The character [\f", + "] is a white space character.\n", + "\n", + "The character [\r", + "] is a white space character.\n", + "\n", + "The character [\u000e] is a control character.\n", + "\n", + "The character [\u000f] is a control character.\n", + "\n", + "The character [\u0010] is a Error.\n", + "\n", + "The character [\u0011] is a Error.\n", + "\n", + "The character [\u0012] is a Error.\n", + "\n", + "The character [\u0013] is a Error.\n", + "\n", + "The character [\u0014] is a Error.\n", + "\n", + "The character [\u0015] is a Error.\n", + "\n", + "The character [\u0016] is a Error.\n", + "\n", + "The character [\u0017] is a Error.\n", + "\n", + "The character [\u0018] is a Error.\n", + "\n", + "The character [\u0019] is a Error.\n", + "\n", + "The character [\u001a] is a Error.\n", + "\n", + "The character [\u001b] is a Error.\n", + "\n", + "The character [\u001c", + "] is a Error.\n", + "\n", + "The character [\u001d", + "] is a Error.\n", + "\n", + "The character [\u001e", + "] is a Error.\n", + "\n", + "The character [\u001f] is a Error.\n", + "\n", + "The character [ ] is a white space character.\n", + "\n", + "The character [!] is a punctuation mark.\n", + "\n", + "The character [\"] is a punctuation mark.\n", + "\n", + "The character [#] is a punctuation mark.\n", + "\n", + "The character [$] is a punctuation mark.\n", + "\n", + "The character [%] is a punctuation mark.\n", + "\n", + "The character [&] is a punctuation mark.\n", + "\n", + "The character ['] is a punctuation mark.\n", + "\n", + "The character [(] is a punctuation mark.\n", + "\n", + "The character [)] is a punctuation mark.\n", + "\n", + "The character [*] is a punctuation mark.\n", + "\n", + "The character [+] is a punctuation mark.\n", + "\n", + "The character [,] is a punctuation mark.\n", + "\n", + "The character [-] is a punctuation mark.\n", + "\n", + "The character [.] is a punctuation mark.\n", + "\n", + "The character [/] is a punctuation mark.\n", + "\n", + "The character [0] is a digit.\n", + "\n", + "The character [1] is a digit.\n", + "\n", + "The character [2] is a digit.\n", + "\n", + "The character [3] is a digit.\n", + "\n", + "The character [4] is a digit.\n", + "\n", + "The character [5] is a digit.\n", + "\n", + "The character [6] is a digit.\n", + "\n", + "The character [7] is a digit.\n", + "\n", + "The character [8] is a digit.\n", + "\n", + "The character [9] is a digit.\n", + "\n", + "The character [:] is a punctuation mark.\n", + "\n", + "The character [;] is a punctuation mark.\n", + "\n", + "The character [<] is a punctuation mark.\n", + "\n", + "The character [=] is a punctuation mark.\n", + "\n", + "The character [>] is a punctuation mark.\n", + "\n", + "The character [?] is a punctuation mark.\n", + "\n", + "The character [@] is a punctuation mark.\n", + "\n", + "The character [A] is a capital letter.\n", + "\n", + "The character [B] is a capital letter.\n", + "\n", + "The character [C] is a capital letter.\n", + "\n", + "The character [D] is a capital letter.\n", + "\n", + "The character [E] is a capital letter.\n", + "\n", + "The character [F] is a capital letter.\n", + "\n", + "The character [G] is a capital letter.\n", + "\n", + "The character [H] is a capital letter.\n", + "\n", + "The character [I] is a capital letter.\n", + "\n", + "The character [J] is a capital letter.\n", + "\n", + "The character [K] is a capital letter.\n", + "\n", + "The character [L] is a capital letter.\n", + "\n", + "The character [M] is a capital letter.\n", + "\n", + "The character [N] is a capital letter.\n", + "\n", + "The character [O] is a capital letter.\n", + "\n", + "The character [P] is a capital letter.\n", + "\n", + "The character [Q] is a capital letter.\n", + "\n", + "The character [R] is a capital letter.\n", + "\n", + "The character [S] is a capital letter.\n", + "\n", + "The character [T] is a capital letter.\n", + "\n", + "The character [U] is a capital letter.\n", + "\n", + "The character [V] is a capital letter.\n", + "\n", + "The character [W] is a capital letter.\n", + "\n", + "The character [X] is a capital letter.\n", + "\n", + "The character [Y] is a capital letter.\n", + "\n", + "The character [Z] is a capital letter.\n", + "\n", + "The character [[] is a punctuation mark.\n", + "\n", + "The character [\\] is a punctuation mark.\n", + "\n", + "The character []] is a punctuation mark.\n", + "\n", + "The character [^] is a punctuation mark.\n", + "\n", + "The character [_] is a punctuation mark.\n", + "\n", + "The character [`] is a punctuation mark.\n", + "\n", + "The character [a] is a lower-case letter.\n", + "\n", + "The character [b] is a lower-case letter.\n", + "\n", + "The character [c] is a lower-case letter.\n", + "\n", + "The character [d] is a lower-case letter.\n", + "\n", + "The character [e] is a lower-case letter.\n", + "\n", + "The character [f] is a lower-case letter.\n", + "\n", + "The character [g] is a lower-case letter.\n", + "\n", + "The character [h] is a lower-case letter.\n", + "\n", + "The character [i] is a lower-case letter.\n", + "\n", + "The character [j] is a lower-case letter.\n", + "\n", + "The character [k] is a lower-case letter.\n", + "\n", + "The character [l] is a lower-case letter.\n", + "\n", + "The character [m] is a lower-case letter.\n", + "\n", + "The character [n] is a lower-case letter.\n", + "\n", + "The character [o] is a lower-case letter.\n", + "\n", + "The character [p] is a lower-case letter.\n", + "\n", + "The character [q] is a lower-case letter.\n", + "\n", + "The character [r] is a lower-case letter.\n", + "\n", + "The character [s] is a lower-case letter.\n", + "\n", + "The character [t] is a lower-case letter.\n", + "\n", + "The character [u] is a lower-case letter.\n", + "\n", + "The character [v] is a lower-case letter.\n", + "\n", + "The character [w] is a lower-case letter.\n", + "\n", + "The character [x] is a lower-case letter.\n", + "\n", + "The character [y] is a lower-case letter.\n", + "\n", + "The character [z] is a lower-case letter.\n", + "\n", + "The character [{] is a punctuation mark.\n", + "\n", + "The character [|] is a punctuation mark.\n", + "\n", + "The character [}] is a punctuation mark.\n", + "\n", + "The character [~] is a punctuation mark.\n", + "\n", + "The character [\u007f] is a Error.\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.13, Page no:99" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import math\n", + "def isPrime(n):\n", + " # returns True if n is prime, False otherwise:\n", + " sqrtn = math.sqrt(n)\n", + " if (n < 2):\n", + " return False\n", + " # 0 and 1 are not primes\n", + " if (n < 4):\n", + " return True\n", + " # 2 and 3 are the first primes\n", + " if (n%2 == 0):\n", + " return False\n", + " # 2 is the only even prime\n", + " for d in range(3,int(sqrtn+1),2):\n", + " if (n%d == 0):\n", + " return False\n", + " # n has a nontrivial divisor\n", + " return True;\n", + "\n", + "for n in range(0,80):\n", + " if (isPrime(n)):\n", + " print n,\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.14, Page no:100" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def isLeapYear(y):\n", + " # returns true iff y is a leap year:\n", + " return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)\n", + "\n", + "# tests the isLeapYear() function:\n", + "n = 2\n", + "while n > 1:\n", + " n = int(raw_input())\n", + " if (isLeapYear(n)):\n", + " print \"%d is a leap year.\" % n\n", + " else:\n", + " print \"%d is not a leap year.\" %n\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2004\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2004 is a leap year.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2006\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2006 is not a leap year.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2013\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2013 is not a leap year.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 is a leap year.\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.15, Page no: 101" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def age():\n", + " # prompts the user to input his/her age, and returns that value:\n", + " while (True):\n", + " print \"How old are you: \"\n", + " n = int(raw_input())\n", + " if (n < 0):\n", + " print \"\\a\\tYour age could not be negative.\"\n", + " elif (n > 120):\n", + " print \"\\a\\tYou could not be over 120.\"\n", + " else:\n", + " return n\n", + " print \"\\n\\tTry again.\\n\"\n", + "\n", + "a = age();\n", + "print \"\\nYou are %d years old.\" %a\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How old are you: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\u0007\tYour age could not be negative.\n", + "\n", + "\tTry again.\n", + "\n", + "How old are you: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "125\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\u0007\tYou could not be over 120.\n", + "\n", + "\tTry again.\n", + "\n", + "How old are you: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "24\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "You are 24 years old.\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.16, Page no: 102" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def swap(x,y):\n", + " # exchanges the values of x and y:\n", + " x[0],y[0] = y[0],x[0]\n", + "\n", + "a = [22.2]\n", + "b = [44.4]\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "swap(a,b)\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a = 22.20 , b = 44.40 \n", + "a = 44.40 , b = 22.20 \n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.17, Page no: 104" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Note : Python doesn't support pass value by reference. but can be done by passing list.\n", + "'''\n", + "\n", + "def f(x,y):\n", + " x[0]= 88\n", + " y[0] = 99\n", + "\n", + "# tests the f() function:\n", + "a = [22]\n", + "b = [44]\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "f(a,b)\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "f(2*a,b)\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a = 22.00 , b = 44.00 \n", + "a = 88.00 , b = 99.00 \n", + "a = 88.00 , b = 99.00 \n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.18, Page no: 105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def computeCircle(r):\n", + " # returns the area and circumference of a circle with radius r:\n", + " PI = 3.141592653589793\n", + " area = PI*r*r\n", + " circumference = 2*PI*r\n", + " return area,circumference\n", + "\n", + "# tests the computeCircle() function:\n", + "print \"Enter radius: \"\n", + "r = int(raw_input())\n", + "a,c = computeCircle(r)\n", + "print \"area = %.2f , circumference = %.2f\" %(a,c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter radius: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "area = 78.54 , circumference = 31.42\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.19, Page no: 106" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Note : Python passes variable by value and not by reference. So output would be differ.\n", + "'''\n", + "\n", + "\n", + "def f(x,y,z):\n", + " x[0] += z[0]\n", + " y[0] += z[0]\n", + " print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", + "\n", + "x = [22]\n", + "y = [33]\n", + "z = [44]\n", + "\n", + "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", + "f(x,y,z)\n", + "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", + "x[0] = 2*x[0] - 3\n", + "f(x,y,z)\n", + "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x = 22 , y = 33 , z = 44\n", + "x = 66 , y = 77 , z = 44\n", + "x = 66 , y = 77 , z = 44\n", + "x = 173 , y = 121 , z = 44\n", + "x = 173 , y = 121 , z = 44\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.20, Page no: 107" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def cube(x):\n", + " # returns cube of x:\n", + " return x*x*x\n", + "\n", + "# tests the cube() function:\n", + "print cube(4)\n", + "x = int(raw_input())\n", + "y = cube(2*x-3)\n", + "print y\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "64\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "343\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.21, Page no: 108" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Python has it's own scope so output would be differ.\n", + "'''\n", + "x = 11\n", + "\n", + "def f():\n", + " x = 44\n", + " print \"In f(): x = %d\" % x \n", + "\n", + "def g():\n", + " print \"In g(): x = %d\" % x \n", + "\n", + "x = 22\n", + "x = 33\n", + "print \"In block inside main(): x = %d\" % x\n", + "\n", + "\n", + "print \"In main(): x = %d\" % x \n", + "print \"In main(): ::x = %d\" % x \n", + "f()\n", + "g()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In block inside main(): x = 33\n", + "In main(): x = 33\n", + "In main(): ::x = 33\n", + "In f(): x = 44\n", + "In g(): x = 33\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.22, Page no: 109" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def max_(x, y,z=0):\n", + " if x > y and x > y:\n", + " return x\n", + " elif y > x and y > z:\n", + " return y\n", + " else:\n", + " return z\n", + " \n", + " \n", + "print max(99,77), \" \" , max(55,66,33)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "99 66\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.23, Page no: 110" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "# prints the quotient of two input integers:\n", + "print \"Enter two integers: \"\n", + "n = int(raw_input())\n", + "d = int(raw_input())\n", + "if (d == 0):\n", + " import sys\n", + " sys.exit(0)\n", + "print n , \"/\" , d , \" = \" , n/d \n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "8 / 2 = 4\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.24, Page no: 110" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def reciprocal(x):\n", + " #returns the reciprocal of x:\n", + " if (x == 0):\n", + " import sys\n", + " sys.exit(1); # terminate the program\n", + " return 1.0/x\n", + "\n", + "x = float(raw_input())\n", + "print reciprocal(x)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "25\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.04\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.25, Page no: 111" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "This function evaluates the third degree polynomial a0 + a1x + a2x2 + a3x3. \n", + "'''\n", + "def p(x,a0,a1=0,a2=0,a3=0):\n", + " # returns a0 + a1*x + a2*x^2 + a3*x^3:\n", + " return (a0 + (a1 + (a2 + a3*x)*x)*x)\n", + "\n", + "\n", + "# tests the p() function:\n", + "x = 2.0003\n", + "print \"p(x,7) = %f\" % p(x,7)\n", + "print \"p(x,7,6) = %f\" % p(x,7,6)\n", + "print \"p(x,7,6,5) = %f\" % p(x,7,6,5)\n", + "print \"p(x,7,6,5,4) = %f\" % p(x,7,6,5,4)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "p(x,7) = 7.000000\n", + "p(x,7,6) = 19.001800\n", + "p(x,7,6,5) = 39.007800\n", + "p(x,7,6,5,4) = 71.022203\n" + ] + } + ], + "prompt_number": 24 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch6.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch6.ipynb new file mode 100644 index 00000000..bca2c6e0 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch6.ipynb @@ -0,0 +1,1264 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Arrays" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.1, page no. 126" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = [0, 0, 0]\n", + "a[2] = 55.55\n", + "a[0] = 11.11\n", + "a[1] = 33.33\n", + "print \"a[0] = \" , a[0] \n", + "print \"a[1] = \" , a[1] \n", + "print \"a[2] = \" , a[2] \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a[0] = 11.11\n", + "a[1] = 33.33\n", + "a[2] = 55.55\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.2, page no. 127" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "SIZE=5 # defines the size N for 5 elements\n", + "a = []\n", + "# declares the array's elements as type double\n", + "print \"Enter \" , SIZE , \" numbers:\\t\"\n", + "for i in range(SIZE):\n", + " a.append(float(raw_input()))\n", + " \n", + "print \"In reverse order: \"\n", + "for i in range(SIZE-1,-1,-1):\n", + " print \"\\t\" , a[i]\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 5 numbers:\t\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11.11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33.33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55.55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77.77\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "99.99\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In reverse order: \n", + "\t99.99\n", + "\t77.77\n", + "\t55.55\n", + "\t33.33\n", + "\t11.11\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.3, page no. 128" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = [ 22.2, 44.4, 66.6 ]\n", + "\n", + "size = len(a)\n", + "for i in range(size):\n", + " print \"\\ta[\" , i , \"] = \" , a[i]\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\ta[ 0 ] = 22.2\n", + "\ta[ 1 ] = 44.4\n", + "\ta[ 2 ] = 66.6\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.4, page no. 128" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]\n", + "size = len(a)\n", + "for i in range(size):\n", + " print \"\\ta[\" , i , \"] = \" , a[i] \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\ta[ 0 ] = 22.2\n", + "\ta[ 1 ] = 44.4\n", + "\ta[ 2 ] = 66.6\n", + "\ta[ 3 ] = 0\n", + "\ta[ 4 ] = 0\n", + "\ta[ 5 ] = 0\n", + "\ta[ 6 ] = 0\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.5, page no. 129" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "SIZE = 4\n", + "a = numpy.zeros(4)\n", + "# declares the array's elements as type float\n", + "for i in range(SIZE):\n", + " print \"\\ta[\" , i , \"] = \" , a[i]\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\ta[ 0 ] = 0.0\n", + "\ta[ 1 ] = 0.0\n", + "\ta[ 2 ] = 0.0\n", + "\ta[ 3 ] = 0.0\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.6, page no. 129" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "SIZE=4\n", + "a = [ 33.3, 44.4, 55.5, 66.6 ]\n", + "for i in range(7): # ERROR: index is out of bounds!\n", + " print \"\\ta[\" , i , \"] = \" , a[i] \n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-1-b39022f1d3ab>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[1;36m33.3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m44.4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m55.5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m66.6\u001b[0m \u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# ERROR: index is out of bounds!\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\ta[\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;34m\"] = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIndexError\u001b[0m: list index out of range" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\ta[ 0 ] = 33.3\n", + "\ta[ 1 ] = 44.4\n", + "\ta[ 2 ] = 55.5\n", + "\ta[ 3 ] = 66.6\n", + "\ta[ 4 ] = " + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.7, page no. 130" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = [ 22.2, 44.4, 66.6 ]\n", + "x=11.1\n", + "print \"x = \" , x \n", + "a.append(88.8) # ERROR: index is out of bounds!\n", + "print \"x = \" , x \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " x = 11.1\n", + "x = 11.1\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.8, page no. 130" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = [ 22.2, 44.4, 66.6 ]\n", + "x=11.1\n", + "print \"x = \" , x \n", + "a[3333] = 88.8 # ERROR: index is out of bounds!\n", + "print \"x = \" , x \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list assignment index out of range", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-8-63b6c8e60294>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m11.1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3333\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m88.8\u001b[0m \u001b[1;31m# ERROR: index is out of bounds!\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIndexError\u001b[0m: list assignment index out of range" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x = 11.1\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.9, page no. 131" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def sum_(a):\n", + " s = 0\n", + " for i in a:\n", + " s += i\n", + " return s\n", + " \n", + "a = [ 11, 33, 55, 77 ]\n", + "print \"sum(a) = \" , sum_(a) \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sum(a) = 176\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.10, page no. 132" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def read(a):\n", + " print \"Enter integers. Terminate with 0:\\n\"\n", + " n = 1\n", + " while True:\n", + " n = int(raw_input(\"a[\" + str(len(a)) + \"]: \"))\n", + " if n == 0:\n", + " break\n", + " a.append(n)\n", + " \n", + "\n", + "def print_(a):\n", + " for i in a:\n", + " print i ,\n", + "\n", + "\n", + "a = []\n", + "read(a)\n", + "print \"The array has \" , len(a) , \" elements: \"\n", + "print_(a)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter integers. Terminate with 0:\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a[0]: 11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a[1]: 22\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a[2]: 33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a[3]: 44\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a[4]: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The array has 4 elements: \n", + "11 22 33 44\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.11, page no. 133" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "a = [ 22, 44, 66, 88 ]\n", + "print \"a = \" , id(a) # the address of a[0]\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a = 169156908\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.12, page no. 133" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def index(x,a,n):\n", + " for i in range(len(a)):\n", + " if (a[i] == x):\n", + " return i\n", + " return n # x not found\n", + "\n", + "a = [ 22, 44, 66, 88, 44, 66, 55 ]\n", + "print \"index(44,a,7) = \" , index(44,a,7)\n", + "print \"index(50,a,7) = \" , index(50,a,7) \n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "index(44,a,7) = 1\n", + "index(50,a,7) = 7\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.13, page no. 134" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def sort(a,n):\n", + " # bubble sort:\n", + " n = len(a)\n", + " for i in range(n):\n", + " # bubble up max{a[0..n-i]}:\n", + " for j in range(n-i-1):\n", + " if (a[j] > a[j+1]):\n", + " a[j],a[j+1] = a[j+1],a[j]\n", + "\n", + "def print_(a):\n", + " for i in range(len(a)):\n", + " print a[i],\n", + " print ''\n", + " \n", + "a = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n", + "\n", + "print_(a)\n", + "sort(a,8)\n", + "print_(a)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n", + "22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.14, page no. 135" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def index(x,a,n):\n", + " # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n", + " # binary search:\n", + " lo=0\n", + " hi=n-1\n", + " while (lo <= hi):\n", + " i = (lo + hi)/2 # the average of lo and hi\n", + " if (a[i] == x):\n", + " return i\n", + " if (a[i] < x):\n", + " lo = i+1 # continue search in a[i+1..hi]\n", + " else:\n", + " hi = i-1 # continue search in a[lo..i-1]\n", + " return n # x was not found in a[0..n-1]\n", + "\n", + "a = [ 22, 33, 44, 55, 66, 77, 88 ]\n", + "print \"index(44,a,7) = \" , index(44,a,7)\n", + "print \"index(60,a,7) = \" , index(60,a,7) \n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "index(44,a,7) = 2\n", + "index(60,a,7) = 7\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.15, page no. 136" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def isNondecreasing(a,n):\n", + " # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n", + " for i in range(1,n):\n", + " if (a[i]<a[i-1]):\n", + " return False\n", + " return True\n", + "\n", + "a = [ 22, 44, 66, 88, 44, 66, 55 ]\n", + "print \"isNondecreasing(a,4) = \" , isNondecreasing(a,4)\n", + "print \"isNondecreasing(a,7) = \" , isNondecreasing(a,7)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "isNondecreasing(a,4) = True\n", + "isNondecreasing(a,7) = False\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.16, page no. 137" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def isNondecreasing(a,n):\n", + " # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n", + " for i in range(1,n):\n", + " if (a[i]<a[i-1]):\n", + " return False\n", + " return True\n", + "\n", + "\n", + "def index(x,a,n):\n", + " # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n", + " # binary search:\n", + " assert(isNondecreasing(a,n)) \n", + " \n", + " lo=0\n", + " hi=n-1\n", + " while (lo <= hi):\n", + " i = (lo + hi)/2\n", + " if (a[i] == x):\n", + " return i\n", + " if (a[i] < x):\n", + " lo = i+1 # continue search in a[i+1..hi]\n", + " else:\n", + " hi = i-1 # continue search in a[lo..i-1]\n", + " return n # x was not found in a[0..n-1]\n", + "\n", + "a = [ 22, 33, 44, 55, 66, 77, 88, 60 ]\n", + "print \"index(44,a,7) = \" , index(44,a,7) \n", + "print \"index(60,a,7) = \" , index(60,a,7)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " index(44,a,7) = 2\n", + "index(60,a,8) = 7\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.17, page no. 137" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Day = [ 0, 1, 2, 3, 4, 5, 6 ]\n", + "high = [ 88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7]\n", + "\n", + "for i in Day:\n", + " print \"The high temperature for day \" , i , \" was \" , high[i] \n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The high temperature for day 0 was 88.3\n", + "The high temperature for day 1 was 95.0\n", + "The high temperature for day 2 was 91.2\n", + "The high temperature for day 3 was 89.9\n", + "The high temperature for day 4 was 91.4\n", + "The high temperature for day 5 was 92.5\n", + "The high temperature for day 6 was 86.7\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.18, page no. 139" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def sort(a,n):\n", + " a.sort()\n", + "\n", + "def print_(a,n):\n", + " for i in a:\n", + " print i,\n", + " print ''\n", + "a = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n", + "print_(a,8);\n", + "sort(a,8)\n", + "print_(a,8)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n", + "22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.19, page no. 139" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def read(a):\n", + " print \"Enter 15 integers, 5 per row:\\n\"\n", + " for i in range(3):\n", + " ar = []\n", + " print \"Row \" , i , \": \",\n", + " for j in range(5):\n", + " ar.append(int(raw_input()))\n", + " a.append(ar)\n", + "\n", + "def print_(a):\n", + " for i in range(3):\n", + " for j in range(5):\n", + " print a[i][j],\n", + " print ''\n", + "a = []\n", + "read(a)\n", + "print_(a)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 15 integers, 5 per row:\n", + "\n", + "Row 0 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "44\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "44\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Row 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "70\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Row 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "85\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "25\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 44 77 33 11 44 \n", + "60 50 30 90 70 \n", + "85 25 45 45 55 \n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.20, page no. 140" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def read(score):\n", + " for s in range(3):\n", + " print \"Student \" , s , \": \",\n", + " st = []\n", + " for q in range(5):\n", + " st.append(int(raw_input()))\n", + " score.append(st)\n", + "\n", + "def printQuizAverages(score):\n", + " for s in range(3):\n", + " sm = 0\n", + " for q in range(5):\n", + " sm += score[s][q]\n", + " print \"\\tStudent \" , s , \": \" , sm/5.0\n", + "\n", + "def printClassAverages(score):\n", + " for q in range(5):\n", + " sm = 0\n", + " for s in range(3):\n", + " sm += score[s][q]\n", + " print \"\\tQuiz \" , q , \": \" , sm/3.0\n", + "\n", + "\n", + "\n", + "NUM_STUDENTS = 3\n", + "NUM_QUIZZES = 5\n", + "\n", + "\n", + "score = []\n", + "print \"Enter \" , NUM_QUIZZES , \" scores for each student: \"\n", + "read(score)\n", + "print \"The quiz averages are:\"\n", + "printQuizAverages(score)\n", + "print \"The class averages are: \"\n", + "printClassAverages(score)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 5 scores for each student: \n", + "Student 0 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Student 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Student 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The quiz averages are:\n", + "\tStudent 0 : 8.2\n", + "\tStudent 1 : 8.8\n", + "\tStudent 2 : 7.0\n", + "The class averages are: \n", + "\tQuiz 0 : 7.33333333333\n", + "\tQuiz 1 : 7.33333333333\n", + "\tQuiz 2 : 8.33333333333\n", + "\tQuiz 3 : 8.33333333333\n", + "\tQuiz 4 : 8.66666666667\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.21, page no. 141" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def numZeros(a,n1,n2,n3):\n", + " count = 0\n", + " for i in range(n1):\n", + " for j in range(n2):\n", + " for k in range(n3):\n", + " if (a[i][j][k] == 0):\n", + " count += 1\n", + " return count\n", + "\n", + "\n", + "a = [ [ [5,0,2], [0,0,9], [4,1,0], [7,7,7] ],[ [3,0,0], [8,5,0], [0,0,0], [2,0,9] ]]\n", + "print \"This array has \" , numZeros(a,2,4,3) , \" zeros\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This array has 11 zeros\n" + ] + } + ], + "prompt_number": 25 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch7.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch7.ipynb new file mode 100755 index 00000000..63248e07 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch7.ipynb @@ -0,0 +1,750 @@ +{
+ "metadata": {
+ "name": "ch7"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Capter 7: Pointers and References"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.1, Page no: 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n=44\n",
+ "print \"n = \" , n \n",
+ "# prints the value of n\n",
+ "print \"&n = \" , hex(id(n)) # prints the address of n\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = 44\n",
+ "&n = 0x8fc0eec\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.2, Page no: 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Note : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.\n",
+ "'''\n",
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "rn=n # r is a synonym for n\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "n[0] -= 1\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "rn[0] *= 2\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , rn = [44]\n",
+ "n = [43] , rn = [43]\n",
+ "n = [86] , rn = [86]\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.3, Page no: 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "n = [44]\n",
+ "rn=n # r is a synonym for n\n",
+ "print \"&n = \" , hex(id(n)) , \", rn = \" , hex(id(rn ))\n",
+ "rn2 = n\n",
+ "rn3 = rn\n",
+ "print \"&rn2 = \" , hex(id(rn2)) , \", rn = \" , hex(id(rn ))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "&n = 0x9c6228c , rn = 0x9c6228c\n",
+ "&rn2 = 0x9c6228c , rn = 0x9c6228c\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.4, Page no: 159"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"pn = \" , hex(id(pn)) , \", &pn = \" , hex(id(hex(id(pn))))\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , &n = 0x9c624ec\n",
+ "pn = 0x9c624ec , &pn = 0x9c6aa60\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.5, Page no: 159"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , &n = 0x9c58d6c\n",
+ "\tpn = 0x9c58d6c ,\n",
+ " &pn = 0x9c6ab20\n",
+ "*pn = [44]\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.6, Page no: 160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "ppn = pn\n",
+ "\n",
+ "print \" ppn = \" , hex(id(hex(id(ppn)))) \n",
+ "print \" &ppn = \" , hex(id(hex(id(hex(id(ppn))))))\n",
+ "print \" *ppn = \" , hex(id(ppn)) \n",
+ "print \"**ppn = \" , ppn \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] , &n = 0x9bf05ac\n",
+ "\tpn = 0x9bf05ac ,\n",
+ " &pn = 0x9c58160\n",
+ "*pn = [44]\n",
+ " ppn = 0x9c58680\n",
+ " &ppn = 0x9c58160\n",
+ " *ppn = 0x9bf05ac\n",
+ "**ppn = [44]\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.7, Page no: 160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \"\\n &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "nn = pn\n",
+ "print \" ppn = \" , hex(id(nn))\n",
+ "print \" &ppn = \" , hex(id(hex(id(nn))))\n",
+ "rpn = pn\n",
+ "print \" ppn = \" , hex(id(rpn))\n",
+ "print \" &ppn = \" , hex(id(hex(id(rpn))))\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n = [44] \n",
+ " &n = 0x9bf60ec\n",
+ "\tpn = 0x9bf60ec ,\n",
+ " &pn = 0x9bf0e40\n",
+ "*pn = [44]\n",
+ " ppn = 0x9bf60ec\n",
+ " &ppn = 0x9bf0e40\n",
+ " ppn = 0x9bf60ec\n",
+ " &ppn = 0x9bf0f20\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.8, Page no: 162"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def max_(m,n):\n",
+ " if m>n:\n",
+ " return m\n",
+ " else:\n",
+ " return n\n",
+ "\n",
+ "m = 44\n",
+ "n = 22\n",
+ "print m , \", \" , n , \", \" , max_(m,n)\n",
+ "m = max_(m,n) \n",
+ "m = 55\n",
+ "# changes the value of m from 44 to 55\n",
+ "print m , \", \" , n , \", \" , max_(m,n) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "44 , 22 , 44\n",
+ "55 , 22 , 55\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.9, Page no: 163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "v = []\n",
+ "for k in range(1,5):\n",
+ " v.append(1.0/k)\n",
+ "\n",
+ "for i in range(4):\n",
+ " print \"v[\" , i , \"] = \" , v[i]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "v[ 0 ] = 1.0\n",
+ "v[ 1 ] = 0.5\n",
+ "v[ 2 ] = 0.333333333333\n",
+ "v[ 3 ] = 0.25\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.10, Page no: 163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import sys\n",
+ "a = [22, 33, 44]\n",
+ "\n",
+ "print \"a = \" , hex(id(a))\n",
+ "print \"sizeof(int) = \" , sys.getsizeof(1) \n",
+ "s = 0\n",
+ "for i in a:\n",
+ " s += i\n",
+ " print \"\\t i = \" , hex(id(i)),\n",
+ " print \"\\t *i = \" , i,\n",
+ " print \"\\t sum = \" , s\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a = 0x9bf688c\n",
+ "sizeof(int) = 12\n",
+ "\t i = 0x8fc0ff4 \t *i = 22 \t sum = 22\n",
+ "\t i = 0x8fc0f70 \t *i = 33 \t sum = 55\n",
+ "\t i = 0x8fc0eec \t *i = 44 \t sum = 99\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.11, Page no: 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "a = [22, 33, 44, 55, 66]\n",
+ "print \"a = \" , hex(id(a)) , \", *a = \" , a[0] \n",
+ "for p in a:\n",
+ " print \"p = \" , hex(id(p)) , \", *p = \" , p \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a = 0x9c6526c , *a = 22\n",
+ "p = 0x8fc0ff4 , *p = 22\n",
+ "p = 0x8fc0f70 , *p = 33\n",
+ "p = 0x8fc0eec , *p = 44\n",
+ "p = 0x8fc0e68 , *p = 55\n",
+ "p = 0x8fc0de4 , *p = 66\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.12, Page no: 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def loc(a1,a2,n1,n2):\n",
+ " p = []\n",
+ " for element in a2:\n",
+ " if element in a1:\n",
+ " p.append(element)\n",
+ " return p\n",
+ "\n",
+ "a1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]\n",
+ "a2 = [11, 11, 11, 22, 33]\n",
+ "print \"Array a1 begins at location\\t\" , hex(id(a1 ))\n",
+ "print \"Array a2 begins at location\\t\" , hex(id(a2)) \n",
+ "p = loc(a1, a2, 9, 5)\n",
+ "if (p):\n",
+ " print \"Array a2 found at location\\t\" , hex(id(p))\n",
+ " for i in range(len(p)):\n",
+ " print \"\\t\" , hex(id(p[i])) , \": \" , p[i], \"\\t\" , hex(id(a2[i])) , \": \" , a2[i] \n",
+ "else:\n",
+ " print \"Not found.\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Array a1 begins at location\t0x9bea56c\n",
+ "Array a2 begins at location\t0x9bea62c\n",
+ "Array a2 found at location\t0x9bea6cc\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc0ff4 : 22 \t0x8fc0ff4 : 22\n",
+ "\t0x8fc0f70 : 33 \t0x8fc0f70 : 33\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.13, Page no: 166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def get(a):\n",
+ " print \"Enter number of items: \"\n",
+ " n = int(raw_input())\n",
+ " print \"Enter \" , n , \" items, one per line:\"\n",
+ " for i in range(n):\n",
+ " print \"\\t\" , i+1 , \": \",\n",
+ " a.append(float(raw_input()))\n",
+ "\n",
+ "def print_(a):\n",
+ " for i in range(len(a)):\n",
+ " print a[i] ,\n",
+ " print ''\n",
+ "\n",
+ "a = []\n",
+ "get(a)\n",
+ "print_(a)\n",
+ "a = []\n",
+ "get(a)\n",
+ "print_(a)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of items: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 4 items, one per line:\n",
+ "\t1 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "44.4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t2 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "77.7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t3 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "22.2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t4 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "88.8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 44.4 77.7 22.2 88.8 \n",
+ "Enter number of items: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 2 items, one per line:\n",
+ "\t1 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3.33\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \t2 : "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "9.99\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 3.33 9.99 \n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.14, Page no: 167"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def sort(p, n):\n",
+ " for i in range(1,n):\n",
+ " for j in range(n-i):\n",
+ " if (p[j] > p[j+1]):\n",
+ " p[j],p[j+1] = p[j+1],p[j]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.15, Page no: 168"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def sum_(k,n):\n",
+ " # returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):\n",
+ " s = 0\n",
+ " for i in range(1,n+1):\n",
+ " s += k(i)\n",
+ " return s\n",
+ "\n",
+ "def square(k):\n",
+ " return k*k\n",
+ "\n",
+ "def cube(k):\n",
+ " return k*k*k\n",
+ "\n",
+ "\n",
+ "print sum_(square,4) # 1 + 4 + 9 + 16\n",
+ "print sum_(cube,4) # 1 + 8 + 27 + 64\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n",
+ "100\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch8.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch8.ipynb new file mode 100755 index 00000000..2b83cace --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch8.ipynb @@ -0,0 +1,1064 @@ +{
+ "metadata": {
+ "name": "ch8"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 8: C- Strings"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.1, page no. 184"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "n= [44] # n holds the int 44\n",
+ "print \"int n=44; // n holds the int 44:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "pn = n \n",
+ "print \"int* pn=&n; // pn holds the address of n:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "\n",
+ "pn[0] = 77 # changes the value of n to 77\n",
+ "print \"*pn = 77; // changes the value of n to 77:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "\n",
+ "q = n \n",
+ "print \"int* q=&n; // q also holds the address of n:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "print \"\\t\\t q = \" , hex(id(q))\n",
+ "print \"\\t\\t &q = \" , hex(id(hex(id(hex(id(pn))))))\n",
+ "print \"\\t\\t *q = \" , q \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "int n=44; // n holds the int 44:\n",
+ "\n",
+ "\t\t n = [44]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "int* pn=&n; // pn holds the address of n:\n",
+ "\n",
+ "\t\t n = [44]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "\t\t pn = 0x9bfb92c\n",
+ "\t\t &pn = 0x9bf5aa0\n",
+ "\t\t *pn = [44]\n",
+ "*pn = 77; // changes the value of n to 77:\n",
+ "\n",
+ "\t\t n = [77]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "\t\t pn = 0x9bfb92c\n",
+ "\t\t &pn = 0x9c6a760\n",
+ "\t\t *pn = [77]\n",
+ "int* q=&n; // q also holds the address of n:\n",
+ "\n",
+ "\t\t n = [77]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "\t\t pn = 0x9bfb92c\n",
+ "\t\t &pn = 0x9bf5c80\n",
+ "\t\t *pn = [77]\n",
+ "\t\t q = 0x9bfb92c\n",
+ "\t\t &q = 0x9c6a760\n",
+ "\t\t *q = [77]\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.2, page no. 185"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"ABCD\"\n",
+ "for i in range(4):\n",
+ " print \"s[\" , i , \"] = '\" , s[i] , \"'\\n\";\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "s[ 0 ] = ' A '\n",
+ "\n",
+ "s[ 1 ] = ' B '\n",
+ "\n",
+ "s[ 2 ] = ' C '\n",
+ "\n",
+ "s[ 3 ] = ' D '\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.3, page no. 186"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " word = raw_input()\n",
+ " if len(word) < 2:\n",
+ " break\n",
+ " l = word.split(' ')\n",
+ " for i in l:\n",
+ " print '\\t\"' , i , '\"'\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Today's date is March 12, 2000.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t\" Today's \"\n",
+ "\t\" date \"\n",
+ "\t\" is \"\n",
+ "\t\" March \"\n",
+ "\t\" 12, \"\n",
+ "\t\" 2000. \"\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Tomorrow is Monday.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t\" Tomorrow \"\n",
+ "\t\" is \"\n",
+ "\t\" Monday. \"\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.4, page no. 187"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " line = raw_input()\n",
+ " if len(line) < 2:\n",
+ " break\n",
+ " print \"\\t[\" , line , \"]\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Once upon a midnight dreary, while I pondered, weak and weary, ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Over a many quaint and curious volume of forgotten lore,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Over a many quaint and curious volume of forgotten lore, ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.5, page no. 187"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " word = raw_input()\n",
+ " if len(word) < 2:\n",
+ " break\n",
+ " l = word.split(',')\n",
+ " for i in range(len(l)-1):\n",
+ " print '\\t[' , l[i] , ']'\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Once upon a midnight dreary ]\n",
+ "\t[ while I pondered ]\n",
+ "\t[ weak and weary ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Over a many quaint and curious volume of forgotten lore,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Over a many quaint and curious volume of forgotten lore ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.6, page no. 188"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "count = 0\n",
+ "while True:\n",
+ " a = raw_input()\n",
+ " if len(a) < 1:\n",
+ " break\n",
+ " for ch in a:\n",
+ " if (ch == 'e'): count+=1\n",
+ " \n",
+ "print count , \" e's were counted.\\n\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Over many a quaint and curious volume of forgotten lore,\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "11 e's were counted.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.7, page no. 188"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " a = raw_input()\n",
+ " if len(a) < 1:\n",
+ " break\n",
+ " print a.title()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Fourscore and seven years ago our fathers\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Fourscore And Seven Years Ago Our Fathers\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "brought forth upon this continent a new nation,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Brought Forth Upon This Continent A New Nation,\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.8, page no. 189"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "a = raw_input()\n",
+ "l = a.split(' ')\n",
+ "nos = []\n",
+ "for i in l:\n",
+ " try:\n",
+ " i = int(i)\n",
+ " nos.append(i)\n",
+ " except:\n",
+ " continue\n",
+ "m = nos[0]\n",
+ "n = nos[1] \n",
+ "print m , \" + \" , n , \" = \" , m+n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "what is 305 plus 9416 ?\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "305 + 9416 = 9721\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.10, page no. 191"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "count=0\n",
+ "\n",
+ "print \"Enter at most 4 names with at most 19 characters:\\n\";\n",
+ "while (True):\n",
+ " n = raw_input()\n",
+ " if len(n) < 1:\n",
+ " break\n",
+ " name.append(n)\n",
+ " count += 1\n",
+ " \n",
+ "print \"The names are:\\n\"\n",
+ "for i in range(count):\n",
+ " print \"\\t\" , i , \". [\" , name[i] , \"]\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter at most 4 names with at most 19 characters:\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "George Washington\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "John Adams\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Thomas Jefferson\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.11, page no. 192"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "name = []\n",
+ "count=0\n",
+ "\n",
+ "print \"Enter at most 4 names with at most 19 characters:\\n\";\n",
+ "while (True):\n",
+ " n = raw_input()\n",
+ " if len(n) < 1:\n",
+ " break\n",
+ " name.append(n)\n",
+ " count += 1\n",
+ " \n",
+ "print \"The names are:\\n\"\n",
+ "for i in range(count):\n",
+ " print \"\\t\" , i , \". [\" , name[i] , \"]\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter at most 4 names with at most 19 characters:\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "George Washington\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "John Adams\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Thomas Jefferson\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.12, page no. 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "name = [ \"George Washington\", \"John Adams\", \"Thomas Jefferson\"]\n",
+ "print \"The names are:\\n\"\n",
+ "for i in range(3):\n",
+ " print \"\\t\" , i , \". [\" , name[i] , \"]\"\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.13, page no. 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"ABCDEFG\"\n",
+ "print \"len(\" , s , \") = \" , len(s) \n",
+ "print \"len(\\\"\\\") = \" , len(\"\")\n",
+ "print \"Enter string: \"\n",
+ "b = raw_input()\n",
+ "print \"len(\" , b , \") = \" , len(b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "len( ABCDEFG ) = 7\n",
+ "len(\"\") = 0\n",
+ "Enter string: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "hello how are you !!!\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "len( hello how are you !!! ) = 21\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.14, page no. 194"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"The Mississippi is a long river.\"\n",
+ "print 's = \"' , s , '\"'\n",
+ "p = s.find(' ')\n",
+ "print \"find(s, ' ') points to s[\" , p , \"].\"\n",
+ "p = s.find('s')\n",
+ "print \"find(s, 's') points to s[\" , p , \"].\"\n",
+ "p = s.rfind('s')\n",
+ "print \"reverse find(s, 's') points to s[\" , p , \"].\"\n",
+ "p = s.find(\"is\")\n",
+ "print \"strstr(s, \\\"is\\\") points to s[\" , p , \"].\"\n",
+ "p = s.find(\"isi\")\n",
+ "if p== -1:\n",
+ " print 's.find(\"isi\") returns NULL'\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "s = \" The Mississippi is a long river. \"\n",
+ "find(s, ' ') points to s[ 3 ].\n",
+ "find(s, 's') points to s[ 6 ].\n",
+ "reverse find(s, 's') points to s[ 17 ].\n",
+ "strstr(s, \"is\") points to s[ 5 ].\n",
+ "s.find(\"isi\") returns NULL\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.15, page no. 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before strcpy(s1,s2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 = s2\n",
+ "print \"After strcpy(s1,s2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before strcpy(s1,s2):\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After strcpy(s1,s2):\n",
+ "\n",
+ "\ts1 = [ XYZ ], length = 3\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.16, page no. 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before strcpy(s1,s2,2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 = s2[:2] + s1[2:]\n",
+ "print \"After strcpy(s1,s2,2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before strcpy(s1,s2,2):\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After strcpy(s1,s2,2):\n",
+ "\n",
+ "\ts1 = [ XYCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.17, page no. 196"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before string concatination :\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 += s2\n",
+ "print \"After string concatination :\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before string concatination :\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After string concatination :\n",
+ "\ts1 = [ ABCDEFGXYZ ], length = 10\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.18, page no. 197"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before string concatination :\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 += s2[:2]\n",
+ "print \"After string concatination :\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before string concatination :\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After string concatination :\n",
+ "\ts1 = [ ABCDEFGXY ], length = 9\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.19, page no. 197"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"Today's date is March 12, 2000.\"\n",
+ "\n",
+ "print \"The string is: [\" , s , \"] \\nIts tokens are: \"\n",
+ "p = s.split(\" \")\n",
+ "\n",
+ "for i in p:\n",
+ " print \"\\t[\" , i , \"] \"\n",
+ "\n",
+ "print \"Now the string is: [\" , p[0] , \"] \";\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The string is: [ Today's date is March 12, 2000. ] \n",
+ "Its tokens are: \n",
+ "\t[ Today's ] \n",
+ "\t[ date ] \n",
+ "\t[ is ] \n",
+ "\t[ March ] \n",
+ "\t[ 12, ] \n",
+ "\t[ 2000. ] \n",
+ "Now the string is: [ Today's ] \n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.20, page no. 199"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def strpbrk(s,s1):\n",
+ " found = []\n",
+ " for i in range(len(s1)):\n",
+ " if s1[i] in s:\n",
+ " index = s.find(s1[i])\n",
+ " found.append(index)\n",
+ " if found:\n",
+ " return min(found)\n",
+ " return None\n",
+ " \n",
+ "\n",
+ "s = \"The Mississippi is a long river.\"\n",
+ "print 's = \"' , s , '\"'\n",
+ "p = strpbrk(s, \"nopqr\")\n",
+ "print 'strpbrk(s, \"nopqr\") points to s[' , p , \"].\"\n",
+ "p = strpbrk(s, \"NOPQR\")\n",
+ "if (p == None):\n",
+ " print 'strpbrk(s, \"NOPQR\") returns NULL.\\n'"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "s = \" The Mississippi is a long river. \"\n",
+ "strpbrk(s, \"nopqr\") points to s[ 12 ].\n",
+ "strpbrk(s, \"NOPQR\") returns NULL.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/ch9.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch9.ipynb new file mode 100644 index 00000000..e3bb4a45 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/ch9.ipynb @@ -0,0 +1,422 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9: Standard C++ strings" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.2, Page no: 214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "while True:\n", + " try:\n", + " n = int(raw_input())\n", + " print \"n = \" , n \n", + " except:\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "46\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "n = 46\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "n = 22\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "44\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "n = 44\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "66\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "n = 66\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "n = 88\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33,\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.4, Page no: 215" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "king = [] # defines king to be an array \n", + "n=0\n", + "while True:\n", + " name = raw_input()\n", + " if len(name) < 1:\n", + " break\n", + " king.append(name)\n", + " n += 1\n", + "# now n == the number of names read\n", + "for i in range(n):\n", + " print '\\t' , i+1 , \". \" , king[i] " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Kenneth II (971-995)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constantine III (995-997)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Kenneth III (997-1005)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Malcolm II (1005-1034)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Duncan I (1034-1040)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Macbeth (1040-1057)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Lulach (1057-1058)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Malcolm III (1058-1093)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\t1 . Kenneth II (971-995)\n", + "\t2 . Constantine III (995-997)\n", + "\t3 . Kenneth III (997-1005)\n", + "\t4 . Malcolm II (1005-1034)\n", + "\t5 . Duncan I (1034-1040)\n", + "\t6 . Macbeth (1040-1057)\n", + "\t7 . Lulach (1057-1058)\n", + "\t8 . Malcolm III (1058-1093)\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.6, Page no: 217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Note : you need input.txt in same directory for the program to run without any errors.\n", + "'''\n", + "\n", + "infile = open(\"input.txt\",\"r\")\n", + "outfile = open(\"output.txt\",\"w\")\n", + "\n", + "for i in infile:\n", + " s = i.title()\n", + " outfile.write(s)\n", + "\n", + "infile.close()\n", + "outfile.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7, Page no: 218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''Note: You need the files - north.dat, south.dat, combined.dat in the same directory for the program to run without any errors'''\n", + "\n", + "fin1 = open(\"north.dat\",\"r\")\n", + "fin2 = open(\"south.dat\",\"r\")\n", + "fout = open(\"combined.dat\",\"w\")\n", + "\n", + "file1 = []\n", + "file2 = []\n", + "for i in fin1:\n", + " try:\n", + " s = i.split(\" \")\n", + " for j in s:\n", + " file1.append(int(j))\n", + " except:\n", + " continue\n", + " \n", + "for i in fin2:\n", + " try:\n", + " s = i.split(\" \")\n", + " for j in s:\n", + " file2.append(int(j))\n", + " except:\n", + " continue\n", + "\n", + "\n", + "for i in sorted(file1 + file2):\n", + " fout.write(str(i) + \" \")\n", + "\n", + "fin1.close()\n", + "fin2.close()\n", + "fout.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.8, Page no: 219" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def print_(oss):\n", + " print 'oss.str() = \"' , str(oss) , '\"'\n", + "\n", + "s=\"ABCDEFG\"\n", + "n=33\n", + "x=2.718\n", + "l = ''\n", + "print_(l)\n", + "l += s\n", + "print_(l)\n", + "l += ( \" \" + str(n) )\n", + "print_(l)\n", + "l += ( \" \" + str(x) )\n", + "print_(l)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "oss.str() = \" \"\n", + "oss.str() = \" ABCDEFG \"\n", + "oss.str() = \" ABCDEFG 33 \"\n", + "oss.str() = \" ABCDEFG 33 2.718 \"\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.9, Page no: 220" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def print_(iss,s='',n=0,x=0.0):\n", + " print 's = \"' , s , '\", n = ' , n , \", x = \" , x, ', iss.str() = \"' \\\n", + " , iss , '\"' \n", + "\n", + "s=\"\"\n", + "n=0\n", + "x=0.0\n", + "l = ''\n", + "iss = \"ABCDEFG 44 3.14\"\n", + "print_(iss)\n", + "s = \"ABCDEFG\"\n", + "print_(iss,s)\n", + "n = 44\n", + "print_(iss,s,n)\n", + "x = 3.14\n", + "print_(iss,s,n,x)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s = \" \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n", + "s = \" ABCDEFG \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n", + "s = \" ABCDEFG \", n = 44 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n", + "s = \" ABCDEFG \", n = 44 , x = 3.14 , iss.str() = \" ABCDEFG 44 3.14 \"\n" + ] + } + ], + "prompt_number": 6 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines:_Programming_with_C++/screenshots/ratio.png b/Schaum's_Outlines:_Programming_with_C++/screenshots/ratio.png Binary files differnew file mode 100755 index 00000000..68bb8cd0 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/screenshots/ratio.png diff --git a/Schaum's_Outlines:_Programming_with_C++/screenshots/swap.png b/Schaum's_Outlines:_Programming_with_C++/screenshots/swap.png Binary files differnew file mode 100755 index 00000000..7d707e37 --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/screenshots/swap.png diff --git a/Schaum's_Outlines:_Programming_with_C++/screenshots/vector.png b/Schaum's_Outlines:_Programming_with_C++/screenshots/vector.png Binary files differnew file mode 100755 index 00000000..b51554ce --- /dev/null +++ b/Schaum's_Outlines:_Programming_with_C++/screenshots/vector.png |