{ "metadata": { "name": "Chapter VII" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7: Working with arrays" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.1, Page number: 99" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "values=[0]*10\n", "\n", "values[0]=197\n", "values[2]=-100\n", "values[5]=350\n", "values[3]=values[0]+values[5]\n", "values[9]=values[5]/10\n", "values[2]-=1\n", "i=0\n", "\n", "for index in values:\n", " print(\"values[{0}] = {1}\".format(i,index))\n", " i=i+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "values[0] = 197\n", "values[1] = 0\n", "values[2] = -101\n", "values[3] = 547\n", "values[4] = 0\n", "values[5] = 350\n", "values[6] = 0\n", "values[7] = 0\n", "values[8] = 0\n", "values[9] = 35\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.2, Page number: 101" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "ratingCounters=[0]*11\n", "\n", "for i in range(1,11):\n", " ratingCounters[i]=0\n", "\n", "print(\"Enter your Responses:\")\n", "\n", "for i in range (1,21):\n", " response=5 #score=5 for all iterations\n", " #response=input()\n", "\n", " if(response<1 or response>10):\n", " print(\"Bad response: {0}\".format(response))\n", " else:\n", " ratingCounters[response]+=1\n", "\n", "print(\"\\n\\n Rating | Number of Responses\\n\")\n", "print(\" ----- -------------------\")\n", "\n", "for i in range(1,11):\n", " print(\" {0} {1}\".format(i,ratingCounters[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your Responses:\n", "\n", "\n", " Rating | Number of Responses\n", "\n", " ----- -------------------\n", " 1 0\n", " 2 0\n", " 3 0\n", " 4 0\n", " 5 20\n", " 6 0\n", " 7 0\n", " 8 0\n", " 9 0\n", " 10 0\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.3, Page number: 103" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "Fibonacci=[0]*15\n", "\n", "Fibonacci[0]=0\n", "Fibonacci[1]=1\n", "\n", "for i in range(2,15):\n", " Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1] #Calculation\n", "\n", "for i in range(15):\n", " print(\"{0}\".format(Fibonacci[i])) #Print Result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n", "55\n", "89\n", "144\n", "233\n", "377\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.4, Page number: 105" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "primeIndex=2\n", "primes=[0]*50\n", "\n", "primes[0]=2\n", "primes[1]=3\n", "p=5\n", "\n", "while(p<=50): #Outer Loop-1\n", " isPrime=True\n", " \n", " i=1\n", " while(isPrime and p/primes[i]>=primes[i]): #Inner Loop\n", " if(p%primes[i]==0):\n", " isPrime=False\n", " i+=1\n", " \n", "\n", " if(isPrime==True):\n", " primes[primeIndex]=p\n", " primeIndex+=1\n", " p=p+2\n", "\n", "for i in range (primeIndex): #Outer Loop-2\n", " print(\"{0}\".format(primes[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n", "3\n", "5\n", "7\n", "11\n", "13\n", "17\n", "19\n", "23\n", "29\n", "31\n", "37\n", "41\n", "43\n", "47\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.5, Page number: 107" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "array_values=[0]*10\n", "array_values[0:4]=[0,1,4,9,16]\n", "\n", "for i in range(5,10):\n", " array_values[i]=i*i\n", "\n", "for i in range(10):\n", " print(\"array values[{0}] = {1}\".format(i,array_values[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "array values[0] = 0\n", "array values[1] = 1\n", "array values[2] = 4\n", "array values[3] = 9\n", "array values[4] = 16\n", "array values[5] = 25\n", "array values[6] = 36\n", "array values[7] = 49\n", "array values[8] = 64\n", "array values[9] = 81\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.6, Page number: 108" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys #For sys.stdout.write()\n", "\n", "word=['H','e','l','l','o','!']\n", "\n", "for i in word:\n", " sys.stdout.write(\"{0} \".format(i))\n", "print(\"\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "H e l l o ! \n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.7, Page number: 110" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys #for sys.stdout.write()\n", "\n", "baseDigits=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']\n", "convertedNumber=[0]*10\n", "index=0\n", "\n", "numberToConvert=234 #numberToConvert=input(\"Number to be converted?: \")\n", "base=2 #base=input(\"Base?: \")\n", "\n", "while True:\n", " convertedNumber[index]=numberToConvert%base\n", " index+=1\n", " numberToConvert=numberToConvert/base\n", " if(numberToConvert==0):\n", " break\n", "\n", "print(\"Converted Number= \")\n", "index-=1\n", "while(index>=0):\n", " nextDigit=convertedNumber[index]\n", " sys.stdout.write(\"{0} \".format(baseDigits[nextDigit]))\n", " index-=1\n", "\n", "print(\"\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Converted Number= \n", "1 1 1 0 1 0 1 0 \n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 7.8, Page number: 116" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys #For sys.exit() \n", " \n", "numFibs=18 #numFibs=input(\"How many Fibonacci numbers do you want (between 1 and 75)? \")\n", "\n", "if(numFibs<1 or numFibs>75):\n", " print(\"bad number,sorry!\")\n", " sys.exit()\n", "\n", "Fibonacci=[0]*numFibs\n", "Fibonacci[0]=0\n", "Fibonacci[1]=1\n", "\n", "for i in range(2,numFibs):\n", " Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1] \n", " \n", "for i in range(numFibs):\n", " print(\"{0}\".format(Fibonacci[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n", "55\n", "89\n", "144\n", "233\n", "377\n", "610\n", "987\n", "1597\n" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }