{ "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\u001b[0m in \u001b[0;36m\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\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": "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]