{ "metadata": { "name": "", "signature": "sha256:ce6594f5391167e50e92652dc99b5d59ded886787329ad2d8f4011e41b7e20c7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 5: Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example main.c, page no. 157" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Welcome to functions\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Welcome to functions\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example prtmsg2.c, page no. 158" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def PrintMesg():\n", " print \"PrintMesg, Welcome to functions\"\n", "print \"Main, Welcome to functions\"\n", "PrintMesg()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Main, Welcome to functions\n", "PrintMesg, Welcome to functions\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.1, page no. 160" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def cube(i):\n", " return i*i*i\n", "print \"The ccube of 10 is\", cube(10)\n", "input_int = int(raw_input(\"Input an integer: \"))\n", "print \"The cube of \", input_int, \"is \", cube(input_int)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The ccube of 10 is 1000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Input an integer: 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The cube of 20 is 8000\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.2, page no. 161" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#there is no concept of function declaration in Python, program will remain same as above\n", "def cube(i):\n", " return i*i*i\n", "print \"The cube of 10 is\", cube(10)\n", "input_int = int(raw_input(\"Input an integer: \"))\n", "print \"The cube of \", input_int, \"is \", cube(input_int)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The cube of 10 is 1000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Input an integer: 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The cube of 20 is 8000\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.3, page no. 163" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#\\a for the beed would not work in Python\n", "\n", "def cube(i):\n", " return i*i*i\n", "print \"The cube of 10 is\", cube(10)\n", "input_int = int(raw_input(\"Input an integer: \"))\n", "if input_int < 1:\n", " print \"Input is invalid!\"\n", "else:\n", " print \"The cube of \", input_int, \"is \", cube(input_int)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The cube of 10 is 1000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Input an integer: -9\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Input is invalid!\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.4, page no. 164" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#there is no concept of function declaration in Python. So, the program will remain same as above\n", "\n", "def cube(i):\n", " return i*i*i\n", "print \"The cube of 10 is\", cube(10)\n", "input_int = int(raw_input(\"Input an integer: \"))\n", "if input_int < 1:\n", " print \"Input is invalid!\"\n", "else:\n", " print \"The cube of \", input_int, \"is \", cube(input_int)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The cube of 10 is 1000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Input an integer: 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The cube of 20 is 8000\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7, page no. 165" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def maxfunc(i, j , k):\n", " if i >= j and i >= k:\n", " max = i\n", " elif j >=k :\n", " max = j\n", " else:\n", " max = k\n", " return max\n", "print \"Input 3 numbers\"\n", "a = int(raw_input(\"a: \"))\n", "b = int(raw_input(\"b: \"))\n", "c = int(raw_input(\"c: \"))\n", "m = maxfunc(a, b, c)\n", "print \"The maximum is \", m" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input 3 numbers\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "a: 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "b: 6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "c: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The maximum is 6\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example funded.c, page no. 168" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def max(a, b):\n", " return a if a > b else b\n", "print \"Enter 2 numbers\"\n", "i = int(raw_input(\"i: \"))\n", "j = int(raw_input(\"j: \"))\n", "iMax = max(i,j)\n", "print \"The maximum of %d and %d is %d\" %(i, j, iMax)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter 2 numbers\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i: 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j: 7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The maximum of 5 and 7 is 7\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example square1.c, page no. 169" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def SquareInt(x):\n", " return x**2\n", "Sq = SquareInt(32)\n", "print \"The square of 32 is\", Sq" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The square of 32 is 1024\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.5, page no. 170" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def Min(m, n):\n", " if m > n:\n", " MinNum = n\n", " else:\n", " MinNum = m\n", " return MinNum\n", "print \"Enter 2 numbers\"\n", "a = int(raw_input(\"a: \"))\n", "b = int(raw_input(\"b: \"))\n", "Small = Min(a,b)\n", "print \"The minimum of 2 numbers is \", Small\n", "print \"The minimum of 2 numbers is \", Min(a,b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter 2 numbers\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "a: 32567\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "b: 32657\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The minimum of 2 numbers is 32567\n", "The minimum of 2 numbers is 32567\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example pval.c, page no. 171" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def ModifierFunc(Num):\n", " print \"In function, the value of Num is %d\" %Num\n", " Num = 19\n", " print \"In function, after changing, value of Num is: %d\" %Num\n", "Num = 100\n", "print \"In Main, the value of num is %d\" %Num\n", "ModifierFunc(Num)\n", "print \"After calling function, the value of Num is %d\" %Num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In Main, the value of num is 100\n", "In function, the value of Num is 100\n", "In function, after changing, value of Num is: 19\n", "After calling function, the value of Num is 100\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example swap1.c, page no. 172" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def swap(a, b):\n", " temp = a\n", " a = b\n", " b = temp\n", "print \"Input 2 integers\"\n", "i = int(raw_input(\"i: \"))\n", "j = int(raw_input(\"j: \"))\n", "print \"Before Swapping: \", i, j\n", "swap(i, j)\n", "print \"After Swapping: \", i , j" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input 2 integers\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i: 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Before Swapping: 3 5\n", "After Swapping: 3 5\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example swap2.c, page no. 173" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def swap(l):\n", " temp = l[0]\n", " l[0] = l[1]\n", " l[1] = temp\n", " return l\n", "print \"Input 2 integers\"\n", "i = int(raw_input(\"i: \"))\n", "j = int(raw_input(\"j: \"))\n", "print \"Before Swapping: \", i, j\n", "a = [i, j]\n", "l = swap(a)\n", "print \"After Swapping: \", l[0] , l[1]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input 2 integers\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i: 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Before Swapping: 3 5\n", "After Swapping: 5 3\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.6, page no. 174" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def fact(n):\n", " if n == 0:\n", " lfact = 1\n", " else:\n", " lfact = 1\n", " for i in range(2, n+1):\n", " lfact = lfact * i\n", " return lfact\n", "num = int(raw_input(\"Enter a positive number: \"))\n", "factorial = fact(num)\n", "print \"The factorial of %d is %d\" %(num, factorial)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive number: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The factorial of 5 is 120\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.7, page no. 174" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def SumTwo(n1, n2):\n", " return n1+n2\n", "def SumThree(n1, n2, n3):\n", " return n1+n2+n3\n", "i = 56\n", "j = 32\n", "SumTwo(3, 4)\n", "SumTwo(i, j), SumThree(100, i, j)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "(88, 188)" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.8, page no. 175" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#mistake in the book, it should print both 0 & 1 at the start instead of just 0\n", "\n", "def Fib(TotNum):\n", " fLast = 1\n", " sLast = 0\n", " print \"Fibonacci numbers: \"\n", " print \"0 1\",\n", " while(TotNum > 2):\n", " CurNum = fLast + sLast\n", " print CurNum,\n", " sLast = fLast\n", " fLast = CurNum\n", " TotNum -= 1\n", "print \"Program to generate fibonacci numbers\"\n", "print \"Enter the total numbers to be generated: \",\n", "n = int(raw_input())\n", "Fib(n)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Program to generate fibonacci numbers\n", "Enter the total numbers to be generated: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Fibonacci numbers: \n", "0 1 1 2 3\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.9, page no. 176" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def Sum20():\n", " sum = 0\n", " for i in range(1, 21):\n", " sum += i\n", " return sum\n", "print \"Program to print the sum of first 20 numbers\"\n", "print \"The sum of first 20 number is \", Sum20()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Program to print the sum of first 20 numbers\n", "The sum of first 20 number is 210\n" ] } ], "prompt_number": 29 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.10, page no. 176" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def isPalindrome(string):\n", " len1 = len(string)\n", " if len1 == 0:\n", " return True\n", " if string==string[::-1]:\n", " return True\n", " return False\n", "\n", "print \"Program to test the given string is a palindrome\"\n", "string = raw_input(\"Enter a string : \")\n", "if isPalindrome(string):\n", " print \"The given string %s is a palindrome\"%string\n", "else:\n", " print \"The given string %s is not a palindrome\"%string" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Program to test the given string is a palindrome\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string : ratsdrowninwordstar\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The given string ratsdrowninwordstar is a palindrome\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example ifact.c, page no. 178" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def calc_fact(num):\n", " fact = 1\n", " for i in range(num, 1, -1):\n", " fact *= i\n", " return fact\n", "n = int(raw_input(\"Enter an integer: \"))\n", "print \"The factorial of %d is %d\" %(n, calc_fact(n))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter an integer: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The factorial of 5 is 120\n" ] } ], "prompt_number": 31 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.11, page no. 179" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#output will differ as there is no concept of static in Python\n", "i = 1\n", "def CountNumber(n):\n", " global i\n", " print \"In the function the value of n is %d\" %n\n", " print \"The depth of the call is: %d\" %i\n", " i +=1\n", " if n > 1:\n", " CountNumber(n-1)\n", " print \"After recurssive the value of i is %d\" %i\n", "num = 3\n", "CountNumber(num)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In the function the value of n is 3\n", "The depth of the call is: 1\n", "In the function the value of n is 2\n", "The depth of the call is: 2\n", "In the function the value of n is 1\n", "The depth of the call is: 3\n", "After recurssive the value of i is 4\n", "After recurssive the value of i is 4\n", "After recurssive the value of i is 4\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example fibrec1.c, page no. 180" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def uFibr(uNum):\n", " if uNum == 0:\n", " return 0\n", " elif uNum == 1:\n", " return 1\n", " else:\n", " return uFibr(uNum-1)+uFibr(uNum-2)\n", "uTnum = int(raw_input(\"Enter how many number are to be generated: \"))\n", "print \"The Fibonacci sequences are: \"\n", "for uIndex in range(uTnum):\n", " print \"%u\" %uFibr(uIndex)," ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter how many number are to be generated: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The Fibonacci sequences are: \n", "0 1 1 2 3\n" ] } ], "prompt_number": 38 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example fiborec2.c, page no. 181" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "f1 = 0\n", "f2 = 1\n", "def Fib(n):\n", " global f1,f2\n", " if n < 2:\n", " f1 = 0\n", " f2 = 1\n", " else:\n", " Fib(n-1)\n", " temp = f2\n", " f2 = f1+f2\n", " f1 = temp\n", "print \"Number of terms to be generated ?\",\n", "n = int(raw_input())\n", "print \"Fibonacci sequence up to %d terms: \" %n,Fib(n)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Number of terms to be generated ?" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Fibonacci sequence up to 10 terms: 0 1 1 2 3 5 8 13 21 34 None\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Eample 5.13, page no. 184" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def gcd(p, q):\n", " remainder = p-(p/q*q)\n", " if remainder == 0:\n", " return q\n", " else:\n", " return gcd(q, remainder)\n", "print \"Type in any 2 nubers whose GCD is to be found: \"\n", "a = int(raw_input(\"a: \"))\n", "b = int(raw_input(\"b: \"))\n", "iGcd = gcd(a, b)\n", "print iGcd\n", "print \"GCD of %d and %d is %d\" %(a, b, iGcd)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type in any 2 nubers whose GCD is to be found: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "a: 13\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "b: 45\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "GCD of 13 and 45 is 1\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.14, page no. 185" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def hanoi(n, sndl, indl, dndl):\n", " if n != 0:\n", " hanoi(n-1, sndl, dndl, indl)\n", " print \"Move disk %d from %c to %c\" %(n, sndl, dndl)\n", " hanoi(n-1, indl, sndl, dndl)\n", "snvalue = 'L'\n", "invalue = 'C'\n", "dnvalue = 'R'\n", "print \"Enter the number of disks: \",\n", "nvalue = int(raw_input())\n", "print \"Tower of Hanoi problem with %d disks\" %nvalue\n", "hanoi(nvalue, snvalue, invalue, dnvalue)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of disks: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Tower of Hanoi problem with 3 disks\n", "Move disk 1 from L to R\n", "Move disk 2 from L to C\n", "Move disk 1 from R to C\n", "Move disk 3 from L to R\n", "Move disk 1 from C to L\n", "Move disk 2 from C to R\n", "Move disk 1 from L to R\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.15, page nol 187" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mid = 0\n", "def binSrch(b, key, low, high):\n", " global mid\n", " if low <= high:\n", " mid = int((low+high)/2)\n", " if key < b[mid]:\n", " high = mid - 1\n", " return binSrch(b, key, low, high)\n", " elif key > b[mid]:\n", " low = mid + 1\n", " return binSrch(b, key, low, high)\n", " elif b[mid] == key:\n", " return mid+1\n", " else:\n", " return 0\n", "n = int(raw_input(\"Enter the size of array: \"))\n", "print \"Array elements (ascending order) ?\"\n", "a = []\n", "for i in range(n):\n", " a.append(int(raw_input()))\n", "print \"Element to be searched ?\",\n", "key = int(raw_input())\n", "position = binSrch(a, key, 0, n-1)\n", "if position == 0 :\n", " print \"Unsuccessful search, %d not found\" %key\n", "else:\n", " print \"Successful search\"\n", " print \"%d found at position %d\" %(key, position)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the size of array: 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Array elements (ascending order) ?\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "67\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "89\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "99\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "100\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Element to be searched ?" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "99\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Successful search\n", "99 found at position 5\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.16, page no. 188" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def my_max(j, n, a):\n", " if n == 0:\n", " return a[j]\n", " else:\n", " m1 = my_max(j, n/2, a)\n", " m2 = my_max(j + n/2 + 1, n/2, a)\n", " if m1 < m2:\n", " return m2\n", " else:\n", " return m1\n", "def my_min(j, n, a):\n", " if n == 0:\n", " return a[j]\n", " else:\n", " m1 = my_min(j, n/2, a)\n", " m2 = my_min(j + n/2 + 1, n/2, a)\n", " if m1 < m2:\n", " return m1\n", " else:\n", " return m2\n", "print \"Finding the maximum and minimum recursively\"\n", "print \"Number of elements in vector ?\"\n", "n = int(raw_input())\n", "print \"Vector elements ?\"\n", "x = []\n", "for i in range(n):\n", " x.append(int(raw_input()))\n", "i = 0\n", "m = n - 1\n", "maxim = my_max(i, m, x)\n", "print \"Maximum of %d elements is %d\" %(n, maxim)\n", "i = 0\n", "m = n - 1\n", "minim = my_min(i,m,x)\n", "print \"Minimum of %d elements is %d\" %(n, minim)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Finding the maximum and minimum recursively\n", "Number of elements in vector ?\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Vector elements ?\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "34\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "56\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "78\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "54\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Maximum of 8 elements is 89\n", "0 7 [12, 34, 56, 78, 54, -12, 0, 89]\n", "Minimum of 8 elements is -12\n" ] } ], "prompt_number": 37 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.17, page no. 189" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Command line arguments cannot be done using IPython notebook. Hence, skipping the example" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 38 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.18, page no. 190" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Command line arguments" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }