{
 "metadata": {
  "name": "",
  "signature": "sha256:ea85b38c0942e3b3d3a1243525badd8ffa28ac492392c6aef6c8ea5823294150"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 10 : Recursion"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example: 1, Page No.: 5.67"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Factorial of an integer number.\n",
      "\n",
      "def rec(x):\n",
      "    f=0\n",
      "    if x==1:\n",
      "        return (1)\n",
      "    else:\n",
      "        f=x*rec(x-1)\n",
      "        return(f)\n",
      "\n",
      "a=input(\"Enter the number:\")\n",
      "print \"The Factorial value of %d:\"%a,rec(a)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number:5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The Factorial value of 5: 120\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example: 2, Page No.:5.67"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Program to read characters and print reversely using recursion.\n",
      "\n",
      "def reverse(c):\n",
      "    if len(c)<=0:\n",
      "        return c\n",
      "    return reverse(c[1:])+c[0]\n",
      "\n",
      "c=raw_input(\"Enter the Line of Text...\")\n",
      "print reverse(c)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Line of Text...munilak\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "kalinum\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example: 3, Page No.:5.68"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#concept of recursion\n",
      "\n",
      "def add(pk, pm):\n",
      "    if pm==0:\n",
      "        return pk\n",
      "    else:\n",
      "        return(1+add(pk,pm-1))\n",
      "    \n",
      "    \n",
      "k=70\n",
      "m=20\n",
      "\n",
      "i=add(k,m)\n",
      "\n",
      "print \"The Value of addition is %d\"%i\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The Value of addition is 90\n"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example: 4, Page No:5.70"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Tower of honoi\n",
      "n=int(raw_input(\"How many Disks ...\\n\"))\n",
      "\n",
      "def honoi(n,fro,to,t):\n",
      "    if(n>0):\n",
      "        honoi(n-1,fro,t,to)\n",
      "        print \"Move disk %d from %c to %c\\n\"%(n,fro,to)\n",
      "        honoi(n-1,t,to,fro)\n",
      "        \n",
      "fro='L'\n",
      "to='R'\n",
      "t='C'\n",
      "        \n",
      "honoi(n,fro,to,t)\n",
      "            "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "How many Disks ...\n",
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Move disk 1 from L to R\n",
        "\n",
        "Move disk 2 from L to C\n",
        "\n",
        "Move disk 1 from R to C\n",
        "\n",
        "Move disk 3 from L to R\n",
        "\n",
        "Move disk 1 from C to L\n",
        "\n",
        "Move disk 2 from C to R\n",
        "\n",
        "Move disk 1 from L to R\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Case Study: 1, Page No. : 5.76"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#user defined function that computes 'x' raised to the power 'y'\n",
      "\n",
      "import math \n",
      "\n",
      "x,y=input(\"Enter the Values of X and Y \")\n",
      "\n",
      "print \"%d to the power of %d is ... \"%(x,y),math.pow(x,y)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Values of X and Y 10,2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10 to the power of 2 is ...  100.0\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Case Study: 2, page No.5.76"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#nesting of function\n",
      "\n",
      "def ratio(x,y,z):\n",
      "    \n",
      "    def diff(m,n):\n",
      "        if(m!=n):\n",
      "            return(1)\n",
      "        else:\n",
      "            return(0)\n",
      "    \n",
      "    diff(y,z)\n",
      "    if diff(y,z):\n",
      "       return(x/(y-z))\n",
      "    else:\n",
      "       return(0)\n",
      "        \n",
      "    \n",
      "x,y,z=input(\"Enter the values of X,Y,Z: \")\n",
      "\n",
      "    \n",
      "print \"Ratio is..%f\\n\"%ratio(x,y,z)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the values of X,Y,Z: 10,10,5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Ratio is..2.000000\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Case Study: 3, Page No. 5.78"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Demonstration of Global Variable\n",
      "\n",
      "a=39               #Global Variable\n",
      "print \"A... %d\\n\"%a\n",
      "\n",
      "def f1():\n",
      "    global a\n",
      "    a=a+10\n",
      "    return a\n",
      "\n",
      "def f2():\n",
      "    global a\n",
      "    a=a+20\n",
      "    return a\n",
      "\n",
      "def f3():\n",
      "    global a\n",
      "    a=a+30\n",
      "    return a\n",
      "\n",
      "\n",
      "\n",
      "print \"A...%d\\n\"%f1()\n",
      "print \"A...%d\\n\"%f2()\n",
      "print \"A...%d\\n\"%f3()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "A... 39\n",
        "\n",
        "A...49\n",
        "\n",
        "A...69\n",
        "\n",
        "A...99\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 39
    }
   ],
   "metadata": {}
  }
 ]
}