{ "metadata": { "name": "", "signature": "sha256:a27de4c3e22de467837d62cdd94a3b4f20a866984babfe11880e076feb1374ad" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4 : Arrays" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.1, Page No 79" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Declarations\n", "\n", "iNoOfDays = 31\n", "dSum = 0\n", "dblTempApr=[[0 for col in range(31)] for row in range(31)]\n", "\n", "\n", "#Entry and calculation\n", "\n", "for i in range(1,iNoOfDays):\n", " dblTempApr[i] = raw_input(\"Temperature Day %d: \" % i)\n", " dSum += dblTempApr[i]\n", "\n", "dAvg = dSum / iNoOfDays\n", "\n", "#Printout\n", "\n", "print \"Average temperature: %f\" % dAvg\n", "print \"Temperatures exceeding average: \"\n", "\n", "for i in range(1,iNoOfDays):\n", " if (dblTempApr[i] > dAvg):\n", " print \"Day no.: %d\" % i,\n", " print \" temp: %d\" % dblTempApr[i]" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 1: 32\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 2: 33\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 3: 35\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 4: 36\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 5: 34\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 6: 32\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 7: 31\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 8: 29\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 9: 28\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 10: 27\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 11: 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 12: 26\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 13: 35\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 14: 36\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 15: 35\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 16: 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 17: 40\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 18: 42\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 19: 44\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 20: 43\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 21: 41\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 22: 38\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 23: 37\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 24: 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 25: 36\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 26: 35\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 27: 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 28: 32\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 29: 21\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Temperature Day 30: 15\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average temperature: 32.000000\n", "Temperatures exceeding average: \n", "Day no.: 2 temp: 33\n", "Day no.: 3 temp: 35\n", "Day no.: 4 temp: 36\n", "Day no.: 5 temp: 34\n", "Day no.: 13 temp: 35\n", "Day no.: 14 temp: 36\n", "Day no.: 15 temp: 35\n", "Day no.: 16 temp: 50\n", "Day no.: 17 temp: 40\n", "Day no.: 18 temp: 42\n", "Day no.: 19 temp: 44\n", "Day no.: 20 temp: 43\n", "Day no.: 21 temp: 41\n", "Day no.: 22 temp: 38\n", "Day no.: 23 temp: 37\n", "Day no.: 25 temp: 36\n", "Day no.: 26 temp: 35\n", "Day no.: 27 temp: 45\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.2, Page No 83" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Declaration\n", "\n", "iMaxNo = 100\n", "dLimit = 5000\n", "perc1 = 0.1\n", "perc2 = 0.15\n", "sales = [[0 for col in range(1)] for row in range(iMaxNo)]\n", "\n", "#Initialize array\n", "\n", "for i in range(0,iMaxNo):\n", " sales[i] = 0\n", "\n", "#Enter Salesman info\n", "nr=input(\"Enter Salesman no. : \")\n", "dAmount=input(\"Enter sales amount: \")\n", "\n", "while (nr and dAmount):\n", " if(nr<1 or nr>iMaxNo or dAmount<0):\n", " print \"Input error\"\n", " break;\n", " else:\n", " sales[nr-1]+= dAmount\n", " nr=int(raw_input(\"Enter Salesman no. : \"))\n", " dAmount=float(raw_input(\"Enter sales amount: \"))\n", "\n", "#Print summary\n", "\n", "print \"Number Amount Fee\"\n", "print \"====== ====== ===\"\n", "\n", "for i in range(0,iMaxNo):\n", " if (sales[i] > 0):\n", " if(sales[i] <= dLimit):\n", " dFee = perc1 * sales[i]\n", " else:\n", " dFee = perc1 * dLimit + perc2*(sales[i] - dLimit)\n", " j=i;\n", " j=j+1;\n", " print \"%4d\" % j,\n", " print \"%13d\" % sales[i],\n", " print \"%10.0f\" % dFee" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Salesman no. : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter sales amount: 1000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Salesman no. : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter sales amount: 2000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Salesman no. : 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter sales amount: 3000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Salesman no. : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter sales amount: 4000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Salesman no. : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter sales amount: 5000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Salesman no. : 6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter sales amount: 00\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Number Amount Fee\n", "====== ====== ===\n", " 1 1000 100\n", " 2 2000 200\n", " 3 3000 300\n", " 4 4000 400\n", " 5 5000 500\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.3, Page No 92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Declaration\n", "l=0\n", "r=30\n", "iFound=0\n", "# Array is in sorted form\n", "iProdid= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]\n", "\n", "iSrch = raw_input(\"Enter the searched product id: \")\n", "\n", "if(iSrch == iProdid[0]):\n", " iPos = 0\n", " iFound= 1\n", "\n", "if (iSrch == iProdid[30]):\n", " iPos = 30\n", " iFound = 1\n", "\n", "while not (iFound):\n", " iMid = (int)(l + ((r-l)/2))\n", " if(iSrch == iProdid[iMid]):\n", " iFound = 1\n", " iPos = iMid\n", "\n", " if(iSrch > iProdid[iMid]):\n", " l=iMid\n", " else:\n", " r=iMid" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the searched product id: 15\n" ] } ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }