{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7: Recursion" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.1, Page number: 219" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration, calculation and result\n", "str = raw_input('Enter a string: ')\n", "\n", "print str" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: HELLO\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "HELLO\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.2, Page number: 221" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration, calculation and result\n", "str = raw_input('Enter a string: ')\n", "\n", "print str[::-1]" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: HELLO\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "OLLEH\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.3, Page number: 224" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def fact (n) :\n", " if n>0 :\n", " return n * fact (n-1)\n", " else :\n", " return 1\n", "\n", "print ('Factorial of %d is %d' % (n, fact (n)))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Factorial of 5 is 120\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.4, Page number: 227" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def printnum (n) :\n", " if n>0 :\n", " printnum (n-1)\n", " print n,\n", "\n", "print ('Counting up to %d' % n)\n", "printnum (n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Counting up to 10\n", "1 2 3 4 5 6 7 8 9 10\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.5, Page number: 229" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def printeven (n) :\n", "\tif not hasattr(printeven, \"x\") : \n", "\t\tprinteven.x = 2\n", " \n", "\tif printeven.x <= n :\n", "\t\tprint printeven.x,\n", "\t\tprinteven.x = printeven.x + 2\n", "\t\tprinteven (n)\n", "\n", "print ('Even numbers up to %d are' % n)\n", "printeven (n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Even numbers up to 10 are\n", "2 4 6 8 10\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.6, Page number: 231" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def printodd (x, n) :\n", "\tif x <= n : \n", "\t\tprint x,\n", "\t\tprintodd (x+2, n)\n", "\n", "print ('Odd numbers up to %d are' % n)\n", "printodd (1, n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Odd numbers up to 10 are\n", "1 3 5 7 9\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.7, Page number: 232" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def printnum (fro, to) :\n", "\tif fro <= to : \n", "\t\tprint fro,\n", "\t\tprintnum (fro+1, to)\n", "\n", "print ('Number line for %d is' % n)\n", "printnum (-n, n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Number line for 5 is\n", "-5 -4 -3 -2 -1 0 1 2 3 4 5\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.8, Page number: 234" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def summate (n) :\n", "\tif n > 0 : \n", "\t\treturn n + summate (n-1)\n", "\telse :\n", "\t\treturn 0\n", "\n", "print ('Sum of first %d natural numbers is %d' % (n, summate (n)))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum of first 10 natural numbers is 55\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.9, Page number: 237" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def factors (n) :\n", "\tif not hasattr(factors, \"i\") : \n", "\t\tfactors.i = 1\n", "\t\t\n", "\tif factors.i <= n : \n", "\t\tif n % factors.i == 0 :\n", "\t\t\tprint factors.i,\n", "\t\tfactors.i = factors.i + 1\n", "\t\tfactors (n)\n", "\n", "print ('The factors of %d are' % n)\n", "factors (n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 28\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The factors of 28 are\n", "1 2 4 7 14 28\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.10, Page number: 239" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def multiple (n) :\n", "\tif not hasattr(multiple, \"i\") : \n", "\t\tmultiple.i = 1\n", "\t\t\n", "\tif multiple.i <= 10 : \n", "\t\tprint ('%d X %d = %d' % (n, multiple.i, n*multiple.i))\n", "\t\tmultiple.i = multiple.i + 1\n", "\t\tmultiple (n)\n", "\n", "print ('The first 10 multiples of %d are' % n)\n", "multiple (n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The first 10 multiples of 6 are\n", "6 X 1 = 6\n", "6 X 2 = 12\n", "6 X 3 = 18\n", "6 X 4 = 24\n", "6 X 5 = 30\n", "6 X 6 = 36\n", "6 X 7 = 42\n", "6 X 8 = 48\n", "6 X 9 = 54\n", "6 X 10 = 60\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.11, Page number: 241" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def checkprime (n) :\n", " flag = 1\n", " i = 2\n", " while i=10 :\n", " sum = 0\n", " while n>0 :\n", " sum = sum + n%10\n", " n = n/10\n", " n = sum\n", " return sum\n", "\n", "result = sumofdigit (n, sum)\n", "\n", "print ('Sum of digits of %d is %d' % (n, result))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 8626\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum of digits of 8626 is 4\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.14, Page number: 245" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def prime (n) :\n", " for num in range (3, n+1) :\n", " flag = 1\n", " for i in range (2, num) :\n", " if num % i == 0 : \n", " flag = 0\n", " if flag :\n", " print ('%d ' % num),\n", "\n", "print ('Prime numbers upto %d are' % n)\n", "prime (n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 30\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Prime numbers upto 30 are\n", "3 5 7 11 13 17 19 23 29 \n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.15, Page number: 246" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int (raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def pattern (n) :\n", "\tif n > 0 :\n", "\t\tpattern (n-1)\n", "\t\tfor count in range (1, n+1) :\n", "\t\t\tprint ('%d' % count),\n", " \tprint\n", " \t\n", "pattern (n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1\n", "1 2\n", "1 2 3\n", "1 2 3 4\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.16, Page number: 249" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def printline (x, total) :\n", "\tif x > 0 :\n", "\t\tprintline (x-1, total)\n", "\t\tfor blk in range (1, total-x+1) :\n", "\t\t\tprint ' ',\n", "\t\tfor up in range (1, x+1) :\n", "\t\t\tprint ('%d' % up),\n", "\t\tdown = x-1\n", "\t\twhile down>0 :\n", "\t\t\tprint ('%d' % down),\n", "\t\t\tdown = down-1\n", "\t\tprint\n", "\n", "printline (n, n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 1\n", " 1 2 1\n", " 1 2 3 2 1\n", "1 2 3 4 3 2 1\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.17, Page number: 253" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Function declaration, calculation and result\n", "def listsum () :\n", "\tn = int(raw_input('Enter any number '))\n", "\tif n > 0 :\n", "\t\treturn n + listsum ()\n", "\telse :\n", "\t\treturn 0\n", "\t\t\n", "sum = listsum ()\n", "print ('Sum of list of numbers is %d' % sum)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number -4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum of list of numbers is 6\n" ] } ], "prompt_number": 18 } ], "metadata": {} } ] }