{ "metadata": { "name": "Chapter VIII" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Chapter 8: Working with functions" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.1, Page number: 120" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.1.py\n", "#Writing a Function in python\n", "\n", "\n", "def printMessage(): #Function to Print Message\n", " print(\"Programming is Fun.\\n\") #Print Statement\n", "\n", "\n", "def main(): #Main() function\n", " printMessage()\n", "\n", "\n", "if __name__ =='__main__': #Setting Top-level conditional script\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Programming is Fun.\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.2, Page number: 121" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.2.py\n", "#Calling Functions\n", "\n", "\n", "def printMessage(): #Function to Print Message\n", " print(\"Programming is Fun.\\n\") #Print Statement\n", "\n", "\n", "def main(): #Main() function\n", " printMessage() #First Function call\n", " printMessage() #Second Function Call\n", "\n", "if __name__ =='__main__': #Setting Top-level conditional script\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Programming is Fun.\n", "\n", "Programming is Fun.\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.3, Page number: 122" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.3.py\n", "#More on Calling Functions\n", "\n", "\n", "def printMessage(): #Function to Print Message\n", " print(\"Programming is Fun.\\n\") #Print Statement\n", "\n", "\n", "def main(): \n", " for i in range (1,6): #Main() function\n", " printMessage()\n", "\n", "\n", "if __name__ =='__main__': #Setting Top-level script environment\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Programming is Fun.\n", "\n", "Programming is Fun.\n", "\n", "Programming is Fun.\n", "\n", "Programming is Fun.\n", "\n", "Programming is Fun.\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.4, Page number: 123" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.4.py\n", "#Calculating the nth Triangular Number\n", "\n", "\n", "def calculateTriangularNumber(n): #Function to calculate triangular number\n", " triangularNumber=0 #Variable Dclaration\n", " \n", " for i in range (1,n+1): #Calculation/Iteration\n", " triangularNumber+=i\n", " \n", " print(\"Triangular Number {0} is {1}\".format(n,triangularNumber))\n", "\n", "\n", "def main():\n", " calculateTriangularNumber(10) #Function call-1\n", " calculateTriangularNumber(20) #Function call-2\n", " calculateTriangularNumber(50) #Function call-3\n", " \n", "\n", "if __name__=='__main__': #Setting top level conditional script\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Triangular Number 10 is 55\n", "Triangular Number 20 is 210\n", "Triangular Number 50 is 1275\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.5, Page number: 125" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.5.py\n", "#Revising the Program to Find the Greatest Common Divisor\n", "\n", "#Import Library \n", "import sys \n", "\n", "def gcd(u,v): #Function to calculate gcd\n", " sys.stdout.write(\"gcd of {0} and {1} is: \".format(u,v))\n", " while(v!=0):\n", " temp=u%v\n", " u=v\n", " v=temp\n", " sys.stdout.write(\"{0}\\n\".format(u))\n", "\n", "\n", "\n", "def main(): #Main() function\n", " gcd(150,35) \n", " gcd(1026,405)\n", " gcd(83,240)\n", "\n", "if __name__=='__main__': #Setting Top level conditional script \n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "gcd of 150 and 35 is: 5\n", "gcd of 1026 and 405 is: 27\n", "gcd of 83 and 240 is: 1\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.6, Page number: 127" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.6.py\n", "#Finding the Greatest Common Divisor and Returning the Results\n", "\n", "#Import Library \n", "import sys \n", "\n", "def gcd(u,v): #Function to calculate gcd\n", "\n", " while(v!=0):\n", " temp=u%v\n", " u=v\n", " v=temp\n", " return u\n", "\n", "def main(): #Main() function\n", " result= gcd(150,35) \n", " print(\"the gcd of 150 and 35 is: {0}\".format(result))\n", " result=gcd(1026,405)\n", " print(\"the gcd of 1026 and 405 is: {0}\".format(result)) \n", " result= gcd(83,240)\n", " print(\"the gcd of 83 and 240 is: {0}\".format(result))\n", "\n", "if __name__=='__main__': #Top level conditional script \n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "the gcd of 150 and 35 is: 5\n", "the gcd of 1026 and 405 is: 27\n", "the gcd of 83 and 240 is: 1\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.7, Page number: 129" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.7.py\n", "#Calculating the Absolute Value\n", "\n", "#yourstory.in\n", "\n", "#Calculations\n", "def absoluteValue(x): #Function to calculate & return absolute values\n", " if(x<0):\n", " x=-x\n", " return x\n", "\n", "def main(): #Main() Function\n", " \n", " f1=-15.5\n", " f2=20.0\n", " f3=-5.0\n", " il=-716\n", "#Result \n", " result=absoluteValue(f1)\n", " print(\"result= {0:.2f}\".format(result))\n", " print(\"f1={0:.2f}\".format(f1))\n", "\n", " result=absoluteValue(f2)+absoluteValue(f3)\n", " print(\"result= {0:.2f}\".format(result))\n", "\n", " result=absoluteValue(float(il))\n", " print(\"result= {0:.2f}\".format(result))\n", " \n", " result=absoluteValue(il)\n", " print(\"resut= {0:.2f}\".format(result))\n", "\n", " print(\"{0:.2f}\".format(absoluteValue((-6.0)/4)))\n", "\n", "\n", "#End of Main()\n", " \n", "if __name__=='__main__': #Setting Top level conditional script\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "result= 15.50\n", "f1=-15.50\n", "result= 25.00\n", "result= 716.00\n", "resut= 716.00\n", "1.50\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.8, Page number: 132" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.8.py\n", "#Calculating the Square Root of a Number using Newton-Raphson Method \n", "\n", "\n", "#Function to return absolute value\n", "def absoluteValue(x): \n", " if(x<0):\n", " x=-x\n", " return x\n", "\n", "#function to calculate square root\n", "def squareRoot(x):\n", " epsilon=0.0001\n", " guess=1.0\n", " while(absoluteValue(guess*guess-x)>=epsilon):\n", " guess=(x/guess+guess)/2.0\n", " return guess\n", "\n", "#Main()\n", "def main():\n", " print(\"squareRoot (2.0) = {0}\".format(squareRoot (2.0)));\n", " print(\"squareRoot (144.0) = {0}\".format(squareRoot (144.0)));\n", " print(\"squareRoot (17.5) = {0}\".format(squareRoot (17.5)));\n", "\n", "#Setting top level conditional script\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "squareRoot (2.0) = 1.41421568627\n", "squareRoot (144.0) = 12.0000000124\n", "squareRoot (17.5) = 4.18330153622\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.9, Page number: 138" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.9.py\n", "#Finding the Minimum Value in an Array\n", "\n", "#Function to return minimum value\n", "def minimum(values):\n", " minValue=values[0] #Variable Declaration\n", " for i in range (1,10): #Calculation\n", " if(values[i]a[j]): #Conditional swapping\n", " temp=a[i]\n", " a[i]=a[j]\n", " a[j]=temp\n", "\n", "#Main()\n", "def main():\n", "\n", " #List/Variable Declaration \n", " array= [ 34, -5, 6, 0, 12, 100, 56, 22,\\\n", " 44, -3, -9, 12, 17, 22, 6, 11 ]\n", " \n", " print(\"The array before the sort:\\n\");\n", " for i in range (0,16):\n", " sys.stdout.write(\"{0} \".format(array[i]))\n", " \n", " sort (array, 16); #Function Call\n", " \n", " print(\"\\n\\n\\nThe array after the sort:\\n\")\n", " for i in range (0,16):\n", " sys.stdout.write(\"{0} \".format(array[i]))\n", " print(\"\\n\")\n", "\n", "\n", "#Setting top level conditional script \n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The array before the sort:\n", "\n", "34 -5 6 0 12 100 56 22 44 -3 -9 12 17 22 6 11 \n", "\n", "\n", "The array after the sort:\n", "\n", "-9 -5 -3 0 6 6 11 12 12 17 22 22 34 44 56 100 \n", "\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.13, Page number: 147" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.13.py\n", "#Using Multidimensional Arrays and Functions\n", "\n", "#Import system Library\n", "import sys\n", "\n", "#Main()\n", "def main():\n", "\n", " #List/Variable Declaration\n", " sampleMatrix =[ [ 7, 16, 55, 13, 12 ],\n", " [ 12, 10, 52, 0, 7 ],\n", " [ -2, 1, 2, 4, 9 ] ]\n", "\n", "\n", " print(\"Original matrix:\\n\")\n", " displayMatrix(sampleMatrix) #Function call-1\n", "\n", " scalarMultiply(sampleMatrix, 2) #Function Call-2\n", " print(\"\\nMultiplied by 2:\\n\")\n", " \n", " displayMatrix(sampleMatrix); #Function call-3\n", " scalarMultiply(sampleMatrix, -1) #Function call-4\n", " \n", " print(\"\\nThen multiplied by -1:\\n\")\n", " displayMatrix(sampleMatrix) #Function call-5\n", "\n", "\n", "#Function to multiply matrix by a scalar quantity \n", "def scalarMultiply(matrix,scalar):\n", " for row in range(0,3): #Calculation\n", " for column in range(0,5):\n", " matrix[row][column]*=scalar \n", "\n", "\n", "#Function to display the matrix\n", "def displayMatrix(matrix):\n", " for row in range(0,3): #Result\n", " for column in range(0,5):\n", " sys.stdout.write(\"{0:7}\".format(matrix[row][column]))\n", " sys.stdout.write(\"\\n\")\n", "\n", "\n", "#Setting top level conditional script\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original matrix:\n", "\n", " 7 16 55 13 12\n", " 12 10 52 0 7\n", " -2 1 2 4 9\n", "\n", "Multiplied by 2:\n", "\n", " 14 32 110 26 24\n", " 24 20 104 0 14\n", " -4 2 4 8 18\n", "\n", "Then multiplied by -1:\n", "\n", " -14 -32 -110 -26 -24\n", " -24 -20 -104 0 -14\n", " 4 -2 -4 -8 -18\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.13A, Page number: 150" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.13A.py\n", "#Multidimensional Variable-Length Arrays\n", "\n", "#Import system Library\n", "import sys\n", "\n", "#Main()\n", "def main():\n", "\n", " #List/Variable Declaration\n", " sampleMatrix =[ [ 7, 16, 55, 13, 12 ],\n", " [ 12, 10, 52, 0, 7 ],\n", " [ -2, 1, 2, 4, 9 ] ]\n", "\n", "\n", " print(\"Original matrix:\\n\")\n", " displayMatrix(3,5,sampleMatrix) #Function call-1\n", "\n", " scalarMultiply(3,5,sampleMatrix, 2) #Function Call-2\n", " print(\"\\nMultiplied by 2:\\n\")\n", " \n", " displayMatrix(3,5,sampleMatrix); #Function call-3\n", " scalarMultiply(3,5,sampleMatrix, -1) #Function call-4\n", " \n", " print(\"\\nThen multiplied by -1:\\n\")\n", " displayMatrix(3,5,sampleMatrix) #Function call-5\n", "\n", "\n", "#Function to multiply matrix by a scalar quantity \n", "def scalarMultiply(nRows,nCols,matrix,scalar):\n", " for row in range(0,nRows): #Calculation\n", " for column in range(0,nCols):\n", " matrix[row][column]*=scalar \n", "\n", "\n", "#Function to display the matrix\n", "def displayMatrix(nRows,nCols,matrix):\n", " for row in range(0,nRows): #Result\n", " for column in range(0,nCols):\n", " sys.stdout.write(\"{0:7}\".format(matrix[row][column]))\n", " sys.stdout.write(\"\\n\")\n", "\n", "\n", "#Setting top level conditional script\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original matrix:\n", "\n", " 7 16 55 13 12\n", " 12 10 52 0 7\n", " -2 1 2 4 9\n", "\n", "Multiplied by 2:\n", "\n", " 14 32 110 26 24\n", " 24 20 104 0 14\n", " -4 2 4 8 18\n", "\n", "Then multiplied by -1:\n", "\n", " -14 -32 -110 -26 -24\n", " -24 -20 -104 0 -14\n", " 4 -2 -4 -8 -18\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.14, Page number: 153" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.14.py\n", "#Converting a Positive Integer to Another Base\n", "\n", "#Import system Libraries\n", "import sys\n", "\n", "#Function to get Number & Base\n", "def getNumberAndBase():\n", " \n", " #Global Reference\n", " global digit\n", " global numberToConvert\n", " global base\n", " \n", " #Variable Declaration\n", " digit=0\n", " numberToConvert=420 #numberToConvert=input(\"Number to be converted ?\")\n", " base=8 #base=input(\"Base?\")\n", " if(base<2 or base>16):\n", " print(\"Bad base - must be between 2 and 16\\n\");\n", " base = 10;\n", "\n", "#Conversion Function\n", "def convertNumber():\n", " \n", " #Global Reference\n", " global numberToConvert\n", " global base\n", " global convertedNumber\n", " global digit\n", " convertedNumber=[0]*64 #List declaration\n", " \n", " while(numberToConvert!=0):\n", " convertedNumber[digit]=numberToConvert%base #Calculations\n", " digit=digit+1\n", " numberToConvert/=base\n", "\n", "\n", "#Function to display\n", "def displayConvertedNumber():\n", "\n", " #Global reference \n", " global baseDigits\n", " global digit \n", "\n", " #List/Variable Declaration\n", " baseDigits=[ '0', '1', '2', '3', '4', '5', '6', '7',\\\n", " '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]\n", " sys.stdout.write(\"Converted number = \")\n", " \n", " digit=digit-1\n", "\n", " for i in range(digit,-1,-1):\n", " nextDigit = convertedNumber[i];\n", " sys.stdout.write(\"{0}\".format(baseDigits[nextDigit])); #Result\n", "\n", " sys.stdout.write(\"\\n\")\n", "\n", "\n", "\n", "#Main()\n", "def main():\n", " getNumberAndBase() #Function call-1\n", " convertNumber() #Function call-2\n", " displayConvertedNumber() #Function call-3\n", "\n", "\n", "#Setting top level conditional script\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Converted number = 644\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.15, Page number: 157" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.15.py\n", "#Illustrating Static and Automatic Variables\n", "\n", "\n", "#Function to display variables\n", "def auto_static():\n", " global staticVar #Global reference\n", " autoVar=1 #variable Declaration\n", " print(\"automatic = {0}, static = {1}\\n\".format(autoVar,staticVar)); #Result\n", " \n", " #Calculation \n", " autoVar+=1\n", " staticVar+=1\n", "\n", "#Main()\n", "def main():\n", " global staticVar\n", " staticVar=1 #Variable Declaration\n", " for i in range(0,5):\n", " auto_static()\n", "\n", "#Setting top level conditional script\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "automatic = 1, static = 1\n", "\n", "automatic = 1, static = 2\n", "\n", "automatic = 1, static = 3\n", "\n", "automatic = 1, static = 4\n", "\n", "automatic = 1, static = 5\n", "\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 8.16, Page number: 159" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#8.16.py\n", "#Calculating Factorials Recursively\n", "\n", "#Function to calculate factorial\n", "def factorial(n):\n", " if( n == 0 ): #Calculation\n", " result = 1\n", " else:\n", " result = n * factorial (n - 1)\n", " return result;\n", "\n", "#Main()\n", "def main():\n", " for j in range (0,11):\n", " print(\"{0:3}! = {1}\\n\".format(j,factorial (j)))\n", "\n", "\n", "#Setting top level conditional script\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 0! = 1\n", "\n", " 1! = 1\n", "\n", " 2! = 2\n", "\n", " 3! = 6\n", "\n", " 4! = 24\n", "\n", " 5! = 120\n", "\n", " 6! = 720\n", "\n", " 7! = 5040\n", "\n", " 8! = 40320\n", "\n", " 9! = 362880\n", "\n", " 10! = 3628800\n", "\n" ] } ], "prompt_number": 17 } ], "metadata": {} } ] }