diff options
Diffstat (limited to 'Magnifying_C/Chapter_8.ipynb')
-rwxr-xr-x | Magnifying_C/Chapter_8.ipynb | 2718 |
1 files changed, 0 insertions, 2718 deletions
diff --git a/Magnifying_C/Chapter_8.ipynb b/Magnifying_C/Chapter_8.ipynb deleted file mode 100755 index ccafa905..00000000 --- a/Magnifying_C/Chapter_8.ipynb +++ /dev/null @@ -1,2718 +0,0 @@ -{ - "metadata": { - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 8: Arrays" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.1, Page number: 256" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Variable declaration\n", - "word = array('c', ['H', 'e', 'l', 'l', 'o'])\n", - "\n", - "# Result\n", - "print ('The contents of word [] is %s ' % ''.join(word))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The contents of word [] is Hello \n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.2, Page number: 259" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Variable declaration\n", - "marks = [0]*10\n", - "avg = 0\n", - "\n", - "# Calculation and result\n", - "for i in range (0, 10) :\n", - "\tmarks[i] = int(raw_input('Enter value in cell [%d] ' % i))\n", - "\tavg = avg + marks[i]\n", - "\n", - "avg = avg/10\n", - "print ('The average marks are %f' % avg)\n", - "\n", - "for i in range (0, 10) :\n", - "\tif marks[i] < avg :\n", - "\t\tprint ('Marks in cell [%d] are %d less than average' % (i, marks[i]))\n", - "\telse :\n", - "\t\tprint ('Marks in cell [%d] are %d above/equal to average' % (i, marks[i]))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [0] 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [1] 78\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [2] 65\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [3] 43\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [4] 56\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [5] 98\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [6] 53\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [7] 82\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [8] 74\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [9] 39\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The average marks are 63.000000\n", - "Marks in cell [0] are 45 less than average\n", - "Marks in cell [1] are 78 above/equal to average\n", - "Marks in cell [2] are 65 above/equal to average\n", - "Marks in cell [3] are 43 less than average\n", - "Marks in cell [4] are 56 less than average\n", - "Marks in cell [5] are 98 above/equal to average\n", - "Marks in cell [6] are 53 less than average\n", - "Marks in cell [7] are 82 above/equal to average\n", - "Marks in cell [8] are 74 above/equal to average\n", - "Marks in cell [9] are 39 less than average\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.3, Page number: 261" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Variable declaration\n", - "data = raw_input('Enter the string ')\n", - "\n", - "# Calculation and result\n", - "rev_data = reversed (data)\n", - "\n", - "if list (data) == list (rev_data) :\n", - "\tprint ('The string %s is a PALINDROME' % data)\n", - "else :\n", - "\tprint ('The string %s is not a PALINDROME' % data)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the string MADAM\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The string MADAM is a PALINDROME\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.4, Page number: 266" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Variable declaration\n", - "marks = [0]*10\n", - "avg = 0\n", - "\n", - "# Function declaration, calculation and result\n", - "def readdata (marks) :\n", - "\tfor i in range (0, 10) :\n", - "\t\tmarks[i] = int(raw_input('Enter value in cell [%d] ' % i))\n", - "\t\t\n", - "def average (marks) :\n", - "\tmean = 0\n", - "\tfor i in range (0, 10) :\n", - "\t\tmean = mean + marks[i]\n", - "\treturn mean/10\n", - "\n", - "def printdata (marks, avg) :\n", - "\tfor i in range (0, 10) :\n", - "\t\tif marks[i] < avg :\n", - "\t\t\tprint ('Marks in cell [%d] are %d less than average' % (i, marks[i]))\n", - "\t\telse :\n", - "\t\t\tprint ('Marks in cell [%d] are %d above/equal to average' % (i, marks[i]))\t\n", - "\n", - "\n", - "readdata (marks)\n", - "avg = average (marks)\t\n", - "print ('The average marks are %f' % avg)\n", - "printdata (marks, avg)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [0] 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [1] 78\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [2] 65\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [3] 43\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [4] 56\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [5] 98\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [6] 53\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [7] 82\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [8] 74\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter value in cell [9] 39\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The average marks are 63.000000\n", - "Marks in cell [0] are 45 less than average\n", - "Marks in cell [1] are 78 above/equal to average\n", - "Marks in cell [2] are 65 above/equal to average\n", - "Marks in cell [3] are 43 less than average\n", - "Marks in cell [4] are 56 less than average\n", - "Marks in cell [5] are 98 above/equal to average\n", - "Marks in cell [6] are 53 less than average\n", - "Marks in cell [7] are 82 above/equal to average\n", - "Marks in cell [8] are 74 above/equal to average\n", - "Marks in cell [9] are 39 less than average\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.5, Page number: 273" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Variable declaration\n", - "a = [10, 20, 30, 40, 50]\n", - "\n", - "# Function declaration, calculation and result\n", - "def fun (x, y, z) :\n", - "\tx = x + 5\n", - "\ty = y + 10\n", - "\tz[4] = z[4] + 100\n", - "\n", - "\n", - "for i in range (0, 5) :\n", - "\tprint ('The cell [%d] contains %d' % (i, a[i]))\n", - "\tprint ('The address of cell [%d] is %x' % (i, id(a[i])))\n", - "\t\t\n", - "fun (a[0], a[3], a)\t\n", - "print\n", - "\n", - "for i in range (0, 5) :\n", - "\tprint ('The cell [%d] contains %d' % (i, a[i]))\n", - "\tprint ('The address of cell [%d] is %x' % (i, id(a[i])))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The cell [0] contains 10\n", - "The address of cell [0] is 18bc0c0\n", - "The cell [1] contains 20\n", - "The address of cell [1] is 18bbfd0\n", - "The cell [2] contains 30\n", - "The address of cell [2] is 18bbee0\n", - "The cell [3] contains 40\n", - "The address of cell [3] is 18bc5b8\n", - "The cell [4] contains 50\n", - "The address of cell [4] is 18bc4c8\n", - "\n", - "The cell [0] contains 10\n", - "The address of cell [0] is 18bc0c0\n", - "The cell [1] contains 20\n", - "The address of cell [1] is 18bbfd0\n", - "The cell [2] contains 30\n", - "The address of cell [2] is 18bbee0\n", - "The cell [3] contains 40\n", - "The address of cell [3] is 18bc5b8\n", - "The cell [4] contains 150\n", - "The address of cell [4] is 18bcaf8\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.6, Page number: 275" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Variable declaration\n", - "data = raw_input('Enter the string ')\n", - "\n", - "# Function declaration, calculation and result\n", - "def revstring (data) :\n", - "\trev_data = reversed (data)\n", - "\n", - "\tif list (data) == list (rev_data) :\n", - "\t\tprint ('The string %s is a PALINDROME' % data)\n", - "\telse :\n", - "\t\tprint ('The string %s is not a PALINDROME' % data)\n", - "\n", - "revstring (data)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the string python\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The string python is not a PALINDROME\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.7, Page number: 277" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Variable declaration\n", - "a = [0]*10\n", - "\n", - "# Function declaration, calculation and result\n", - "def readnsort (a) :\n", - "\tpos = 0\n", - "\tfor len in range (0, 10) :\n", - "\t\tnum = int(raw_input('Enter data element no. %d ' % len))\n", - "\t\t\n", - "\t\tfor pos in range (0, len+1) :\n", - "\t\t\tif num < a[pos] :\n", - "\t\t\t\tbreak\n", - "\t\t\t\t\n", - "\t\ti = len\t\t\n", - "\t\twhile i>=pos :\n", - "\t\t\ta[i] = a[i-1]\n", - "\t\t\ti = i-1\n", - "\t\ta[pos] = num\n", - "\t\t\t\n", - "def printdata (a) :\n", - "\tfor i in range (0, 10) :\n", - "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\t\t\t\n", - "\t\t\t\n", - "readnsort (a)\n", - "print\n", - "printdata (a)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 0 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 1 78\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 2 65\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 3 43\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 4 56\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 5 98\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 6 53\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 7 82\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 8 74\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data element no. 9 39\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The data in cell [0] is 39 \n", - "The data in cell [1] is 43 \n", - "The data in cell [2] is 45 \n", - "The data in cell [3] is 53 \n", - "The data in cell [4] is 56 \n", - "The data in cell [5] is 65 \n", - "The data in cell [6] is 74 \n", - "The data in cell [7] is 78 \n", - "The data in cell [8] is 82 \n", - "The data in cell [9] is 98 \n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.8, Page number: 279" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Function declaration, calculation and result\n", - "def readdata () :\n", - "\ta = []\n", - "\tfor i in range (0, 10) :\n", - "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n", - "\treturn a\n", - "\n", - "def sortdata (a) :\n", - "\tfor p in range (0, 9) :\n", - "\t\tmin = p\n", - "\t\tfor i in range (p, 10) :\n", - "\t\t\tif a[min] > a[i] :\n", - "\t\t\t\tmin = i\n", - "\t\ttemp = a[p]\n", - "\t\ta[p] = a[min]\n", - "\t\ta[min] = temp\n", - "\treturn a\n", - "\n", - "def printdata (a) :\n", - "\tfor i in range (0, 10) :\n", - "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n", - "\n", - "a = readdata ()\n", - "a = sortdata (a)\n", - "print ('\\nThe sorted data is ')\n", - "printdata (a)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [0] 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [1] 78\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [2] 65\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [3] 43\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [4] 56\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [5] 98\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [6] 53\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [7] 82\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [8] 74\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [9] 39\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The sorted data is \n", - "The data in cell [0] is 39 \n", - "The data in cell [1] is 43 \n", - "The data in cell [2] is 45 \n", - "The data in cell [3] is 53 \n", - "The data in cell [4] is 56 \n", - "The data in cell [5] is 65 \n", - "The data in cell [6] is 74 \n", - "The data in cell [7] is 78 \n", - "The data in cell [8] is 82 \n", - "The data in cell [9] is 98 \n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.9, Page number: 282" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Function declaration, calculation and result\n", - "def readdata () :\n", - "\ta = []\n", - "\tfor i in range (0, 10) :\n", - "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n", - "\treturn a\n", - "\n", - "def bubble (a) :\n", - "\tfor p in range (0, 10) :\n", - "\t\tfor i in range (0, 9-p) :\n", - "\t\t\tif a[i] > a[i+1] :\n", - "\t\t\t\ttemp = a[i]\n", - "\t\t\t\ta[i] = a[i+1]\n", - "\t\t\t\ta[i+1] = temp\n", - "\treturn a\n", - "\n", - "def printdata (a) :\n", - "\tfor i in range (0, 10) :\n", - "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n", - "\n", - "a = readdata ()\n", - "a = bubble (a)\n", - "print ('\\nThe sorted data is ')\n", - "printdata (a)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [0] 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [1] 78\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [2] 65\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [3] 43\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [4] 56\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [5] 98\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [6] 53\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [7] 82\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [8] 74\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [9] 39\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The sorted data is \n", - "The data in cell [0] is 39 \n", - "The data in cell [1] is 43 \n", - "The data in cell [2] is 45 \n", - "The data in cell [3] is 53 \n", - "The data in cell [4] is 56 \n", - "The data in cell [5] is 65 \n", - "The data in cell [6] is 74 \n", - "The data in cell [7] is 78 \n", - "The data in cell [8] is 82 \n", - "The data in cell [9] is 98 \n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.10, Page number: 285" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Function declaration, calculation and result\n", - "def readdata (size) :\n", - "\ta = []\n", - "\tfor i in range (0, size) :\n", - "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n", - "\treturn a\n", - "\n", - "def bubble (a, size) :\n", - "\tfor p in range (0, size) :\n", - "\t\tfor i in range (0, size-p-1) :\n", - "\t\t\tif a[i] > a[i+1] :\n", - "\t\t\t\ttemp = a[i]\n", - "\t\t\t\ta[i] = a[i+1]\n", - "\t\t\t\ta[i+1] = temp\n", - "\treturn a\n", - "\n", - "def printdata (a, size) :\n", - "\tfor i in range (0, size) :\n", - "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n", - "\n", - "def lsearch (a, key, size) :\n", - "\tfor i in range (0, size) :\n", - "\t\tif a[i] == key :\n", - "\t\t\treturn i\n", - "\t\tif i == size :\n", - "\t\t\treturn -1\n", - "\t\t\t\n", - "size = int(raw_input('Enter the total number of elements '))\n", - "a = readdata (size)\n", - "a = bubble (a, size)\n", - "print ('\\nThe sorted array is ')\n", - "printdata (a, size)\n", - "\n", - "find = int(raw_input('\\nEnter the element to be searched '))\n", - "pos = lsearch (a, find, size)\n", - "\n", - "if pos<0 :\n", - "\tprint ('\\nThe element %d NOT FOUND ' % find)\n", - "else :\n", - "\tprint ('\\nThe element %d found at position %d ' % (find, pos))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the total number of elements 5\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [0] 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [1] 78\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [2] 65\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [3] 34\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [4] 49\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The sorted array is \n", - "The data in cell [0] is 34 \n", - "The data in cell [1] is 45 \n", - "The data in cell [2] is 49 \n", - "The data in cell [3] is 65 \n", - "The data in cell [4] is 78 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter the element to be searched 65\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The element 65 found at position 3 \n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.11, Page number: 288" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Function declaration, calculation and result\n", - "def readdata (size) :\n", - "\ta = []\n", - "\tfor i in range (0, size) :\n", - "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n", - "\treturn a\n", - "\n", - "def bubble (a, size) :\n", - "\tfor p in range (0, size) :\n", - "\t\tfor i in range (0, size-p-1) :\n", - "\t\t\tif a[i] > a[i+1] :\n", - "\t\t\t\ttemp = a[i]\n", - "\t\t\t\ta[i] = a[i+1]\n", - "\t\t\t\ta[i+1] = temp\n", - "\treturn a\n", - "\n", - "def printdata (a, size) :\n", - "\tfor i in range (0, size) :\n", - "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n", - "\n", - "def bsearch (a, key, size) :\n", - "\tfirst = 0\n", - "\tlast = size - 1\n", - "\tmid = (first + last)/2\n", - "\t\n", - "\twhile (first <= last and a[mid] != key) :\n", - "\t\tif a[mid] < key :\n", - "\t\t\tfirst = mid + 1\n", - "\t\telse :\n", - "\t\t\tif a[mid] > key :\n", - "\t\t\t\tlast = mid - 1\n", - "\t\tmid = (first + last)/2\n", - "\t\n", - "\tif a[mid] == key :\n", - "\t\treturn mid\n", - "\telse :\n", - "\t\treturn -1\n", - "\t\t\t\n", - "size = int(raw_input('Enter the number of elements in the array '))\n", - "a = readdata (size)\n", - "a = bubble (a, size)\n", - "print ('\\nThe sorted array is ')\n", - "printdata (a, size)\n", - "\n", - "find = int(raw_input('\\nPlease enter the element to be searched for '))\n", - "pos = bsearch (a, find, size)\n", - "\n", - "if pos<0 :\n", - "\tprint ('\\nThe element %d NOT FOUND ' % find)\n", - "else :\n", - "\tprint ('\\nThe element %d found at position %d ' % (find, pos))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the number of elements in the array 5\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [0] 45\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [1] 78\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [2] 65\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [3] 34\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter data in cell [4] 49\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The sorted array is \n", - "The data in cell [0] is 34 \n", - "The data in cell [1] is 45 \n", - "The data in cell [2] is 49 \n", - "The data in cell [3] is 65 \n", - "The data in cell [4] is 78 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Please enter the element to be searched for 45\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The element 45 found at position 1 \n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.12, Page number: 293" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "from array import *\n", - "\n", - "# Function declaration, calculation and result\n", - "def readncompute (no_of_students) :\n", - "\ta = [[0 for y in xrange(5)] for x in xrange(no_of_students)]\n", - "\tper = [0 for x in range(no_of_students)]\n", - "\tavg = 0\n", - "\t\n", - "\tfor i in range (0, no_of_students) :\n", - "\t\tprint ('\\nEnter marks of 5 subjects for Roll no. %d ' % (i+1))\n", - "\t\t\n", - "\t\tfor j in range (0, 5) :\n", - "\t\t\ta[i][j] = int(raw_input('Subject %d = ' % (j+1)))\n", - "\t\t\tper[i] = per[i] + a[i][j]\n", - "\t\t\t\n", - "\t\tper[i] = per[i]/5\n", - "\t\tavg = avg + per[i]\n", - "\t\t\n", - "\treturn a, avg/no_of_students, per\n", - "\n", - "def printdata (a, no_of_students, avg, per) :\n", - "\tfor i in range (0, no_of_students) :\n", - "\t\tprint ('\\nMarks for Roll no. %d are ' % (i+1))\n", - "\t\tfor j in range (0, 5) :\n", - "\t\t\tprint a[i][j]\n", - "\t\tprint ('Percentage for Roll no. %d is %f ' % ((i+1), per[i]))\n", - "\t\t\n", - "\t\tif per[i] < avg :\n", - "\t\t\tprint ('Below average')\n", - "\t\telse :\n", - "\t\t\tprint ('Above average')\n", - "\t\t\n", - "\n", - "no_of_students = int(raw_input('How many students marks do you want to enter '))\n", - "a, average, per = readncompute (no_of_students)\n", - "print ('\\nThe average marks are %f ' % average)\n", - "printdata (a, no_of_students, average, per)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many students marks do you want to enter 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter marks of 5 subjects for Roll no. 1 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 1 = 75\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 2 = 43\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 3 = 51\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 4 = 68\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 5 = 73\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter marks of 5 subjects for Roll no. 2 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 1 = 49\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 2 = 71\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 3 = 62\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 4 = 53\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 5 = 50\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter marks of 5 subjects for Roll no. 3 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 1 = 90\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 2 = 82\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 3 = 74\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 4 = 93\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Subject 5 = 89\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The average marks are 68.000000 \n", - "\n", - "Marks for Roll no. 1 are \n", - "75\n", - "43\n", - "51\n", - "68\n", - "73\n", - "Percentage for Roll no. 1 is 62.000000 \n", - "Below average\n", - "\n", - "Marks for Roll no. 2 are \n", - "49\n", - "71\n", - "62\n", - "53\n", - "50\n", - "Percentage for Roll no. 2 is 57.000000 \n", - "Below average\n", - "\n", - "Marks for Roll no. 3 are \n", - "90\n", - "82\n", - "74\n", - "93\n", - "89\n", - "Percentage for Roll no. 3 is 85.000000 \n", - "Above average\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.13, Page number: 296" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Function declaration, calculation and result\n", - "def getchoice () :\n", - "\tprint\n", - "\tprint ('1 - Input two matrices ')\n", - "\tprint ('2 - Add and display sum ')\n", - "\tprint ('3 - Exit ')\n", - "\t\n", - "\tchoice = int(raw_input('Enter choice : '))\n", - "\treturn choice\n", - "\n", - "def read (matrix, rows, cols) :\t\n", - "\tfor i in range (0, rows) :\n", - "\t\tprint ('\\nEnter elements for row %d ' % (i+1))\t\t\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n", - "\n", - "def add (sum, augend, addend, rows, cols) :\n", - "\tfor i in range (0, rows) :\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tsum[i][j] = augend[i][j] + addend[i][j]\n", - "\n", - "def display2matrices (augend, addend, rows, cols) :\n", - "\tprint ('\\nMatrix A ')\n", - "\tfor i in range (0, rows) :\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tprint ('%3d ' % augend[i][j]),\n", - "\t\tprint\n", - "\tprint ('\\nMatrix B ')\n", - "\tfor i in range (0, rows) :\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tprint ('%3d ' % addend[i][j]),\n", - "\t\tprint\n", - "\n", - "def display3matrices (sum, augend, addend, rows, cols) :\n", - "\tprint\n", - "\tfor i in range (0, rows) :\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tprint ('%3d ' % augend[i][j]),\n", - "\t\tprint ('%s ' % ('+' if i==rows/2 else ' ')),\n", - "\t\t\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tprint ('%2d ' % addend[i][j]),\n", - "\t\tprint ('%s ' % ('=' if i==rows/2 else ' ')),\n", - "\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tprint ('%2d ' % sum[i][j]),\n", - "\t\tprint\n", - "\n", - "def display (matrix, rows, cols) :\n", - "\tfor i in range (0, rows) :\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tprint ('%3d ' % matrix[i][j]),\n", - "\t\tprint\n", - "\n", - "augend = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "addend = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "sum = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "\n", - "condition = True\n", - "while condition :\n", - "\tchoice = getchoice ()\n", - "\t\n", - "\tif choice == 1 :\n", - "\t\trows = int(raw_input('\\nEnter the number of rows '))\t\t\n", - "\t\tcols = int(raw_input('Enter the number of columns '))\n", - "\t\t\n", - "\t\tprint ('\\n\\nEnter the first matrix ')\n", - "\t\tread (augend, rows, cols)\n", - "\t\tprint ('\\n\\nEnter the second matrix ')\n", - "\t\tread (addend, rows, cols)\n", - "\t\tprint ('\\n\\nYou entered ')\n", - "\t\tdisplay2matrices (augend, addend, rows, cols)\n", - "\t\n", - "\telif choice == 2 :\n", - "\t\tadd (sum, augend, addend, rows, cols)\n", - "\t\tdisplay3matrices (sum, augend, addend, rows, cols)\n", - "\t\n", - "\telif choice == 3 :\n", - "\t\tcondition = False\n", - "\t\texit ()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "1 - Input two matrices \n", - "2 - Add and display sum \n", - "3 - Exit \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter choice : 1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter the number of rows 3\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the number of columns 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "\n", - "Enter the first matrix \n", - "\n", - "Enter elements for row 1 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][1] = 1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][2] = 2\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][3] = 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter elements for row 2 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][1] = 3\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][2] = 4\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][3] = 5\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter elements for row 3 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][1] = 5\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][2] = 6\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][3] = 7\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "\n", - "Enter the second matrix \n", - "\n", - "Enter elements for row 1 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][1] = 3\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][2] = 5\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][3] = 1\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter elements for row 2 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][1] = 6\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][2] = 5\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][3] = 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter elements for row 3 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][1] = 8\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][2] = 7\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][3] = 4\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "\n", - "You entered \n", - "\n", - "Matrix A \n", - " 1 2 3 \n", - " 3 4 5 \n", - " 5 6 7 \n", - "\n", - "Matrix B \n", - " 3 5 1 \n", - " 6 5 3 \n", - " 8 7 4 \n", - "\n", - "1 - Input two matrices \n", - "2 - Add and display sum \n", - "3 - Exit \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter choice : 2\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - " 1 2 3 3 5 1 4 7 4 \n", - " 3 4 5 + 6 5 3 = 9 9 8 \n", - " 5 6 7 8 7 4 13 13 11 \n", - "\n", - "1 - Input two matrices \n", - "2 - Add and display sum \n", - "3 - Exit \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter choice : 3\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.14, Page number: 300" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Function declaration, calculation and result\n", - "def readmatrix (matrix, rows, cols) :\t\n", - "\tfor i in range (0, rows) :\n", - "\t\tprint ('\\nPlease enter elements for row %d ' % (i+1))\t\t\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n", - "\n", - "def multiply (prod, a, b, Ar, Ac, Bc) :\n", - "\tfor i in range (0, Ar) :\n", - "\t\tfor j in range (0, Bc) :\n", - "\t\t\tprod[i][j] = 0\n", - "\t\t\tfor k in range (0, Ac) :\n", - "\t\t\t\tprod[i][j] += a[i][k] * b[k][j]\n", - "\n", - "def display (matrix, rows, cols) :\n", - "\tfor i in range (0, rows) :\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tprint ('%3d ' % matrix[i][j]),\n", - "\t\tprint\n", - "\n", - "a = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "b = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "product = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "\n", - "condition = True\n", - "while condition :\n", - "\tArows, Acols = map(int, raw_input('Enter order of matrix A: ').split())\n", - "\tBrows, Bcols = map(int, raw_input('Enter order of matrix B: ').split())\n", - "\n", - "\tif Acols == Brows :\n", - "\t\tcondition = False\n", - "\t\tbreak\n", - "\telse : \n", - "\t\tprint ('\\nMatrices are not compatible for multiplication. Enter the order again. ')\n", - "\t\tcontinue\n", - "\t\n", - "print ('\\nEnter matrix A: ')\n", - "readmatrix (a, Arows, Acols)\n", - "\t\n", - "print ('\\nEnter matrix B: ')\n", - "readmatrix (b, Brows, Bcols)\n", - "\n", - "multiply (product, a, b, Arows, Acols, Bcols)\n", - "print ('\\nThe resultant product (A.B) is: ')\n", - "display (product, Arows, Bcols)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter order of matrix A: 2 3\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter order of matrix B: 3 2\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter matrix A: \n", - "\n", - "Please enter elements for row 1 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][1] = 1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][2] = 2\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][3] = 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Please enter elements for row 2 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][1] = 4\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][2] = 5\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][3] = 6\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter matrix B: \n", - "\n", - "Please enter elements for row 1 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][1] = 6\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][2] = 5\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Please enter elements for row 2 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][1] = 4\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][2] = 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Please enter elements for row 3 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][1] = 2\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][2] = 1\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The resultant product (A.B) is: \n", - " 20 14 \n", - " 56 41 \n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.15, Page number: 303" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Function declaration, calculation and result\n", - "def getchoice () :\n", - "\tprint\n", - "\tprint ('1 - Input square matrix ')\n", - "\tprint ('2 - Display upper triangular matrix ')\n", - "\tprint ('3 - Display lower triangular matrix ')\n", - "\tprint ('4 - Exit ')\n", - "\t\n", - "\tcondition = True\n", - "\twhile condition :\n", - "\t\tchoice = int(raw_input('Enter choice : '))\n", - "\t\t\n", - "\t\tif choice in range (1, 5) :\n", - "\t\t\tcondition = False\n", - "\t\t\t\n", - "\treturn choice\n", - "\n", - "def displayupper (matrix, order) :\n", - "\tprint\n", - "\tfor i in range (0, order) :\n", - "\t\tfor j in range (0, order) :\n", - "\t\t\tif j >= i :\n", - "\t\t\t\tprint ('%3d ' % matrix[i][j]),\n", - "\t\t\telse :\n", - "\t\t\t\tprint ('%3d ' % 0),\n", - "\t\tprint\n", - "\t\t\n", - "def displaylower (matrix, order) :\n", - "\tprint\n", - "\tfor i in range (0, order) :\n", - "\t\tfor j in range (0, order) :\n", - "\t\t\tif j <= i :\n", - "\t\t\t\tprint ('%3d ' % matrix[i][j]),\n", - "\t\t\telse :\n", - "\t\t\t\tprint ('%3d ' % 0),\n", - "\t\tprint\n", - "\n", - "def read (matrix, rows, cols) :\t\n", - "\tfor i in range (0, rows) :\n", - "\t\tprint ('\\nEnter %d elements for row %d ' % (rows, (i+1)))\t\t\n", - "\t\tfor j in range (0, cols) :\n", - "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n", - "\n", - "matrix = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "\n", - "condition = True\n", - "while condition :\n", - "\tchoice = getchoice ()\n", - "\t\n", - "\tif choice == 1 :\n", - "\t\torder = int(raw_input('\\nEnter order of square matrix '))\t\t\n", - "\t\tread (matrix, order, order)\n", - "\t\n", - "\telif choice == 2 :\n", - "\t\tdisplayupper (matrix, order)\n", - "\t\n", - "\telif choice == 3 :\n", - "\t\tdisplaylower (matrix, order)\n", - "\t\t\n", - "\telif choice == 4 :\n", - "\t\tcondition = False\n", - "\t\texit ()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "1 - Input square matrix \n", - "2 - Display upper triangular matrix \n", - "3 - Display lower triangular matrix \n", - "4 - Exit \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter choice : 1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter order of square matrix 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter 3 elements for row 1 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][1] = 1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][2] = 2\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][3] = 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter 3 elements for row 2 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][1] = 4\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][2] = 5\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][3] = 6\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter 3 elements for row 3 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][1] = 7\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][2] = 8\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][3] = 9\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "1 - Input square matrix \n", - "2 - Display upper triangular matrix \n", - "3 - Display lower triangular matrix \n", - "4 - Exit \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter choice : 2\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - " 1 2 3 \n", - " 0 5 6 \n", - " 0 0 9 \n", - "\n", - "1 - Input square matrix \n", - "2 - Display upper triangular matrix \n", - "3 - Display lower triangular matrix \n", - "4 - Exit \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter choice : 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - " 1 0 0 \n", - " 4 5 0 \n", - " 7 8 9 \n", - "\n", - "1 - Input square matrix \n", - "2 - Display upper triangular matrix \n", - "3 - Display lower triangular matrix \n", - "4 - Exit \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter choice : 4\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.16, Page number: 307" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Function declaration, calculation and result\n", - "def readmatrix (matrix, n) :\n", - "\tfor i in range (0, n) :\n", - "\t\tprint ('\\nEnter elements for row no %d ' % (i+1))\n", - "\t\tfor j in range (0, n) :\n", - "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n", - "\n", - "def display (matrix, n) :\n", - "\tprint\n", - "\tfor i in range (0, n) :\n", - "\t\tfor j in range (0, n) :\n", - "\t\t\tprint ('%5d ' % matrix[i][j]),\n", - "\t\tprint\n", - "\t\t\n", - "def isunitmatrix(matrix, n) :\n", - "\tfor i in range (0, n) :\n", - "\t\tif matrix[i][i] != 1 :\n", - "\t\t\treturn 0\n", - "\t\n", - "\tfor i in range (0, n) :\n", - "\t\tfor j in range (0, n) :\n", - "\t\t\tif i != j :\n", - "\t\t\t\tif matrix[i][j] != 0 :\n", - "\t\t\t\t\treturn 0\n", - "\treturn 1\n", - "\n", - "matrix = [[0 for y in xrange(10)] for x in xrange(10)]\n", - "n = int(raw_input('Enter order of matrix '))\n", - "readmatrix (matrix, n)\n", - "display (matrix, n)\n", - "\n", - "if (isunitmatrix (matrix, n)) :\n", - "\tprint \"UNIT MATRIX\"\n", - "else :\n", - "\tprint \"NOT A UNIT MATRIX\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter order of matrix 3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter elements for row no 1 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][1] = 1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][2] = 0\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[1][3] = 0\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter elements for row no 2 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][1] = 0\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][2] = 1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[2][3] = 0\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Enter elements for row no 3 \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][1] = 0\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][2] = 0\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "matrix[3][3] = 1\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - " 1 0 0 \n", - " 0 1 0 \n", - " 0 0 1 \n", - "UNIT MATRIX\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.17, Page number: 311" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Variable declaration\n", - "string = raw_input('Enter a string: ')\n", - "\n", - "# Calculation and result\n", - "swap_string = string.swapcase()\n", - "\n", - "print swap_string" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a string: hello world\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "HELLO WORLD\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.18, Page number: 312" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Variable declaration\n", - "string = raw_input('Enter a string ')\n", - "\n", - "# Function declaration, calculation and result\n", - "def printstr (string) :\n", - "\tfor i in range (0, len(string)) :\n", - "\t\tfor j in range (0, i+1) :\n", - "\t\t\tprint string[j],\n", - "\t\tprint\n", - "\t\n", - "\ti = i - 2\n", - "\twhile i >= 0 :\n", - "\t\tfor j in range (0, i+1) :\n", - "\t\t\tprint string[j],\n", - "\t\tprint\n", - "\t\ti = i - 1\n", - "\t\t\n", - "printstr (string)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a string hello\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "h\n", - "h e\n", - "h e l\n", - "h e l l\n", - "h e l l o\n", - "h e l\n", - "h e\n", - "h\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.19, Page number: 314" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Variable declaration\n", - "a = raw_input('Enter any string ')\n", - "\n", - "# Function declaration, calculation and result\n", - "def copy (a) :\n", - "\tb = a\n", - "\treturn b\n", - "\t\n", - "b = copy (a)\n", - "print\n", - "print ('The first string is %s ' % a)\n", - "print ('The second string is %s ' % b)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter any string hello\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "The first string is hello \n", - "The second string is hello \n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8.20, Page number: 315" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Variable declaration\n", - "a = raw_input('Enter the major string: ')\n", - "b = raw_input('Enter the minor string: ')\n", - "\n", - "# Function declaration, calculation and result\n", - "def ispart (a, b) :\n", - "\tif b in a :\n", - "\t\tprint ('The string \"%s\" is A SUBSTRING of \"%s\" ' % (b, a))\n", - "\telse :\n", - "\t\tprint ('The string \"%s\" is NOT A SUBSTRING of \"%s\" ' % (b, a))\n", - "\n", - "ispart (a, b)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the major string: python is a programming language\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the minor string: python\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The string \"python\" is A SUBSTRING of \"python is a programming language\" \n" - ] - } - ], - "prompt_number": 10 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file |