diff options
Diffstat (limited to 'Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb')
-rw-r--r-- | Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb | 257 |
1 files changed, 240 insertions, 17 deletions
diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb index 827142fe..cfd875f2 100644 --- a/Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb +++ b/Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "ch13" + "name": "", + "signature": "sha256:cc39907a700b2b635fe495cd65c97f54d7249e166a21cea325643318306862ee" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,7 +11,21 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.1 The swap Function Template\n'''\ndef swap(x,y):\n x[0],y[0] = y[0],x[0]\n\nm = [22]\nn = [66]\nswap(m, n)\ns1 = [\"John Adams\"]\ns2 = [\"James Madison\"]\nswap(s1, s2)\nx = [22/7]\ny = [-3]\nswap(x, y)\n", + "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": [], @@ -19,14 +34,40 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.2 The Bubble Sort Template\n'''\n\ndef 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\ndef print_( v,n):\n for i in range(n):\n print v[i],\n print \"\"\n \na = [55, 33, 88, 11, 44, 99, 77, 22, 66]\nprint_(a,9);\nsort(a,9)\nprint_(a,9)\ns = [\"Tom\", \"Hal\", \"Dan\", \"Bob\", \"Sue\", \"Ann\", \"Gus\"]\nprint_(s,7)\nsort(s,7)\nprint_(s,7)\n", + "input": [ + "\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 \n11 22 33 44 55 66 77 88 99 \nTom Hal Dan Bob Sue Ann Gus \nAnn Bob Dan Gus Hal Sue Tom \n" + "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 @@ -34,14 +75,51 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.3 A Stack Class Template\n'''\nclass 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 \nintStack1 = Stack(5)\nintStack2 = Stack(10)\ncharStack = Stack(8)\nintStack1.push(77)\ncharStack.push('A')\nintStack2.push(22)\ncharStack.push('E')\ncharStack.push('K')\nintStack2.push(44)\nprint intStack2.pop() \nprint intStack2.pop() \nif (intStack2.isEmpty()):\n print \"intStack2 is empty.\"\n", + "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\n22\nintStack2 is empty.\n" + "text": [ + "44\n", + "22\n", + "intStack2 is empty.\n" + ] } ], "prompt_number": 3 @@ -49,14 +127,33 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.4 A Vector Class Template\n= operator does work in python by defaultly.\n'''\nclass 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)\nv = Vector()\nv.data[5] = 127\nw = v\nx = Vector(3)\nprint w.size\n", + "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", + "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" + "text": [ + "8\n" + ] } ], "prompt_number": 4 @@ -64,14 +161,56 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.5 A Subclass Template for Vectors\n'''\nclass 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\nclass 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\nx = Array(1,3)\nx.data[0] = 3.14159\nx.data[1] = 0.08516\nx.data[2] = 5041.92\nprint \"x.size() = \" , x.size \nprint \"x.firstSubscript() = \" , x.firstSubscript() \nprint \"x.lastSubscript() = \" , x.lastSubscript()\nfor i in range(0,3):\n print \"x[\" , i + 1 , \"] = \" , x.data[i]\n", + "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\nx.firstSubscript() = 1\nx.lastSubscript() = 3\nx[ 1 ] = 3.14159\nx[ 2 ] = 0.08516\nx[ 3 ] = 5041.92\n" + "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 @@ -79,14 +218,45 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.6 A Matrix Class Template\n'''\n\nclass 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\na = Matrix(2,3)\na.vector[0][0] = 0.0\na.vector[0][1] = 0.1\na.vector[0][2] = 0.2\na.vector[1][0] = 1.0\na.vector[1][1] = 1.1\na.vector[1][2] = 1.2\n\nprint \"The matrix a has \" , a.rows , \" rows and \", a.columns , \" columns:\"\nfor i in range(2):\n for j in range(3):\n print a.vector[i][j] ,\n print \"\"\n", + "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:\n0.0 0.1 0.2 \n1.0 1.1 1.2 \n" + "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 @@ -94,14 +264,37 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.7 A List Class Template\nA list consists of a linked sequence of nodes. Each node contains one data item and a link to the next\nnode. So we begin by defining a ListNode class template:\n'''\n\nfriends = []\n\n\nfriends.insert(0,\"Bowen, Van\")\nfriends.insert(0,\"Dixon, Tom\")\nfriends.insert(0,\"Mason, Joe\")\nfriends.insert(0,\"White, Ann\")\n\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\nfriends.remove('White, Ann')\nprint \"Removed: \" , 'White, Ann'\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\n", + "input": [ + "\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 -> *\nRemoved: White, Ann\nMason, Joe -> Dixon, Tom -> Bowen, Van -> *\n" + "text": [ + "White, Ann -> Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\n", + "Removed: White, Ann\n", + "Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\n" + ] } ], "prompt_number": 7 @@ -109,14 +302,44 @@ { "cell_type": "code", "collapsed": false, - "input": "'''\nEXAMPLE 13.8\n'''\nfriends = []\nfriends.append(\"Bowen, Van\")\nfriends.append(\"Dixon, Tom\")\nfriends.append(\"Mason, Joe\")\nfriends.append(\"White, Ann\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.remove(\"Mason, Joe\")\nfriends[1] = \"Davis, Jim\"\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.insert(2,\"Morse, Sam\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfor i in range(len(friends)):\n print \"[\" ,friends[i] , \"]\" , '->' ,\nprint '*'\n", + "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 -> *\nBowen, Van -> Davis, Jim -> White, Ann -> *\nBowen, Van -> Davis, Jim -> Morse, Sam -> White, Ann -> *\n[ Bowen, Van ] -> [ Davis, Jim ] -> [ Morse, Sam ] -> [ White, Ann ] -> *\n" + "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 @@ -124,7 +347,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [] |