summaryrefslogtreecommitdiff
path: root/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb
diff options
context:
space:
mode:
authordebashisdeb2014-06-20 15:42:42 +0530
committerdebashisdeb2014-06-20 15:42:42 +0530
commit83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch)
treef54eab21dd3d725d64a495fcd47c00d37abed004 /Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb
parenta78126bbe4443e9526a64df9d8245c4af8843044 (diff)
downloadPython-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.gz
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.bz2
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.zip
removing problem statements
Diffstat (limited to 'Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb')
-rw-r--r--Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb676
1 files changed, 584 insertions, 92 deletions
diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb
index a355a8d2..191b1c78 100644
--- a/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb
+++ b/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch6"
+ "name": "",
+ "signature": "sha256:002670bb2c70e6ed5cc1d52c3936c4a90e901b6c8d78abcf5af4402c27118a1a"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,28 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.1 Using Direct Access on Arrays\n'''\n\na = [0, 0, 0]\na[2] = 55.55\na[0] = 11.11\na[1] = 33.33\nprint \"a[0] = \" , a[0] \nprint \"a[1] = \" , a[1] \nprint \"a[2] = \" , a[2] \n",
+ "input": [
+ "\n",
+ "\n",
+ "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\na[1] = 33.33\na[2] = 55.55\n"
+ "text": [
+ "a[0] = 11.11\n",
+ "a[1] = 33.33\n",
+ "a[2] = 55.55\n"
+ ]
}
],
"prompt_number": 1
@@ -25,49 +40,82 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.2 Printing a Sequence in Order\n'''\n\nSIZE=5 # defines the size N for 5 elements\na = []\n# declares the array's elements as type double\nprint \"Enter \" , SIZE , \" numbers:\\t\"\nfor i in range(SIZE):\n a.append(float(raw_input()))\n \nprint \"In reverse order: \"\nfor i in range(SIZE-1,-1,-1):\n print \"\\t\" , a[i]\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "Enter 5 numbers:\t\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1\n"
+ "text": [
+ "1\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2\n"
+ "text": [
+ "2\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "3.3\n"
+ "text": [
+ "3.3\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4.4\n"
+ "text": [
+ "4.4\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5.5\n"
+ "text": [
+ "5.5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "In reverse order: \n\t5.5\n\t4.4\n\t3.3\n\t2.0\n\t1.0\n"
+ "text": [
+ "In reverse order: \n",
+ "\t5.5\n",
+ "\t4.4\n",
+ "\t3.3\n",
+ "\t2.0\n",
+ "\t1.0\n"
+ ]
}
],
"prompt_number": 2
@@ -75,14 +123,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.3 Initializing an Array\nThis program initializes the array a and then prints its values:\n'''\n\na = [ 22.2, 44.4, 66.6 ]\n\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i]\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "\ta[ 0 ] = 22.2\n",
+ "\ta[ 1 ] = 44.4\n",
+ "\ta[ 2 ] = 66.6\n"
+ ]
}
],
"prompt_number": 3
@@ -90,14 +151,29 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.4 Initializing an Array with Trailing Zeros\n'''\n\na = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i] \n",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "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
@@ -105,14 +181,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.5 An Uninitialized Array\nNOte : In PYthon , we have to initialize array. Without initializing it, it doesn't work.\nArray size is automatically incremented when we push data into it.\n'''\nimport numpy\nSIZE = 4\na = numpy.zeros(4)\n# declares the array's elements as type float\nfor i in range(SIZE):\n print \"\\ta[\" , i , \"] = \" , a[i]\n",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "\ta[ 0 ] = 0.0\n",
+ "\ta[ 1 ] = 0.0\n",
+ "\ta[ 2 ] = 0.0\n",
+ "\ta[ 3 ] = 0.0\n"
+ ]
}
],
"prompt_number": 5
@@ -120,7 +209,14 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.6 Allowing an Array Index to Exceed its Bounds\nNOte : Python gives error when this kind of error occurs. so It wont print garbage values after bound exceeds.\n'''\nSIZE=4\na = [ 33.3, 44.4, 55.5, 66.6 ]\nfor i in range(7): # ERROR: index is out of bounds!\n print \"\\ta[\" , i , \"] = \" , a[i] \n\n",
+ "input": [
+ "\n",
+ "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": [
@@ -137,7 +233,13 @@
{
"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 ] = "
+ "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": 6
@@ -145,14 +247,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.7 Causing Side Effects\nThis program inadvertently changes the value of a variable when it accesses a nonexistent element of\nan array:\nNote : Python automatically increase size of array/list. so it wont give error and output would be differ than c gives.\n'''\n\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na.append(88.8) # ERROR: index is out of bounds!\nprint \"x = \" , x \n",
+ "input": [
+ "\n",
+ "\n",
+ "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\nx = 11.1\n"
+ "text": [
+ " x = 11.1\n",
+ "x = 11.1\n"
+ ]
}
],
"prompt_number": 7
@@ -160,7 +273,14 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.8 Causing Unhandled Exceptions\nThis program crashes because the array index gets too big:\n'''\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na[3333] = 88.8 # ERROR: index is out of bounds!\nprint \"x = \" , x \n",
+ "input": [
+ "\n",
+ "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": [
@@ -177,7 +297,9 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 11.1\n"
+ "text": [
+ "x = 11.1\n"
+ ]
}
],
"prompt_number": 8
@@ -185,14 +307,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.9 Passing an Array to a Function that Returns its Sum\n'''\n\ndef sum_(a):\n s = 0\n for i in a:\n s += i\n return s\n \na = [ 11, 33, 55, 77 ]\nprint \"sum(a) = \" , sum_(a) \n",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "sum(a) = 176\n"
+ ]
}
],
"prompt_number": 10
@@ -200,49 +335,86 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.10 Input and Output Functions for an Array\nThis program uses a read() function to input values into the array a interactively. Then it uses a\nprint() function to print the array:\n'''\ndef 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\ndef print_(a):\n for i in a:\n print i ,\n\n\na = []\nread(a)\nprint \"The array has \" , len(a) , \" elements: \"\nprint_(a)\n",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "Enter integers. Terminate with 0:\n",
+ "\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[0]: 11\n"
+ "text": [
+ "a[0]: 11\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[1]: 22\n"
+ "text": [
+ "a[1]: 22\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[2]: 33\n"
+ "text": [
+ "a[2]: 33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[3]: 44\n"
+ "text": [
+ "a[3]: 44\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[4]: 0\n"
+ "text": [
+ "a[4]: 0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The array has 4 elements: \n11 22 33 44\n"
+ "text": [
+ "The array has 4 elements: \n",
+ "11 22 33 44\n"
+ ]
}
],
"prompt_number": 12
@@ -250,14 +422,22 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.11 Printing the Memory Location of an Array\nThis program prints the value of the address stored in an array name.\n'''\nimport sys\na = [ 22, 44, 66, 88 ]\nprint \"a = \" , id(a) # the address of a[0]\n\n",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "a = 169156908\n"
+ ]
}
],
"prompt_number": 13
@@ -265,14 +445,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.12 The Linear Search\nThis program tests a function that implements the Linear Search algorithm:\n'''\ndef 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\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(50,a,7) = \" , index(50,a,7) \n\n\n",
+ "input": [
+ "\n",
+ "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\nindex(50,a,7) = 7\n"
+ "text": [
+ "index(44,a,7) = 1\n",
+ "index(50,a,7) = 7\n"
+ ]
}
],
"prompt_number": 14
@@ -280,14 +476,38 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.13 The Bubble Sort\nThis program tests a function that implements the Bubble Sort algorithm.\n'''\ndef 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\ndef print_(a):\n for i in range(len(a)):\n print a[i],\n print ''\n \na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n\nprint_(a)\nsort(a,8)\nprint_(a)\n",
+ "input": [
+ "\n",
+ "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 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n"
+ "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
@@ -295,14 +515,39 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.14 The Binary Search Algorithm\n'''\n\ndef 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\na = [ 22, 33, 44, 55, 66, 77, 88 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(60,a,7) = \" , index(60,a,7) \n\n",
+ "input": [
+ "\n",
+ "\n",
+ "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\nindex(60,a,7) = 7\n"
+ "text": [
+ "index(44,a,7) = 2\n",
+ "index(60,a,7) = 7\n"
+ ]
}
],
"prompt_number": 16
@@ -310,14 +555,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.15 Determining whether an Array is Sorted\nThis program tests a boolean function that determines whether a given array is nondecreasing.\n'''\n\ndef 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\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"isNondecreasing(a,4) = \" , isNondecreasing(a,4)\nprint \"isNondecreasing(a,7) = \" , isNondecreasing(a,7)\n",
+ "input": [
+ "\n",
+ "\n",
+ "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\nisNondecreasing(a,7) = False\n"
+ "text": [
+ "isNondecreasing(a,4) = True\n",
+ "isNondecreasing(a,7) = False\n"
+ ]
}
],
"prompt_number": 17
@@ -325,7 +586,39 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.16 Using the assert() Function to Enforce a Precondition\nThis program tests an improved version of the search() function from Example 6.14. This version\nuses the isNondecreasing() function from Example 6.15 to determine whether the array is sorted. It\npasses the resulting boolean return value to the assert() function so that the search will not be carried\nout if the array is not sorted:\n'''\n\ndef 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\ndef 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\na = [ 22, 33, 44, 55, 66, 77, 88, 60 ]\nprint \"index(44,a,7) = \" , index(44,a,7) \nprint \"index(44,a,8.py) = \" , index(44,a,8) \nprint \"index(60,a,7) = \" , index(60,a,8)\n",
+ "input": [
+ "\n",
+ "\n",
+ "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(44,a,8.py) = \" , index(44,a,8) \n",
+ "print \"index(60,a,7) = \" , index(60,a,8)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -343,7 +636,10 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "index(44,a,7) = 2\nindex(44,a,7) = "
+ "text": [
+ "index(44,a,7) = 2\n",
+ "index(44,a,7) = "
+ ]
}
],
"prompt_number": 18
@@ -351,14 +647,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.17 Enumerating the Days of the Week\nThis program defines an array high[] of seven floats, representing the high temperatures for\nthe seven days of a week: 138\n'''\nDay = [ 0, 1, 2, 3, 4, 5, 6 ]\nhigh = [ 88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7]\n\nfor i in Day:\n print \"The high temperature for day \" , i , \" was \" , high[i] \n\n",
+ "input": [
+ "\n",
+ "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\nThe high temperature for day 1 was 95.0\nThe high temperature for day 2 was 91.2\nThe high temperature for day 3 was 89.9\nThe high temperature for day 4 was 91.4\nThe high temperature for day 5 was 92.5\nThe high temperature for day 6 was 86.7\n"
+ "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
@@ -366,14 +678,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.18 The Bubble Sort Again\n'''\n\ndef sort(a,n):\n a.sort()\n\ndef print_(a,n):\n for i in a:\n print i,\n print ''\na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\nprint_(a,8);\nsort(a,8)\nprint_(a,8)\n",
+ "input": [
+ "\n",
+ "\n",
+ "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 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n"
+ "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
@@ -381,119 +710,180 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.19 Reading and Printing a Two-Dimensional Array\nThis program shows how a two-dimensional array can be processed:\n'''\n\ndef 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\ndef print_(a):\n for i in range(3):\n for j in range(5):\n print a[i][j],\n print ''\na = []\nread(a)\nprint_(a)\n",
+ "input": [
+ "\n",
+ "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\nRow 0 : "
+ "text": [
+ "Enter 15 integers, 5 per row:\n",
+ "\n",
+ "Row 0 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "11\n"
+ "text": [
+ "11\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Row 1 : "
+ "text": [
+ " Row 1 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "60\n"
+ "text": [
+ "60\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "50\n"
+ "text": [
+ "50\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "30\n"
+ "text": [
+ "30\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "90\n"
+ "text": [
+ "90\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "70\n"
+ "text": [
+ "70\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Row 2 : "
+ "text": [
+ " Row 2 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "85\n"
+ "text": [
+ "85\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "25\n"
+ "text": [
+ "25\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "45\n"
+ "text": [
+ "45\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "45\n"
+ "text": [
+ "45\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " 44 77 33 11 44 \n60 50 30 90 70 \n85 25 45 45 55 \n"
+ "text": [
+ " 44 77 33 11 44 \n",
+ "60 50 30 90 70 \n",
+ "85 25 45 45 55 \n"
+ ]
}
],
"prompt_number": 21
@@ -501,119 +891,204 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.20 Processing a Two-Dimensional Array of Quiz Scores\n'''\ndef 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\ndef 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\ndef 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\nNUM_STUDENTS = 3\nNUM_QUIZZES = 5\n\n\nscore = []\nprint \"Enter \" , NUM_QUIZZES , \" scores for each student: \"\nread(score)\nprint \"The quiz averages are:\"\nprintQuizAverages(score)\nprint \"The class averages are: \"\nprintClassAverages(score)\n",
+ "input": [
+ "\n",
+ "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: \nStudent 0 : "
+ "text": [
+ "Enter 5 scores for each student: \n",
+ "Student 0 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Student 1 : "
+ "text": [
+ " Student 1 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Student 2 : "
+ "text": [
+ " Student 2 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "6\n"
+ "text": [
+ "6\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "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\nThe 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"
+ "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
@@ -621,14 +1096,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.21 Processing a Three-Dimensional Array\nThis program simply counts the number of zeros in a three-dimensional array:\n'''\n\ndef 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\na = [ [ [5,0,2], [0,0,9], [4,1,0], [7,7,7] ],[ [3,0,0], [8,5,0], [0,0,0], [2,0,9] ]]\nprint \"This array has \" , numZeros(a,2,4,3) , \" zeros\"\n\n",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "This array has 11 zeros\n"
+ ]
}
],
"prompt_number": 25
@@ -636,7 +1128,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []