{ "metadata": { "name": "", "signature": "sha256:0c7b2e51d9ff140f666ba709dd09beff8baf4fe046109218b6f3ac69ed4bb2fe" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1 , Page number: CP-173" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to write a function to find factorial and use it to find nCr\n", "# Variable declaration\n", "\n", "n = 5\n", "r = 3\n", "\n", "print \"Enter value to n and r :\",n,r\n", "\n", "# function subprogram to find factorial\n", "\n", "def fact(k):\n", " p = 1\n", " for i in range(1,k+1):\n", " p = p * i\n", " return p\n", "\n", "\n", "ncr = fact(n) / (fact(r) * fact(n-r))\n", "\n", "\n", "print \"Value of nCr =\",ncr" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter value to n and r : 5 3\n", "Value of nCr = 10\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2 , Page number: CP-174" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find the biggest of given three values using function and use it to find total marks of the student\n", "# Variable declaration\n", "\n", "t1 = 62\n", "t2 = 70\n", "t3 = 58\n", "a1 = 17\n", "a2 = 21\n", "a3 = 23\n", "\n", "print \"Enter three test scores :\",t1,t2,t3\n", "print \"Enter three assignment scores :\",a1,a2,a3\n", "\n", "# function to find biggest of three values\n", "\n", "def big(a,b,c):\n", " if a > b:\n", " if a > c:\n", " return a\n", " else:\n", " return c\n", " elif b > c:\n", " return b\n", " else:\n", " return c\n", "\n", "total = big(t1,t2,t3) + big(a1,a2,a3)\n", "\n", "print \"Total marks =\",total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three test scores : 62 70 58\n", "Enter three assignment scores : 17 21 23\n", "Total marks = 93\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3 , Page number: CP-176" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to create a function to compute the cos(x) series upto 15 terms\n", "# Variable declaration\n", "\n", "x = 0\n", "\n", "print \"----------------------------------\"\n", "print \" x in degrees cos(x) \"\n", "print \"----------------------------------\"\n", "\n", "# function for computing cosine function\n", "\n", "def cosine(x):\n", " s = 0\n", " i = 0\n", " term = 1\n", " x = x * (3.14) / 180\n", " k = 1\n", " # loop to find summation of 15 terms\n", " while k <= 15:\n", " s = s + term\n", " term = term * x * x * (-1) / ((i + 1) * (i + 2))\n", " i = i + 2\n", " k = k + 1\n", " return s\n", "\n", "\n", "# calling function\n", "\n", "while x <= 180:\n", " print \" %d \" % x,\" %0.2f \"%cosine(x)\n", " x = x + 30\n", "\n", "print \"-----------------------------------\" " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "----------------------------------\n", " x in degrees cos(x) \n", "----------------------------------\n", " 0 1.00 \n", " 30 0.87 \n", " 60 0.50 \n", " 90 0.00 \n", " 120 -0.50 \n", " 150 -0.87 \n", " 180 -1.00 \n", "-----------------------------------\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4 , Page number: CP-177" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find GCD of two integers\n", "# Variable declaration\n", "\n", "a = 45\n", "b = 27\n", "c = 81\n", "\n", "print \"Enter three integers :\",a,b,c\n", "\n", "def gcd(x,y):\n", " if x >= y:\n", " nr = x\n", " dr = y\n", " else:\n", " nr = y\n", " dr = x\n", " r = nr % dr\n", " while r != 0:\n", " nr = dr\n", " dr = r\n", " r = nr % dr\n", "\n", " return dr\n", "\n", "d1 = gcd(a,b)\n", "d2 = gcd(a,c)\n", "d3 = gcd(b,c)\n", "\n", "if d1 == d2:\n", " if d1 == d3:\n", " print \"Greatest common divisor is\",d1\n", " else:\n", " print \"Greatest common divisor is \",gcd(d1,d3)\n", "else:\n", " print \"Greatest common divisr is\",gcd(d1,d2)\n", "\n", "\n", "print \"Press any key to continue. . .\" " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three integers : 45 27 81\n", "Greatest common divisor is 9\n", "Press any key to continue. . .\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5 , Page number: CP-178" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to reverse the given integer\n", "# Variable declaration\n", "\n", "n = 2846\n", "\n", "print \"Enter the integer :\",n\n", "\n", "# function to reverse an integer\n", "\n", "def reverse(n):\n", " rn = 0\n", " while n > 0:\n", " r = n % 10\n", " rn = rn * 10 + r\n", " n = n /10\n", " return rn\n", "\n", "\n", "# call the function to print the reverse integer\n", "\n", "print n,\"is reversed as \",reverse(n)\n", "print \"Press any key to continue. . .\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the integer : 2846\n", "2846 is reversed as 6482\n", "Press any key to continue. . .\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6 , Page number: CP-179" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to compare two strings S1 and S2 and return the result 0,1,-1\n", "# Variable declaration\n", "\n", "s1 = \"MUMBAI\"\n", "s2 = \"MYSORE\"\n", "\n", "print \"Enter the first string :\",s1\n", "print \"Enter the second string :\",s2\n", "\n", "\n", "# function to compare the strings\n", "\n", "def compare(s1,s2):\n", " if cmp(s1,s2) == 0:\n", " return 0\n", " if cmp(s1,s2) > 0:\n", " return 1\n", " if cmp(s1,s2) < 0:\n", " return -1\n", "\n", "# call the function to print the result\n", "\n", "print \"The result is \", compare(s1,s2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the first string : MUMBAI\n", "Enter the second string : MYSORE\n", "The result is -1\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7 , Page number: CP-181" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find the arithmetic mean of n values using a function\n", "# Variable declaration\n", "\n", "n = 6\n", "x = [3.1,3.8,3.6,4.0,3.4,3.8]\n", "\n", "print \"How many values ?\",n\n", "print \"Enter all values\"\n", "for i in x:\n", " print i,\n", "print \n", "# function to find arithmetic mean\n", "\n", "def amean(x,n):\n", " s = 0\n", " for i in range(n):\n", " s = s + x[i]\n", " return (s/n)\n", "\n", "# call function to print arithmetic mean\n", "\n", "print \"Arithmetic mean = %0.2f\" % amean(x,n)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many values ? 6\n", "Enter all values\n", "3.1 3.8 3.6 4.0 3.4 3.8\n", "Arithmetic mean = 3.62\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8 , Page number: CP-182" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to read a matrix of order m x n and print the sum of all elements using functions\n", "# Variable declaration\n", "\n", "m = 3\n", "n = 2\n", "a = [[2,3,4],\n", " [2,0,1]]\n", "\n", "\n", "print \"How many rows and columns :\",m,n\n", "print \"Enter the matrix values\"\n", "for i in range(n):\n", " for j in range(m):\n", " print a[i][j],\n", " print \n", "\n", "# function to add all elements in the matrix\n", "def elem_sum(a,m,n):\n", " i = 0\n", " j = 0\n", " s = 0\n", " for i in range(n):\n", " for j in range(m):\n", " s = s + a[i][j]\n", " j = j + 1\n", " i = i + 1 \n", " return s\n", "\n", "# call function to print the result\n", "\n", "print \"Sum of all elements in the matrix =\",\n", "print elem_sum(a,m,n)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many rows and columns : 3 2\n", "Enter the matrix values\n", "2 3 4\n", "2 0 1\n", "Sum of all elements in the matrix = 12\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9 , Page number: CP-183" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to reverse a string\n", "# Variable declaration\n", "\n", "st = \"NEW DELHI\"\n", "print \"Enter a string :\",st\n", "def reverse(st):\n", " rst = \"\"\n", " for i in range(0 ,len(st)):\n", " rst += st[(len(st) -1) - i]\n", " return rst\n", "\n", "print st,\"is reversed as \",\n", "print reverse(st)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a string : NEW DELHI\n", "NEW DELHI is reversed as IHLED WEN\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10 , Page number: CP-184" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to apply binary search to set of N numbers using a function\n", "# Variable declaration\n", "\n", "n = 10\n", "x = [-3,8,13,19,21,25,26,29,35,42]\n", "s = 19\n", "\n", "\n", "print \"How many numbers ?\",n\n", "print \"Enter all numbers in the list\"\n", "for a in x:\n", " print a,\n", "print \n", "print \"Enter the number to be searched :\",s\n", "\n", "# function to search the number\n", "\n", "def bi_search(x,n,s):\n", " flag = 0\n", " start = 0\n", " end = n\n", " while start < end and flag ==0:\n", " mid = (start + end) / 2\n", " if x[mid] > s:\n", " end = mid\n", " elif x[mid] < s:\n", " start = mid + 1\n", " else:\n", " flag = 1\n", " return flag\n", "\n", "# calling the function\n", "\n", "if bi_search(x,n,s):\n", " print \"The number\",s,\"is present in the list\"\n", "else:\n", " print \"The number\",s,\"is not present in list\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many numbers ? 10\n", "Enter all numbers in the list\n", "-3 8 13 19 21 25 26 29 35 42\n", "Enter the number to be searched : 19\n", "The number 19 is present in the list\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11 , Page number: CP-187" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find factorial of a given number using recursive function\n", "# Variable declaration\n", "\n", "n = 5\n", "r = 3\n", "\n", "print \"Enter the values to n and r :\",n,r\n", "\n", "# recursive function to find factorial\n", "\n", "def fact(k):\n", " if k ==1:\n", " return 1\n", " else:\n", " return (k * fact(k-1))\n", "\n", "ncr = fact(n)/(fact(r) * fact(n-r))\n", "print \"Value of nCr =\",ncr " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the values to n and r : 5 3\n", "Value of nCr = 10\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 12 , Page number: CP-188" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to compute the value of x power n using a recursive function\n", "# Variable declaration\n", "\n", "x = 4.20\n", "n = 3\n", "\n", "print \"Enter value to x :\",x\n", "print \"Enter its power :\",n\n", "\n", "# recursive function to find x rise to n\n", "\n", "def power(x,n):\n", " if n == 1:\n", " return x\n", " else:\n", " return (x * power(x,n-1))\n", "\n", "\n", "print \"%0.2f\" %x,\"raise to \",n,\"is %0.2f\" % power(x,n) " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter value to x : 4.2\n", "Enter its power : 3\n", "4.20 raise to 3 is 74.09\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13 , Page number: CP-189" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to display first n terms of the fibonacci series using recursive function\n", "\n", "# Variable declaration\n", "\n", "n = 10\n", "t1 = 0\n", "t2 = 1\n", "i = 0\n", "count = 2\n", "\n", "# recursive function to print the terms in series\n", "def fibo(t1,t2):\n", " global count\n", " if (count >= n):\n", " return\n", " else:\n", " t3 = t1 + t2\n", " print t3,\n", " count = count + 1\n", " t1 = t2\n", " t2 = t3\n", " return fibo(t1,t2)\n", " \n", "\n", "\n", "print \"How many terms to be printed ?\",n\n", "print \"The first\",n,\"terms in Fibonacci series are\"\n", "print t1,\n", "print t2,\n", "\n", "fibo(t1,t2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many terms to be printed ? 10\n", "The first 10 terms in Fibonacci series are\n", "0 1 1 2 3 5 8 13 21 34\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14 , Page number: CP-196" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to add two matrices of order m x n and to print the resultant values\n", "# Variable declaration\n", "\n", "m = 2\n", "n = 2\n", "a = [[2,-2],\n", " [0,4]]\n", "b = [[6,2],\n", " [4,-5]]\n", "c = [[0,0],\n", " [0,0]]\n", "\n", "print \"How many rows and columns ?\",m,n\n", "print \"Enter A matrix value\"\n", "for i in range(m):\n", " for j in range(n):\n", " print a[i][j],\n", " print \n", "print \"Enter B matrix values\"\n", "for i in range(m):\n", " for j in range(n):\n", " print b[i][j],\n", " print \n", "\n", "# function to add matrices\n", "def matadd():\n", " for i in range(m):\n", " for j in range(n):\n", " c[i][j] = a[i][j] + b[i][j]\n", "\n", "\n", "\n", "# call function\n", "matadd()\n", "print \"Resultant matrix is\"\n", "for i in range(m):\n", " for j in range(n):\n", " print c[i][j],\n", " print \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many rows and columns ? 2 2\n", "Enter A matrix value\n", "2 -2\n", "0 4\n", "Enter B matrix values\n", "6 2\n", "4 -5\n", "Resultant matrix is\n", "8 0\n", "4 -1\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 15 , Page number: CP-197" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to multiply A matrix of order of m x n with B matrix of order n x l and print the resultant matrix\n", "# Variable declaration\n", "\n", "a = [[2,-2],\n", " [0,4]]\n", "b = [[6,2,],\n", " [4,-5]]\n", "c = [[0,0],\n", " [0,0]]\n", "\n", "m = 2\n", "n = 2\n", "l = 2\n", "\n", "print \"Enter order of A matrix (m x n) :\",m,n\n", "print \"Enter A matrix values\"\n", "for i in range(m):\n", " for j in range(n):\n", " print a[i][j],\n", " print\n", " \n", "print \"Enter order of B matrix (n x l) :\",n,l\n", "print \"Enter B matrix values\"\n", "for i in range(m):\n", " for j in range(n):\n", " print b[i][j],\n", " print \n", " \n", "\n", "# function to multiply matrices\n", "\n", "def matmul():\n", " for i in range(m):\n", " for j in range(l):\n", " c[i][j] = 0\n", " for k in range(n):\n", " c[i][j] = c[i][j] + a[i][k] * b[k][j]\n", " return c\n", "\n", "# call function\n", "matmul()\n", "print \"Resultant matix is\"\n", "for i in range(m):\n", " for j in range(n):\n", " print c[i][j],\n", " print\n", "\n", "\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter order of A matrix (m x n) : 2 2\n", "Enter A matrix values\n", "2 -2\n", "0 4\n", "Enter order of B matrix (n x l) : 2 2\n", "Enter B matrix values\n", "6 2\n", "4 -5\n", "Resultant matix is\n", "4 14\n", "16 -20\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 16 , Page number: CP-199" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to transpose a matrix of order m x n\n", "# Variable declaration\n", "\n", "a = [[-3,6,0],\n", " [3,2,8]]\n", "at = [[0,0],\n", " [0,0],\n", " [0,0]]\n", "m = 2\n", "n = 3\n", "ch = \"y\"\n", "\n", "print \"How many rows and columns ?\",m,n\n", "print \"Enter the matrix values\"\n", "for i in range(m):\n", " for j in range(n):\n", " print a[i][j],\n", " print \n", " \n", "\n", "# function to transpose a matrix\n", "\n", "def transpose(a,at,m,n):\n", " for i in range(m):\n", " for j in range(n):\n", " at[j][i] = a[i][j]\n", " return at\n", "\n", "while ch == 'y' or ch == 'Y' :\n", " # call function to transpose the matrix\n", " transpose(a,at,m,n)\n", " print \"Transpose of the matrix is\"\n", " for i in range(n):\n", " for j in range(m):\n", " print at[i][j],\n", " print\n", " ch = \"N\"\n", " print \"Press y to continue\"\n", " print \" any other key to stop.\",ch" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many rows and columns ? 2 3\n", "Enter the matrix values\n", "-3 6 0\n", "3 2 8\n", "Transpose of the matrix is\n", "-3 3\n", "6 2\n", "0 8\n", "Press y to continue\n", " any other key to stop. N\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 17 , Page number: CP-200" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to create a function to sort the elements of an array in ascending order\n", "# Variable declaration\n", "\n", "n = 4\n", "x = [32,-10,20,5]\n", "\n", "print \"How many numbers ?\",n\n", "print \"Enter the list of values\"\n", "for a in x:\n", " print a,\n", "print \n", "\n", "# function to sort the numbers\n", "def sort(x,n):\n", " for i in range(n-1):\n", " for j in range(i+1,n):\n", " if x[i] > x[j]:\n", " temp = x[i]\n", " x[i] = x[j]\n", " x[j] = temp\n", " return x\n", "\n", "# call function to sort the numbers\n", "sort(x,n)\n", "print \"The sorted list is\"\n", "for a in x:\n", " print a,\n", "print\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many numbers ? 4\n", "Enter the list of values\n", "32 -10 20 5\n", "The sorted list is\n", "-10 5 20 32\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }