{
 "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": {}
  }
 ]
}