{ "metadata": { "name": "ch6" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "a[0] = 11.11\na[1] = 33.33\na[2] = 55.55\n" } ], "prompt_number": 1 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter 5 numbers:\t\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "1\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "2\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "3.3\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "4.4\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "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" } ], "prompt_number": 2 }, { "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", "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": "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", "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": "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", "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": "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", "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\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\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 7\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----> 8\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 9\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": 6 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": " x = 11.1\nx = 11.1\n" } ], "prompt_number": 7 }, { "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", "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\u001b[0m in \u001b[0;36m\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": "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "sum(a) = 176\n" } ], "prompt_number": 10 }, { "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", "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: \n11 22 33 44\n" } ], "prompt_number": 12 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "a = 169156908\n" } ], "prompt_number": 13 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "index(44,a,7) = 1\nindex(50,a,7) = 7\n" } ], "prompt_number": 14 }, { "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", "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" } ], "prompt_number": 15 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "index(44,a,7) = 2\nindex(60,a,7) = 7\n" } ], "prompt_number": 16 }, { "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]\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 34\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[1;36m22\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m33\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m55\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m66\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m77\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m88\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m60\u001b[0m \u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 35\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(44,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 36\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(44,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 37\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(60,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m60\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m\u001b[0m in \u001b[0;36mindex\u001b[1;34m(x, a, n)\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;31m# PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;31m# binary search:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0misNondecreasing\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[0mlo\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mAssertionError\u001b[0m: " ] }, { "output_type": "stream", "stream": "stdout", "text": "index(44,a,7) = 2\nindex(44,a,7) = " } ], "prompt_number": 18 }, { "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", "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" } ], "prompt_number": 19 }, { "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", "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" } ], "prompt_number": 20 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter 15 integers, 5 per row:\n\nRow 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 \n60 50 30 90 70 \n85 25 45 45 55 \n" } ], "prompt_number": 21 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter 5 scores for each student: \nStudent 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\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" } ], "prompt_number": 24 }, { "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", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "This array has 11 zeros\n" } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }