diff options
Diffstat (limited to 'Mastering_C')
-rw-r--r-- | Mastering_C/chapter1.ipynb | 170 | ||||
-rw-r--r-- | Mastering_C/chapter10.ipynb | 913 | ||||
-rw-r--r-- | Mastering_C/chapter11.ipynb | 1993 | ||||
-rw-r--r-- | Mastering_C/chapter12.ipynb | 4076 | ||||
-rw-r--r-- | Mastering_C/chapter15.ipynb | 99 | ||||
-rw-r--r-- | Mastering_C/chapter16.ipynb | 116 | ||||
-rw-r--r-- | Mastering_C/chapter2.ipynb | 981 | ||||
-rw-r--r-- | Mastering_C/chapter3.ipynb | 584 | ||||
-rw-r--r-- | Mastering_C/chapter4.ipynb | 1567 | ||||
-rw-r--r-- | Mastering_C/chapter5.ipynb | 1515 | ||||
-rw-r--r-- | Mastering_C/chapter6.ipynb | 603 | ||||
-rw-r--r-- | Mastering_C/chapter7.ipynb | 2482 | ||||
-rw-r--r-- | Mastering_C/chapter8.ipynb | 2252 | ||||
-rw-r--r-- | Mastering_C/chapter9.ipynb | 1463 | ||||
-rw-r--r-- | Mastering_C/screenshots/interest.png | bin | 0 -> 69524 bytes | |||
-rw-r--r-- | Mastering_C/screenshots/matrix.png | bin | 0 -> 43479 bytes | |||
-rw-r--r-- | Mastering_C/screenshots/stack.png | bin | 0 -> 44229 bytes |
17 files changed, 18814 insertions, 0 deletions
diff --git a/Mastering_C/chapter1.ipynb b/Mastering_C/chapter1.ipynb new file mode 100644 index 00000000..b1a0ed0e --- /dev/null +++ b/Mastering_C/chapter1.ipynb @@ -0,0 +1,170 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:f3ff9d889b7b7951c347d942d053d9fde4d57e52c21d53b779e2930b738a9f69" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1: Introduction" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1, page no. 18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Hello World\"\n", + "print \"Bye.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello World\n", + "Bye\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2, page no. 24" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Print \"Hello World\"\n", + "#This program will give error" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (<ipython-input-2-9028d474ee90>, line 1)", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"<ipython-input-2-9028d474ee90>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m Print \"Hello World\"\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3, page no. 24" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Hello World\"\n", + "print \"Bye.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello World\n", + "Bye.\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4, page no. 24" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Hello World\";print \"Bye.\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello World\n", + "Bye.\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5, page no. 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"\\\n", + " Hello World\"\n", + "\n", + "print \"\\\n", + " Bye.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Hello World\n", + " Bye.\n" + ] + } + ], + "prompt_number": 6 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter10.ipynb b/Mastering_C/chapter10.ipynb new file mode 100644 index 00000000..f65d8afa --- /dev/null +++ b/Mastering_C/chapter10.ipynb @@ -0,0 +1,913 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:32b969a4e6c7c3e26cae81dfc07a38f907135ecba42af43e2cfc7a389c8e7205" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 10: Files" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1, page no. 372" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fp = open(\"outfile1.txt\", \"w\")\n", + "i = int(raw_input(\"Input an integer: \"))\n", + "fp.write(str(i))\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input an integer: 22\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2, page no. 372" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fp = open(\"outfile2.txt\", \"w\")\n", + "fp.write(\"string written to file\\n\")\n", + "fp.write(\"c\\n\")\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3, page no. 372" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fp = open(\"outfile1.txt\", \"r\")\n", + "l = fp.readline()\n", + "print \"The integer in outfile1.txt is \", l" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The integer in outfile1.txt is 22\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10.1, page no. 374" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fp = open(\"sturec\", \"w\")\n", + "print \"Enter student's name and mark: \"\n", + "print \"Use 'q' to stop entry\"\n", + "while True:\n", + " name = raw_input()\n", + " if name == 'q':\n", + " break\n", + " marks = raw_input()\n", + " fp.write(name+','+marks+'\\n')\n", + "fp.close()\n", + "fp = open(\"sturec\", \"r\")\n", + "print \"Name\\tMark\"\n", + "print \"------------\"\n", + "lines = fp.readlines()\n", + "for line in lines:\n", + " line = line.split(',')\n", + " print line[0], '\\t', line[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter student's name and mark: \n", + "Use 'q' to stop entry\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "vivek\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "rajkumar\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "98\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "anand\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "babu\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "teja\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name\tMark\n", + "------------\n", + "vivek \t87\n", + "\n", + "rajkumar \t98\n", + "\n", + "anand \t67\n", + "\n", + "babu \t45\n", + "\n", + "teja \t90\n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10.2, page no. 376" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fp = open(\"str.txt\", \"w\")\n", + "print \"Enter characters \"\n", + "print \"Use 'q' to stop entry\"\n", + "while True:\n", + " char = raw_input()\n", + " if char == 'q':\n", + " break\n", + " fp.write(char)\n", + "fp.close()\n", + "fp = open(\"str.txt\", \"r\")\n", + "lines = fp.readlines()\n", + "count = 1\n", + "string = ''\n", + "for line in lines:\n", + " print line,\n", + " string += line\n", + "print \"\\nNumber of characters in the file: \", len(string)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter characters \n", + "Use 'q' to stop entry\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "I\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "s\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "w\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "t\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "h\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "e\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "c\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "t\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "c\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "l\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "i\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "c\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "k\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "t\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "h\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "e\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "o\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "s\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "e\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + ".\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I saw the cat click the mouse. \n", + "Number of characters in the file: 30\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10.3, page no. 378" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fp = open(\"class.rec\", \"w\")\n", + "print \"Enter the register number, name and mark: \"\n", + "print \"Use 'q' to stop entry\"\n", + "while True:\n", + " reg_no = raw_input()\n", + " if reg_no == 'q':\n", + " break\n", + " name = raw_input()\n", + " marks = raw_input()\n", + " fp.write(reg_no+','+name+','+marks+'\\n')\n", + "fp.close()\n", + "fp = open(\"class.rec\", \"r\")\n", + "lines = fp.readlines()\n", + "for line in lines:\n", + " line = line.split(',')\n", + " print line[0], '\\t', line[1], '\\t\\t', line[2]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the register number, name and mark: \n", + "Use 'q' to stop entry\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4005\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "babu\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "85\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4007\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "rajkumar\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "98\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4012\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "raju\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4028\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "teja\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "93\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4029\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "vivek\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4005 \tbabu \t\t85\n", + "\n", + "4007 \trajkumar \t\t98\n", + "\n", + "4012 \traju \t\t77\n", + "\n", + "4028 \tteja \t\t93\n", + "\n", + "4029 \tvivek \t\t87\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10.4, page no. 381" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import os\n", + "import sys\n", + "fp = open(\"class.rec\", \"r\")\n", + "fp.seek(0, os.SEEK_END)\n", + "size = fp.tell()\n", + "print \"The file class.rec is of \", size, \" bytes\"\n", + "fp.seek(0)\n", + "lines = fp.readlines()\n", + "while True:\n", + " i = int(raw_input(\"Enter the record no. to read(0 to stop): \"))\n", + " if i == 0:\n", + " break\n", + " fp.seek(0)\n", + " counter = 0\n", + " for line in lines:\n", + " counter += 1\n", + " if counter == i:\n", + " print line.split(\",\")[0], line.split(\",\")[1], line.split(\",\")[2]\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The file class.rec is of 70 bytes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the record no. to read(0 to stop): 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4005 babu 85\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the record no. to read(0 to stop): 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4007 rajkumar 98\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the record no. to read(0 to stop): 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4012 raju 77\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the record no. to read(0 to stop): 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4028 teja 93\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the record no. to read(0 to stop): 0\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8, page no. 384" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#The example will remain same as above" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example fwrite.c, page no. 390" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#there is no concept of buffered write. We will do it normally\n", + "\n", + "fp = open(\"testbuf.txt\", \"w\")\n", + "fp.write(\"1. This is fwrite\\n\")\n", + "fp.write(\"2. this is write\\n\")\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 23 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#rest of the examples in the chapters deal with command line arguments which is not possible in IPython Notebook." + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 24 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter11.ipynb b/Mastering_C/chapter11.ipynb new file mode 100644 index 00000000..9b5d4e2e --- /dev/null +++ b/Mastering_C/chapter11.ipynb @@ -0,0 +1,1993 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:e3b54ab088a42a68b5666c432cb7bb7b24f6ac5896dfbd8ae6cf469e8bea26f9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11: Dynamic Memory Allocation\n" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1, page no. 407" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "g = 50\n", + "def func(p):\n", + " s = 200\n", + " i = 10\n", + " print i, s\n", + "a = 1\n", + "print a\n", + "print g\n", + "func(-16)\n", + "print \"over!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "50\n", + "10 200\n", + "over!\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2, page no. 409" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#there is no concept of malloc & free in Python\n", + "\n", + "print \"How many elements?: \",\n", + "n = int(raw_input())\n", + "ary = []\n", + "print \"Enter the elements: \"\n", + "for i in range(n):\n", + " ary.append(int(raw_input()))\n", + "print \"The entered array is: \"\n", + "for ele in ary:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many elements?: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the elements: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The entered array is: \n", + "1 2 3\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.1, page no. 412" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "marks = []\n", + "size = 3\n", + "n = 0\n", + "print \"Enter marks (-1 to stop: )\"\n", + "mark = int(raw_input())\n", + "while mark != -1:\n", + " if n >= size:\n", + " print \"realloc 3 more integers...\"\n", + " size += 3\n", + " marks.append(mark)\n", + " mark = int(raw_input())\n", + " n += 1\n", + "\n", + "for mark in marks:\n", + " print mark," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks (-1 to stop: )\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "70\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "realloc 3 more integers...\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "80\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "realloc 3 more integers...\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "12 50 60 70 80 90 100\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.2, page no. 413" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "n = int(raw_input(\"Rows: \"))\n", + "m = int(raw_input(\"Columns: \"))\n", + "mat = numpy.zeros((n, m))\n", + "print \"Input the elements: \"\n", + "for i in range(n):\n", + " for j in range(m):\n", + " mat[i][j] = int(raw_input())\n", + "print \"Input two rows to swap: \"\n", + "i = int(raw_input())\n", + "j = int(raw_input())\n", + "mat[i], mat[j] = mat[j], mat[i]\n", + "for i in range(n):\n", + " for j in range(m):\n", + " print mat[i][j],\n", + " print \"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rows: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Columns: 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the elements: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two rows to swap: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.0 4.0 6.0 8.0 10.0 \n", + "6.0 7.0 8.0 9.0 0.0 \n", + "2.0 4.0 6.0 8.0 10.0 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.3, page no. 415" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def get(x, r, c):\n", + " for i in range(r):\n", + " for j in range(c):\n", + " x[i][j] = int(raw_input())\n", + " return x\n", + "def display(x, r, c):\n", + " for i in range(r):\n", + " for j in range(c):\n", + " print x[i][j],\n", + " print \"\"\n", + "def mul(a, b, c, row, m, col):\n", + " for i in range(row):\n", + " for j in range(col):\n", + " c[i][j] = 0\n", + " for k in range(m):\n", + " c[i][j] = c[i][j] + a[i][k] * b[k][j]\n", + " return c\n", + "a = numpy.zeros((30, 30)) \n", + "b = numpy.zeros((30, 30)) \n", + "c = numpy.zeros((30, 30)) \n", + "print \"Size of matrix A: \"\n", + "r1 = int(raw_input(\"row: \"))\n", + "c1 = int(raw_input(\"col: \"))\n", + "print \"Size of matrix B: \"\n", + "r2 = int(raw_input(\"row: \"))\n", + "c2 = int(raw_input(\"col: \"))\n", + "if c1 != r2:\n", + " print \"Multiplication not possible.\"\n", + " print \"columns of matrix A are not equal\"\n", + " print \"to rows of Matrix B\"\n", + "else:\n", + " print \"Multiplication possible\"\n", + " print \"Matrix A(%d X %d) elements?\" %(r1, c1)\n", + " a = get(a, r1, c1)\n", + " print \"Matrix B(%d X %d) elements?\" %(r2, c2)\n", + " b = get(a, r1, c1)\n", + " c = mul(a, b, c, r1, c1, c2)\n", + " print \"Product matrix C(%d X %d): \" %(r1, c2)\n", + " display (c, r1, c2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of matrix A: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "col: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of matrix B: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "col: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Multiplication possible\n", + "Matrix A(3 X 4) elements?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Matrix B(4 X 3) elements?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Product matrix C(3 X 3): \n", + "38.0 44.0 50.0 \n", + "98.0 116.0 134.0 \n", + "158.0 188.0 218.0 \n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Exmaple 11.4, page no. 419" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "\n", + "class node:\n", + " def __init__(self, data=0, link=None):\n", + " self.data = data\n", + " self.link = link\n", + "\n", + "start = None\n", + " \n", + "def add_front(data):\n", + " global start\n", + " tmp = node(data)\n", + " if(start == None):\n", + " start = tmp\n", + " else:\n", + " tmp.link = start\n", + " start = tmp\n", + "\n", + "def del_pos(pos):\n", + " global start\n", + " if (pos == 0):\n", + " tmp = start\n", + " start = start.link\n", + " return\n", + " q = start\n", + " count = 1\n", + " while(q.link != None):\n", + " if(count == pos):\n", + " tmp = q.link\n", + " q.link = tmp.link\n", + " return\n", + " count += 1\n", + " q = q.link\n", + " print \"\\n\\nThere is no element at %d position\" %pos\n", + " \n", + "def del_elem(data):\n", + " global start\n", + " if (start.data == data):\n", + " tmp = start\n", + " start = start.link\n", + " return\n", + " q = start\n", + " while(q.link.link != None):\n", + " if(q.link.data == data):\n", + " tmp = q.link\n", + " q.link = tmp.link\n", + " return\n", + " q = q.link\n", + " if(q.link.data == data):\n", + " tmp = q.link;\n", + " q.link = None\n", + " return\n", + " print \"\\n\\nThere is no element whose value is %d\" %data\n", + "\n", + "def display_list():\n", + " global start\n", + " if(start == None):\n", + " print \"\\n\\nList empty\"\n", + " return\n", + " q=start\n", + " print \"\\nThe List is...\"\n", + " while(q != None):\n", + " print q.data,\n", + " q = q.link\n", + " print \"\\n\"\n", + "\n", + "def get_data():\n", + " data = int(raw_input(\"Enter the data: \"))\n", + " return data\n", + "\n", + "while(1):\n", + " print \"\\n1.Insert at the beginning of the list\"\n", + " print \"2.Delete an element at a give position\"\n", + " print \"3.Delete an element with a give value\"\n", + " print \"4.Quit\"\n", + " print \"Choice: \",\n", + " choice =int(raw_input())\n", + " if choice == 1:\n", + " data = get_data()\n", + " add_front(data)\n", + " elif choice == 2:\n", + " if (start == None):\n", + " print \"\\nList empty\"\n", + " continue\n", + " print \"\\n\\nEnter the position:\"\n", + " pos = int(raw_input())\n", + " del_pos(pos)\n", + " elif choice == 3:\n", + " if (start == None):\n", + " print \"\\nList empty\"\n", + " continue\n", + " data = get_data()\n", + " del_elem(data)\n", + " elif choice == 4:\n", + " break\n", + " display_list()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "1 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "2 1 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "3 2 1 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "Enter the position:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "The List is...\n", + "3 1 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "2 3 1 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "1 2 3 1 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "5 1 2 3 1 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "Enter the position:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "The List is...\n", + "5 1 2 3 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.5, page no. 424" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def bubble(x, upbound):\n", + " pas = upbound\n", + " while pas >= 0:\n", + " for i in range(pas):\n", + " if x[i] > x[i+1]:\n", + " x[i], x[i+1] = x[i+1], x[i]\n", + " pas -= 1\n", + " return x\n", + "vector = []\n", + "n = int(raw_input(\"Size of array? \"))\n", + "for i in range(n):\n", + " vector.append(int(raw_input()))\n", + "vector = bubble(vector, n-1)\n", + "print \"Sorted array \"\n", + "for ele in vector:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of array? 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "98\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "62\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorted array \n", + "0 1 5 12 34 62 88 98\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.6, page no. 425" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "s1 = raw_input(\"Enter string one \")\n", + "s2 = raw_input(\"Enter string two \")\n", + "print \"Concatenated string is: \", s1+s2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter string one Tejaswi\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter string two .V.\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Concatenated string is: Tejaswi.V.\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.7, page no. 425" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def sort_strings(strarr):\n", + " return sorted(strarr)\n", + "\n", + "n = int(raw_input(\"Number of strings: \"))\n", + "str_array = []\n", + "print \"Enter the strings: \"\n", + "for i in range(n):\n", + " str_array.append(raw_input())\n", + "\n", + "strarr = sort_strings(str_array)\n", + "print \"The sorted array is\"\n", + "for string in strarr:\n", + " print string" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of strings: 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the strings: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tejaswi\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Anand\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rajkumar\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maya\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sudeep\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sorted array is\n", + "Anand\n", + "Maya\n", + "Rajkumar\n", + "Sudeep\n", + "Tejaswi\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.8, page no. 427" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "\n", + "class node:\n", + " def __init__(self, data=0, link=None):\n", + " self.data = data\n", + " self.link = link\n", + "\n", + "start = None\n", + " \n", + "def add_front(data):\n", + " global start\n", + " tmp = node(data)\n", + " if(start == None):\n", + " start = tmp\n", + " else:\n", + " tmp.link = start\n", + " start = tmp\n", + "\n", + "def del_pos(pos):\n", + " global start\n", + " if (pos == 0):\n", + " tmp = start\n", + " start = start.link\n", + " return\n", + " q = start\n", + " count = 1\n", + " while(q.link != None):\n", + " if(count == pos):\n", + " tmp = q.link\n", + " q.link = tmp.link\n", + " return\n", + " count += 1\n", + " q = q.link\n", + " print \"\\n\\nThere is no element at %d position\" %pos\n", + " \n", + "def del_elem(data):\n", + " global start\n", + " if (start.data == data):\n", + " tmp = start\n", + " start = start.link\n", + " return\n", + " q = start\n", + " while(q.link.link != None):\n", + " if(q.link.data == data):\n", + " tmp = q.link\n", + " q.link = tmp.link\n", + " return\n", + " q = q.link\n", + " if(q.link.data == data):\n", + " tmp = q.link;\n", + " q.link = None\n", + " return\n", + " print \"\\n\\nThere is no element whose value is %d\" %data\n", + "\n", + "def display_list():\n", + " global start\n", + " if(start == None):\n", + " print \"\\n\\nList empty\"\n", + " return\n", + " q=start\n", + " print \"\\nThe List is...\"\n", + " while(q != None):\n", + " print q.data,\n", + " q = q.link\n", + " print \"\\n\"\n", + "\n", + "def smallest():\n", + " global start\n", + " q = start\n", + " min = q.data\n", + " pos = 0\n", + " min_pos = 0\n", + " while(q != None):\n", + " if q.data < min:\n", + " min = q.data\n", + " mis_pos = pos\n", + " pos += 1\n", + " q = q.link\n", + " return min, min_pos+1\n", + "\n", + "def largest():\n", + " global start\n", + " q = start\n", + " max = q.data\n", + " pos = 0\n", + " max_pos = 0\n", + " while(q != None):\n", + " if q.data > max:\n", + " max = q.data\n", + " max_pos = pos\n", + " pos += 1\n", + " q = q.link\n", + " return max, max_pos+1\n", + "\n", + "def get_data():\n", + " data = int(raw_input(\"Enter the data: \"))\n", + " return data\n", + "\n", + "while(1):\n", + " print \"\\n1.Insert at the beginning of the list\"\n", + " print \"2.Delete an element at a give position\"\n", + " print \"3.Delete an element with a give value\"\n", + " print \"4.Find smallest in the list\"\n", + " print \"5.Find largest in the list\"\n", + " print \"6.Quit\"\n", + " print \"Choice: \",\n", + " choice =int(raw_input())\n", + " if choice == 1:\n", + " data = get_data()\n", + " add_front(data)\n", + " elif choice == 2:\n", + " if (start == None):\n", + " print \"\\nList empty\"\n", + " continue\n", + " print \"\\n\\nEnter the position:\"\n", + " pos = int(raw_input())\n", + " del_pos(pos)\n", + " elif choice == 3:\n", + " if (start == None):\n", + " print \"\\nList empty\"\n", + " continue\n", + " data = get_data()\n", + " del_elem(data)\n", + " elif choice == 4:\n", + " min, pos = smallest()\n", + " print \"The smallest element has value \", min\n", + " print \"Its position is \", pos\n", + " elif choice == 5:\n", + " max, pos = largest()\n", + " print \"The largest element has value \", max\n", + " print \"Its position is \", pos\n", + " elif choice == 6:\n", + " break\n", + " display_list()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 56\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "45 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The smallest element has value 34\n", + "Its position is 1\n", + "\n", + "The List is...\n", + "45 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The largest element has value 56\n", + "Its position is 3\n", + "\n", + "The List is...\n", + "45 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "3 45 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 80\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "80 3 45 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "Enter the position:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "There is no element at 9 position\n", + "\n", + "The List is...\n", + "80 3 45 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "Enter the position:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "The List is...\n", + "80 3 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 80\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "3 34 56 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Find smallest in the list\n", + "5.Find largest in the list\n", + "6.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 16 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter12.ipynb b/Mastering_C/chapter12.ipynb new file mode 100644 index 00000000..fd18aaec --- /dev/null +++ b/Mastering_C/chapter12.ipynb @@ -0,0 +1,4076 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:0f4233c9bc3c7a3995f155be01ebee4b829ac86b3935e6b7d37705fb0fea5376" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 12: Data Structures" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Example 12.1, page no. 442" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "arr = numpy.zeros((2, 2, 2))\n", + "\n", + "print \"Reading the elements of an array: \"\n", + "for i in range(2):\n", + " for j in range(2):\n", + " for k in range(2):\n", + " arr[i][j][k] = int(raw_input())\n", + "\n", + "print \"Displaying the elements of an array: \"\n", + "for i in range(2):\n", + " print \"\\n\\t\"\n", + " for j in range(2):\n", + " for k in range(2):\n", + " print \"\\t %3d \" %(arr[i][j][k]),\n", + " print \"\\n\\t\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reading the elements of an array: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "25\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "26\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "13\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "71\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Displaying the elements of an array: \n", + "\n", + "\t\n", + "\t 15 \t 25 \n", + "\t\n", + "\t 26 \t 13 \n", + "\t\n", + "\n", + "\t\n", + "\t 71 \t 9 \n", + "\t\n", + "\t 1 \t 8 \n", + "\t\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.2, page no. 442" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "str1 = raw_input(\"Input the first string: \")\n", + "str2 = raw_input(\"Input the second string: \")\n", + "print \"The concatenated string is ...\"\n", + "print str1+str2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the first string: Tejaswi \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the second string: Venugopal\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The concatenated string is ...\n", + "Tejaswi Venugopal\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.3, page no. 443" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class record:\n", + " name = ''\n", + " regno = ''\n", + " avg = 0.0\n", + " rank = 0\n", + "i = 0\n", + "students = []\n", + "while True:\n", + " print \"Processing student %d record...\" %(i+1)\n", + " print \"Student name ?(END to terminate): \"\n", + " stud = record()\n", + " stud.name = raw_input()\n", + " if stud.name == 'END':\n", + " break\n", + " else:\n", + " stud.regno = raw_input(\"Reg. no? \")\n", + " stud.avg = float(raw_input(\"Average marks? \"))\n", + " students.append(stud)\n", + " i+=1\n", + "n = i\n", + "for i in range(n-1):\n", + " for j in range(i+1, n):\n", + " if students[i].avg < students[j].avg:\n", + " students[i], students[j] = students[j], students[i]\n", + "for i in range(n):\n", + " students[i].rank = i+1\n", + "print \"Student records after assigning ranks: \"\n", + "print \"%s%20s%20s%20s\" %(\"Name\",\"REGISTER_NUMBER\",\"AVERAGE\",\"RANK\")\n", + "print \"------------------------------------------------------------------\"\n", + "for i in range(n):\n", + " print students[i].name, \"\\t\\t\", students[i].regno, \"\\t\\t\\t\", students[i].avg, \"\\t\\t\\t\", students[i].rank" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 1 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Asha\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak001\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 67.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 2 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bina\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak003\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 34.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 3 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Anand\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak004\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 67.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 4 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sudeep\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak005\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 78.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 5 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tejaswi\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak006\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 89.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 6 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maya\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak007\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 89.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 7 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Prakash\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak008\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 89.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 8 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Raju\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak009\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 82.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 9 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Geetha\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak010\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 85.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 10 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Gopi\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak011\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 45.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 11 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ganesh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reg. no? ak012\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks? 89.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Processing student 12 record...\n", + "Student name ?(END to terminate): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "END\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Student records after assigning ranks: \n", + "Name REGISTER_NUMBER AVERAGE RANK\n", + "------------------------------------------------------------------\n", + "Tejaswi \t\tak006 \t\t\t89.0 \t\t\t1\n", + "Maya \t\tak007 \t\t\t89.0 \t\t\t2\n", + "Prakash \t\tak008 \t\t\t89.0 \t\t\t3\n", + "Ganesh \t\tak012 \t\t\t89.0 \t\t\t4\n", + "Geetha \t\tak010 \t\t\t85.0 \t\t\t5\n", + "Raju \t\tak009 \t\t\t82.0 \t\t\t6\n", + "Sudeep \t\tak005 \t\t\t78.0 \t\t\t7\n", + "Anand \t\tak004 \t\t\t67.0 \t\t\t8\n", + "Asha \t\tak001 \t\t\t67.0 \t\t\t9\n", + "Gopi \t\tak011 \t\t\t45.0 \t\t\t10\n", + "Bina \t\tak003 \t\t\t34.0 \t\t\t11\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.4, page no. 446" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "arr = numpy.zeros((3,3))\n", + "print \"Reading the elements of 2-D array: \"\n", + "for i in range(3):\n", + " for j in range(3):\n", + " arr[i][j] = int(raw_input())\n", + "print \"Displaying the elements of 2-D array: \"\n", + "p = arr\n", + "for i in range(3):\n", + " for j in range(3):\n", + " print \"arr[%d][%d] = %d\" %(i, j, p[i][j]),\n", + " print \"\\tAddress of arr[%d][%d] = %d\" %(i, j, id(p[i][j]))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reading the elements of 2-D array: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Displaying the elements of 2-D array: \n", + "arr[0][0] = 1 \tAddress of arr[0][0] = 52403744\n", + "arr[0][1] = 2 \tAddress of arr[0][1] = 57791200\n", + "arr[0][2] = 3 \tAddress of arr[0][2] = 55892528\n", + "arr[1][0] = 4 \tAddress of arr[1][0] = 55892528\n", + "arr[1][1] = 5 \tAddress of arr[1][1] = 55892528\n", + "arr[1][2] = 6 \tAddress of arr[1][2] = 51486976\n", + "arr[2][0] = 7 \tAddress of arr[2][0] = 58587952\n", + "arr[2][1] = 8 \tAddress of arr[2][1] = 50218032\n", + "arr[2][2] = 9 \tAddress of arr[2][2] = 50218032\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.5, page no. 449" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "\n", + "class node:\n", + " def __init__(self, data=0, link=None):\n", + " self.data = data\n", + " self.link = link\n", + "\n", + "start = None\n", + " \n", + "def add_front(data):\n", + " global start\n", + " tmp = node(data)\n", + " if(start == None):\n", + " start = tmp\n", + " else:\n", + " tmp.link = start\n", + " start = tmp\n", + "\n", + "def del_pos(pos):\n", + " global start\n", + " if (pos == 0):\n", + " tmp = start\n", + " start = start.link\n", + " return\n", + " q = start\n", + " count = 1\n", + " while(q.link != None):\n", + " if(count == pos):\n", + " tmp = q.link\n", + " q.link = tmp.link\n", + " return\n", + " count += 1\n", + " q = q.link\n", + " print \"\\n\\nThere is no element at %d position\" %pos\n", + " \n", + "def del_elem(data):\n", + " global start\n", + " if (start.data == data):\n", + " tmp = start\n", + " start = start.link\n", + " return\n", + " q = start\n", + " while(q.link.link != None):\n", + " if(q.link.data == data):\n", + " tmp = q.link\n", + " q.link = tmp.link\n", + " return\n", + " q = q.link\n", + " if(q.link.data == data):\n", + " tmp = q.link;\n", + " q.link = None\n", + " return\n", + " print \"\\n\\nThere is no element whose value is %d\" %data\n", + "\n", + "def display_list():\n", + " global start\n", + " if(start == None):\n", + " print \"\\n\\nList empty\"\n", + " return\n", + " q=start\n", + " print \"\\nThe List is...\"\n", + " while(q != None):\n", + " print q.data,\n", + " q = q.link\n", + " print \"\\n\"\n", + "\n", + "def get_data():\n", + " data = int(raw_input(\"Enter the data: \"))\n", + " return data\n", + "\n", + "while(1):\n", + " print \"\\n1.Insert at the beginning of the list\"\n", + " print \"2.Delete an element at a give position\"\n", + " print \"3.Delete an element with a give value\"\n", + " print \"4.Quit\"\n", + " print \"Choice: \",\n", + " choice =int(raw_input())\n", + " if choice == 1:\n", + " data = get_data()\n", + " add_front(data)\n", + " elif choice == 2:\n", + " if (start == None):\n", + " print \"\\nList empty\"\n", + " continue\n", + " print \"\\n\\nEnter the position:\"\n", + " pos = int(raw_input())\n", + " del_pos(pos)\n", + " elif choice == 3:\n", + " if (start == None):\n", + " print \"\\nList empty\"\n", + " continue\n", + " data = get_data()\n", + " del_elem(data)\n", + " elif choice == 4:\n", + " break\n", + " display_list()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "0 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "1 0 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "2 1 0 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "3 2 1 0 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "4 3 2 1 0 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "Enter the position:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "The List is...\n", + "4 2 1 0 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "The List is...\n", + "4 2 0 \n", + "\n", + "\n", + "1.Insert at the beginning of the list\n", + "2.Delete an element at a give position\n", + "3.Delete an element with a give value\n", + "4.Quit\n", + "Choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.6, page no. 454" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "\n", + "class node:\n", + " def __init__(self, info=None, link=None):\n", + " self.info = info\n", + " self.link = link\n", + "\n", + "last = None\n", + "class Circular_Linked:\n", + " def create(self, num):\n", + " global last\n", + " q = node()\n", + " tmp = node()\n", + " tmp.info = num\n", + " if (last == None):\n", + " last = tmp\n", + " tmp.link = last\n", + " else:\n", + " tmp.link = last.link\n", + " last.link = tmp\n", + " last = tmp\n", + "\n", + " def insert(self, num, pos):\n", + " global last\n", + " q = node()\n", + " tmp = node()\n", + " q = last.link\n", + " for i in range(0, pos-1):\n", + " q = q.link\n", + " if (q == last.link):\n", + " print \"There are less than \", pos, \" elements\"\n", + " return\n", + " tmp.link = q.link\n", + " tmp.info = num\n", + " q.link = tmp\n", + " if(q==last):\n", + " last=tmp\n", + "\n", + " def delete(self):\n", + " global last\n", + " if(last == None):\n", + " print \"LIST EMPTY - CANNOT DELETE\"\n", + " return\n", + " print \"Data item to be deleted:\"\n", + " num = int(raw_input())\n", + " q = node()\n", + " tmp = node()\n", + " if( last.link == last and last.info == num):\n", + " tmp = last\n", + " last = None\n", + " return\n", + " q = last.link\n", + " if(q.info == num):\n", + " tmp = q\n", + " last.link = q.link\n", + " return\n", + " while(q.link != last):\n", + " if(q.link.info == num):\n", + " tmp = q.link\n", + " q.link = tmp.link\n", + " print num, \" deleted\"\n", + " q = q.link\n", + " if(q.link.info == num):\n", + " tmp = q.link\n", + " q.link = last.link\n", + " last = q\n", + " return\n", + " print \"Element not found\"\n", + "\n", + " def display(self):\n", + " global last\n", + " q = node()\n", + " if(last == None):\n", + " print \"List is empty\"\n", + " return\n", + " q = last.link\n", + " print \"List is:\"\n", + " while(q != last):\n", + " print q.info,\n", + " q = q.link\n", + " print last.info\n", + " \n", + "co = Circular_Linked()\n", + "while(1):\n", + " print \"\\n1.Create Circular Linked List\"\n", + " print \"2.Insert an item\"\n", + " print \"3.Delete an item\"\n", + " print \"4.STOP\"\n", + " print \"Enter your choice:\"\n", + " choice = int(raw_input())\n", + " if choice == 1:\n", + " print \"How many nodes you want:\",\n", + " n = int(raw_input())\n", + " for i in range(n):\n", + " print \"\\nEnter the element:\",\n", + " m = int(raw_input())\n", + " co.create(m)\n", + " elif choice == 2:\n", + " print \"\\nEnter the element:\",\n", + " m = int(raw_input())\n", + " print \"\\nEnter the position after which this element is inserted:\",\n", + " po = int(raw_input())\n", + " co.insert(m,po)\n", + " elif choice == 3:\n", + " co.delete()\n", + " elif choice == 4:\n", + " break\n", + " else:\n", + " print \"Error - Try again\"\n", + " co.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "1.Create Circular Linked List\n", + "2.Insert an item\n", + "3.Delete an item\n", + "4.STOP\n", + "Enter your choice:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many nodes you want:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter the element:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter the element:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter the element:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter the element:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " List is:\n", + "1 2 3 4\n", + "\n", + "1.Create Circular Linked List\n", + "2.Insert an item\n", + "3.Delete an item\n", + "4.STOP\n", + "Enter your choice:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many nodes you want:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter the element:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " List is:\n", + "1 2 3 4 5\n", + "\n", + "1.Create Circular Linked List\n", + "2.Insert an item\n", + "3.Delete an item\n", + "4.STOP\n", + "Enter your choice:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter the element:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter the position after which this element is inserted:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " List is:\n", + "1 0 2 3 4 5\n", + "\n", + "1.Create Circular Linked List\n", + "2.Insert an item\n", + "3.Delete an item\n", + "4.STOP\n", + "Enter your choice:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data item to be deleted:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 deleted\n", + "Element not found\n", + "List is:\n", + "1 2 3 4 5\n", + "\n", + "1.Create Circular Linked List\n", + "2.Insert an item\n", + "3.Delete an item\n", + "4.STOP\n", + "Enter your choice:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data item to be deleted:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Element not found\n", + "List is:\n", + "1 2 3 4 5\n", + "\n", + "1.Create Circular Linked List\n", + "2.Insert an item\n", + "3.Delete an item\n", + "4.STOP\n", + "Enter your choice:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.7, page no. 461" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MAXSIZE = 100\n", + "\n", + "class stack:\n", + " def __init__(self):\n", + " self.stack = []\n", + " self.top = -1\n", + "\n", + "def push(pu):\n", + " if(pu.top == MAXSIZE-1):\n", + " print \"The stack is full\"\n", + " else:\n", + " print \"Enter the element to be inserted: \"\n", + " item = int(raw_input())\n", + " pu.stack.append(item)\n", + " pu.top += 1\n", + "\n", + "def pop(po):\n", + " if(po.top == -1):\n", + " print \"Stack is empty\"\n", + " else:\n", + " item = po.stack[po.top]\n", + " po.top-=1\n", + " print \"The popped element is: %d\" %item\n", + "\n", + "\n", + "def display(pt):\n", + " if(pt.top == -1):\n", + " print \"Stack is empty\"\n", + " else:\n", + " print \"The element(s) of the stack are: \"\n", + " i = pt.top\n", + " while(i>=0):\n", + " print \"%d\" %pt.stack[i],\n", + " i-=1\n", + "\n", + "ps = stack()\n", + "ch = 'y'\n", + "\n", + "while(1):\n", + " print \"\\n1. Push into the stack\"\n", + " print \"2. POP\"\n", + " print \"3. View elements of the stack\"\n", + " print \"4. Exit\"\n", + " print \"Enter your choice: \"\n", + " choice = int(raw_input())\n", + " if(choice == 1):\n", + " push(ps)\n", + " elif(choice == 2):\n", + " pop(ps)\n", + " elif(choice == 3):\n", + " display(ps)\n", + " elif(choice == 4):\n", + " break\n", + " else:\n", + " print \"Invalid Choice\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Push into the stack\n", + "2. POP\n", + "3. View elements of the stack\n", + "4. Exit\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the element to be inserted: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Push into the stack\n", + "2. POP\n", + "3. View elements of the stack\n", + "4. Exit\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the element to be inserted: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "23\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Push into the stack\n", + "2. POP\n", + "3. View elements of the stack\n", + "4. Exit\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the element to be inserted: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Push into the stack\n", + "2. POP\n", + "3. View elements of the stack\n", + "4. Exit\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The popped element is: 67\n", + "\n", + "1. Push into the stack\n", + "2. POP\n", + "3. View elements of the stack\n", + "4. Exit\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the element to be inserted: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Push into the stack\n", + "2. POP\n", + "3. View elements of the stack\n", + "4. Exit\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The element(s) of the stack are: \n", + "67 23 12 \n", + "1. Push into the stack\n", + "2. POP\n", + "3. View elements of the stack\n", + "4. Exit\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.8, page no. 463" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class node:\n", + " def __init__(self, link=None):\n", + " self.data = None\n", + " self.link = link\n", + " \n", + "start = None\n", + "\n", + "\n", + "def push(top):\n", + " new = node()\n", + " print \"Enter element to be pushed: \",\n", + " pushed_item = int(raw_input())\n", + " new.data = pushed_item\n", + " new.link = top\n", + " top = new\n", + " return top\n", + "\n", + "def pop(top):\n", + " tmp = node()\n", + " if(top == None):\n", + " print \"Stack is empty...\" \n", + " else:\n", + " tmp = top\n", + " print \"Popped item is: \", tmp.data\n", + " top = top.link\n", + " tmp.link = None\n", + " return top\n", + "\n", + "def display(top):\n", + " if(top == None):\n", + " print \"Stack is empty...\"\n", + " else:\n", + " while(top.link != None):\n", + " print top.data,\n", + " top = top.link\n", + " \n", + "Top = node()\n", + "while(1):\n", + " print \"\\n1.PUSH\"\n", + " print \"2.POP\"\n", + " print \"3.END\"\n", + " print \"Enter your choice: \",\n", + " choice = int(raw_input())\n", + " if choice == 1:\n", + " Top = push(Top)\n", + " print \"STACK after pushing: \"\n", + " display(Top)\n", + " elif choice == 2:\n", + " Top=pop(Top)\n", + " print \"STACK after popping: \"\n", + " display(Top)\n", + " elif choice == 3:\n", + " print \"End of comutation\"\n", + " break\n", + " else:\n", + " print \"Invalid Choice\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter element to be pushed: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "23\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " STACK after pushing: \n", + "23 \n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter element to be pushed: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " STACK after pushing: \n", + "12 23 \n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter element to be pushed: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " STACK after pushing: \n", + "67 12 23 \n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Popped item is: 67\n", + "STACK after popping: \n", + "12 23 \n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Popped item is: 12\n", + "STACK after popping: \n", + "23 \n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter element to be pushed: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " STACK after pushing: \n", + "88 23 \n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Popped item is: 88\n", + "STACK after popping: \n", + "23 \n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Popped item is: 23\n", + "STACK after popping: \n", + "\n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Popped item is: None\n", + "STACK after popping: \n", + "Stack is empty...\n", + "\n", + "1.PUSH\n", + "2.POP\n", + "3.END\n", + "Enter your choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " End of comutation\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.9, page no. 467" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "stck = []\n", + "\n", + "def push(sp,ch):\n", + " if sp==100:\n", + " print \"Error : Stack overflow\"\n", + " else:\n", + " sp += 1\n", + " stck.append(ch)\n", + "\n", + "def pop(sp):\n", + " if sp < 0:\n", + " print \"Error : Stack underflow\"\n", + " return 0\n", + " else:\n", + " sp -= 1\n", + " return stck[sp+1]\n", + "\n", + "def alpha(ch):\n", + " if (ch>='A' and ch<=\"Z\") or (ch>='a' and ch<=\"z\"):\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "def num(ch):\n", + " if ch>='0' and ch<=\"9\":\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "def oper(ch):\n", + " oparray = ['+','-','*','/','^']\n", + " if ch in oparray:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + "def precedence(ch):\n", + " if ch=='(':\n", + " i = 0\n", + " elif ch=='+' or ch=='-':\n", + " i = 1\n", + " elif ch=='*' or ch=='/':\n", + " i = 2\n", + " elif ch=='^':\n", + " i = 3\n", + " return i\n", + "\n", + "def printerror(n,ifp):\n", + " if n==1:\n", + " print \"Error_1_: Missing operator\",\n", + " elif n==2:\n", + " print \"Error_2_: Missing Operand\",\n", + " elif n==3:\n", + " print \"Error_3_: Mismatched parentehses\",\n", + " elif n==4:\n", + " print \"Error_4_: Illegal character in expression\",\n", + " elif n==5:\n", + " print \"Error_5_: Multiletter variable\",\n", + " print \"Column %2d\"%ifp\n", + " \n", + "\n", + "def parse (infix,suffix,error):\n", + " ch =''\n", + " last = '('\n", + " i = 0\n", + " sp=-1\n", + " bracket = 1\n", + " ifp = 0\n", + " sfp = 0\n", + " \n", + " push(sp,'(')\n", + " \n", + " while True:\n", + " while infix[i] == \" \":\n", + " i += 1\n", + " ifp = i\n", + " ch = infix[ifp]\n", + " \n", + " if ch == \"(\":\n", + " if alpha(last) or num(last) or last==')':\n", + " error = 1\n", + " printerror(1,ifp+1)\n", + " bracket +=1 \n", + " push(sp,ch)\n", + " elif ch==\"+\" or ch==\"-\" or ch==\"*\" or ch==\"/\" or ch==\"^\":\n", + " if oper(last) or (not alpha(last) and not num(last) and last!=\")\"):\n", + " error=1\n", + " printerror(2,ifp+1)\n", + " while precedence(stck[sp]) >= precedence(ch):\n", + " suffix.append(pop(sp))\n", + " sfp+=1\n", + " push(sp,ch)\n", + " elif ch==\")\":\n", + " if oper(last):\n", + " error=1\n", + " printerror(2,ifp+1)\n", + " if last==\"(\":\n", + " error=1\n", + " printerror(2,ifp+1)\n", + " printerror(1,ifp+1)\n", + " while stck[sp] != \"(\" and sp > 0:\n", + " suffix.append(pop(sp))\n", + " sfp += 1\n", + " sp -= 1\n", + " bracket -= 1\n", + " elif ch == ' ':\n", + " while infix[ifp+1] == \" \" and not infix[ifp] :\n", + " ifp += 1\n", + " else:\n", + " if alpha(last) or num(last):\n", + " error = 1 \n", + " printerror(1,ifp+1)\n", + " printerror(5,ifp+1)\n", + " if alpha(ch):\n", + " print ch,\n", + " suffix.append(infix[ifp])\n", + " sfp += 1\n", + " elif num(ch):\n", + " while num(infix[ifp]):\n", + " suffix.append(infix[ifp])\n", + " ifp += 1\n", + " sfp += 1\n", + " ifp -= 1\n", + " else:\n", + " printerror(4,ifp+1)\n", + " last = infix[ifp]\n", + " ifp += 1\n", + " i = ifp\n", + " print ifp,\n", + " try:\n", + " if not infix[ifp]:\n", + " break;\n", + " except:\n", + " break\n", + " \n", + " if bracket:\n", + " error = 1\n", + " printerror(3,ifp-1)\n", + " return infix,suffix,error\n", + "def readexpression():\n", + " i = 0\n", + " print \"The given infix expression is\"\n", + " infix = list(raw_input())\n", + " infix.append(\")\")\n", + " \n", + " return infix\n", + "\n", + "\n", + "suffix = [] \n", + "error = 0\n", + "infix = readexpression()\n", + "infix,suffix,error = parse(infix,suffix,error)\n", + "if not error:\n", + " print \"The suffix form of the given expression is \"\n", + " print suffix" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The given infix expression is\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "(a+b)\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 Error_3_: Mismatched parentehses Column 0\n", + "a 2 Error_3_: Mismatched parentehses Column 1\n", + "3 Error_3_: Mismatched parentehses Column 2\n", + "b 4 Error_3_: Mismatched parentehses Column 3\n", + "5 Error_3_: Mismatched parentehses Column 4\n", + "6\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.10, page no. 472" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MAX = 50\n", + "queue_arr = []\n", + "rear = [-1]\n", + "front = [-1]\n", + "\n", + "def add_element():\n", + " if (rear[0] == MAX-1):\n", + " print \"Queue is full...\"\n", + " return\n", + " else:\n", + " if(front[0] == -1):\n", + " front[0] = 0\n", + " print \"Enter the new element: \"\n", + " added_item = int(raw_input())\n", + " rear[0] = rear[0]+1\n", + " queue_arr.append(added_item)\n", + "\n", + "def remove_element():\n", + " if(front[0] == -1 or front[0]>rear[0]):\n", + " print \"Queue is empty...\"\n", + " return\n", + " else:\n", + " dell = front[0]\n", + " print queue_arr[dell], \" is removed from the Queue\"\n", + " front[0] = front[0]+1\n", + "\n", + "def display():\n", + " if(front[0] == -1 or front[0]>rear[0]):\n", + " print \"Queue is empty...\"\n", + " return\n", + " else:\n", + " print \"Queue is: \"\n", + " for i in range(front[0],rear[0]+1):\n", + " print queue_arr[i],\n", + "\n", + "while(1):\n", + " print \"\\n1. Insert an element\"\n", + " print \"2. Display all the elements of the queue\"\n", + " print \"3. Remove elements from the queue\"\n", + " print \"4. Exit from the program\"\n", + " print \"Enter your choice: \"\n", + " choice = raw_input()\n", + " if choice == '1':\n", + " add_element()\n", + " elif choice == '2':\n", + " display()\n", + " elif choice == '3':\n", + " remove_element()\n", + " elif choice == '4':\n", + " break\n", + " else:\n", + " print \"Reenter the choice\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the new element: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "23\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the new element: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the new element: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "23 is removed from the Queue\n", + "\n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "34 is removed from the Queue\n", + "\n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the new element: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Queue is: \n", + "45 45 \n", + "1. Insert an element\n", + "2. Display all the elements of the queue\n", + "3. Remove elements from the queue\n", + "4. Exit from the program\n", + "Enter your choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.11, page no. 477" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class node:\n", + " def __init__(self, link=None):\n", + " self.info = None\n", + " self.link = link\n", + "\n", + "def enqueue(rear):\n", + " new_node = node()\n", + " print \"Enter the new element: \",\n", + " new_node.info = int(raw_input())\n", + " new_node.link = None\n", + " if (rear != None):\n", + " rear.link = new_node\n", + " rear = new_node\n", + " return rear\n", + "\n", + "def del_queue(f, r):\n", + " if f == None:\n", + " print \"The Queue is empty\"\n", + " else:\n", + " print \"The element removed is: \", f.info\n", + " if f != r:\n", + " f = f.link\n", + " else:\n", + " f = None\n", + " return f\n", + "\n", + "def display(fr, re):\n", + " if fr == None:\n", + " print \"The Queue is empty\"\n", + " else:\n", + " print \"The queue is: \"\n", + " while fr != re:\n", + " print fr.info,\n", + " fr = fr.link\n", + " print fr.info,\n", + "\n", + "rear = None\n", + "front = None\n", + "while(1):\n", + " print \"\\n1.Insert\"\n", + " print \"2.Delete\"\n", + " print \"3.Quit\"\n", + " print \"Enter choice: \",\n", + " choice = int(raw_input())\n", + " if choice == 1:\n", + " rear = enqueue(rear)\n", + " if front == None:\n", + " front = rear\n", + " elif choice == 2:\n", + " front = del_queue(front, rear)\n", + " if front == None:\n", + " rear = None\n", + " elif choice == 3:\n", + " break\n", + " display(front, rear)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the new element: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "23\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The queue is: \n", + "23 \n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the new element: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The queue is: \n", + "23 34 \n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the new element: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The queue is: \n", + "23 34 45 \n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The element removed is: 23\n", + "The queue is: \n", + "34 45 \n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The element removed is: 34\n", + "The queue is: \n", + "45 \n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The element removed is: 45\n", + "The Queue is empty\n", + "\n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The Queue is empty\n", + "The Queue is empty\n", + "\n", + "1.Insert\n", + "2.Delete\n", + "3.Quit\n", + "Enter choice: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.12, page no. 480" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "qsize = 50\n", + "rear = 0\n", + "front = 0\n", + "qu = numpy.zeros(50, dtype=numpy.int)\n", + "def insert():\n", + " global rear\n", + " global front\n", + " global qsize\n", + " if rear == qsize:\n", + " rear = 1\n", + " else:\n", + " rear += 1\n", + " if front != rear:\n", + " item = int(raw_input(\"Enter item to be inserted: \"))\n", + " qu[rear] = item\n", + " if front == 0:\n", + " front += 1\n", + " else:\n", + " print \"Overflow\"\n", + "def delete():\n", + " global front\n", + " global rear\n", + " global qsize\n", + " if front != 0:\n", + " item = qu[front]\n", + " if front == rear:\n", + " front = 0\n", + " rear = 0\n", + " return item\n", + " if front == qsize:\n", + " front = 1\n", + " else:\n", + " front += 1\n", + " return item\n", + " else:\n", + " print \"Underflow\"\n", + "def display():\n", + " global front\n", + " global rear\n", + " for i in range(front, rear+1):\n", + " print qu[i],\n", + "\n", + "choice = 0\n", + "while(choice != 3):\n", + " print \"\\n1 - Insert an element into Cqueue\"\n", + " print \"2 - Delete an element into Cqueue\"\n", + " print \"3 - Quit\"\n", + " choice = int(raw_input())\n", + " if choice == 1:\n", + " insert()\n", + " print \"\\nCqueue after insertion is: \"\n", + " display()\n", + " elif choice == 2:\n", + " item = delete()\n", + " print \"Item deleted id: \", item\n", + " print \"\\nCqueue after deletion is: \"\n", + " display()\n", + " else:\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter item to be inserted: 23\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Cqueue after insertion is: \n", + "23 \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter item to be inserted: 34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Cqueue after insertion is: \n", + "23 34 \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter item to be inserted: 45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Cqueue after insertion is: \n", + "23 34 45 \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Item deleted id: 23\n", + "\n", + "Cqueue after deletion is: \n", + "34 45 \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Item deleted id: 34\n", + "\n", + "Cqueue after deletion is: \n", + "45 \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Item deleted id: 45\n", + "\n", + "Cqueue after deletion is: \n", + "0 \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Underflow\n", + "Item deleted id: None\n", + "\n", + "Cqueue after deletion is: \n", + "0 \n", + "1 - Insert an element into Cqueue\n", + "2 - Delete an element into Cqueue\n", + "3 - Quit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Exmaple 9.13, page no. 484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class node:\n", + " data = ''\n", + " next = None\n", + "class queue:\n", + " front = None\n", + " nremovals = 0\n", + "\n", + "class prique:\n", + " queues = []\n", + " nqueues = 0\n", + " npromote = 0\n", + "\n", + "def add(que, new_node):\n", + " new_node.next = 0\n", + " if not que.front:\n", + " que.front = new_node\n", + " else:\n", + " cur = que.front\n", + " while(cur.next):\n", + " cur = cur.next\n", + " cur.next = new_node\n", + " return que\n", + "\n", + "def remove_node(que):\n", + " removed_node = que.front\n", + " if que.front:\n", + " que.front = que.front.next\n", + " return removed_node\n", + "\n", + "def priq_add(priq, priority, data):\n", + " if priority >= priq.nqueues:\n", + " print \"Error: priority number given is invalid!\"\n", + " return priq\n", + " new_node = node()\n", + " new_node.data = data\n", + " priq.queues[priority] = add(priq.queues[priority], new_node)\n", + " return priq\n", + "\n", + "def priq_remove(priq, priority):\n", + " removed_char = ''\n", + " if priority >= priq.nqueues:\n", + " print \"Error: priority number given is invalid!\"\n", + " return\n", + " removed = remove_node(priq.queues[priority])\n", + " if not removed:\n", + " if priority < priq.nqueues-1:\n", + " return priq_remove(priq, priority+1)\n", + " else:\n", + " return priq, 0\n", + " else:\n", + " lower_elm = ''\n", + " priq.queues[priority].nremovals += 1\n", + " if priq.queues[priority].nremovals == priq.npromote:\n", + " lower_elm = priq_remove(priq, priority+1)\n", + " if lower_elm:\n", + " priq_add(priq, priority, lower_elm)\n", + " priq.queues[priority].nremovals = 0\n", + " removed_char = removed.data\n", + " return priq, removed_char\n", + "\n", + " \n", + "def create_priq(nqueues, npromote):\n", + " priq = prique()\n", + " priq.nqueues = nqueues\n", + " for i in range(nqueues):\n", + " priq.queues.append(queue())\n", + " priq.npromote = npromote\n", + " return priq\n", + "\n", + "def get_option():\n", + " print \"\\n1: Insert an element\"\n", + " print \"2: Remove an element\"\n", + " print \"3: Quit\"\n", + " print \"Choose: \"\n", + " option = int(raw_input())\n", + " return option\n", + "\n", + "def get_data():\n", + " data = raw_input(\"Enter the data: \")\n", + " return data\n", + "\n", + "def get_priority():\n", + " priority = int(raw_input(\"Enter the priority: \"))\n", + " return priority\n", + "\n", + "def display_prique(priq):\n", + " print \"The priority queue is ...\"\n", + " for i in range(priq.nqueues):\n", + " que = priq.queues[i]\n", + " cur = que.front\n", + " print i,\n", + " while cur:\n", + " print cur.data,\n", + " if cur.next:\n", + " print \"->\",\n", + " cur = cur.next\n", + " print \"\"\n", + " print \"\"\n", + "\n", + "print \"No of priority levels: \"\n", + "nlevels = int(raw_input())\n", + "print \"No of consecutive removls for proirity promotion: \"\n", + "nremovals = int(raw_input())\n", + "priq = create_priq(nlevels, nremovals)\n", + "while True:\n", + " option = get_option()\n", + " if option == 1:\n", + " data = get_data()\n", + " priority = get_priority()\n", + " priq = priq_add(priq, priority, data)\n", + " elif option == 2:\n", + " priority = get_priority()\n", + " priq, data = priq_remove(priq, priority)\n", + " if data == 0:\n", + " print \"No elements to remove\"\n", + " else:\n", + " print data, \" was removed\"\n", + " elif option == 3:\n", + " break\n", + " else:\n", + " print \"BYE\"\n", + " break\n", + " display_prique(priq)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "No of priority levels: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "No of consecutive removls for proirity promotion: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: q\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The priority queue is ...\n", + "0 q \n", + "1 \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: w\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The priority queue is ...\n", + "0 q -> w \n", + "1 \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: e\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The priority queue is ...\n", + "0 q -> w -> e \n", + "1 \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The priority queue is ...\n", + "0 q -> w -> e -> r \n", + "1 \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the data: z\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The priority queue is ...\n", + "0 q -> w -> e -> r \n", + "1 z \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "q was removed\n", + "The priority queue is ...\n", + "0 w -> e -> r \n", + "1 z \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "w was removed\n", + "The priority queue is ...\n", + "0 e -> r \n", + "1 z \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "No elements to remove\n", + "The priority queue is ...\n", + "0 e -> r \n", + "1 z \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "e was removed\n", + "The priority queue is ...\n", + "0 r \n", + "1 z \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the priority: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "r was removed\n", + "The priority queue is ...\n", + "0 \n", + "1 z \n", + "2 \n", + "3 \n", + "\n", + "\n", + "1: Insert an element\n", + "2: Remove an element\n", + "3: Quit\n", + "Choose: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.14, page no. 495" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Node:\n", + " def __init__(self, data):\n", + " self.data = data\n", + " self.left = None\n", + " self.right = None\n", + "\n", + " def insert(self, data):\n", + " if data < self.data:\n", + " if self.left is None:\n", + " self.left = Node(data)\n", + " else:\n", + " self.left.insert(data)\n", + " elif data > self.data:\n", + " if self.right is None:\n", + " self.right = Node(data)\n", + " else:\n", + " self.right.insert(data)\n", + "\n", + " def inorder(self, tree):\n", + " solution = []\n", + " def recurse(node):\n", + " if not node:\n", + " return\n", + " recurse(node.left)\n", + " solution.append(node.data)\n", + " recurse(node.right)\n", + " recurse(tree)\n", + " return solution\n", + " def preorder(self, tree):\n", + " solution = []\n", + " def recurse(node):\n", + " if not node:\n", + " return\n", + " solution.append(node.data)\n", + " recurse(node.left)\n", + " recurse(node.right)\n", + " recurse(tree)\n", + " return solution\n", + " def postorder(self, tree):\n", + " solution = []\n", + " def recurse(node):\n", + " if not node:\n", + " return\n", + " recurse(node.left)\n", + " recurse(node.right)\n", + " solution.append(node.data)\n", + "\n", + " recurse(tree)\n", + " return solution\n", + "\n", + "print \"Type numbers, one per line\"\n", + "print \"-999 for terminating\"\n", + "num = int(raw_input())\n", + "tree = Node(num)\n", + "while num != -999:\n", + " num = int(raw_input())\n", + " if num == -999:\n", + " break\n", + " tree.insert(num)\n", + "choice = 0\n", + "while choice != 4:\n", + " print \"\\nType of TRAVERSAL ?\"\n", + " print \"1 - Inorder\"\n", + " print \"2 - Preorder\"\n", + " print \"3 - Postorder\"\n", + " print \"4 - STOP\"\n", + " choice = int(raw_input())\n", + " if choice == 1:\n", + " print \"INORDER traversal: \"\n", + " sol = tree.inorder(tree)\n", + " for ele in sol:\n", + " print ele,\n", + " elif choice == 2:\n", + " print \"PREORDER traversal: \"\n", + " sol = tree.preorder(tree)\n", + " for ele in sol:\n", + " print ele,\n", + " elif choice == 3:\n", + " print \"POSTORDER traversal: \"\n", + " sol = tree.postorder(tree)\n", + " for ele in sol:\n", + " print ele,\n", + " else:\n", + " print \"BYE\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Type numbers, one per line\n", + "-999 for terminating\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-999\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Type of TRAVERSAL ?\n", + "1 - Inorder\n", + "2 - Preorder\n", + "3 - Postorder\n", + "4 - STOP\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "INORDER traversal: \n", + "1 2 3 4 5 6 8 9 10 \n", + "Type of TRAVERSAL ?\n", + "1 - Inorder\n", + "2 - Preorder\n", + "3 - Postorder\n", + "4 - STOP\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "PREORDER traversal: \n", + "4 1 3 2 10 9 6 5 8 \n", + "Type of TRAVERSAL ?\n", + "1 - Inorder\n", + "2 - Preorder\n", + "3 - Postorder\n", + "4 - STOP\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "BYE\n" + ] + } + ], + "prompt_number": 11 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter15.ipynb b/Mastering_C/chapter15.ipynb new file mode 100644 index 00000000..cf449021 --- /dev/null +++ b/Mastering_C/chapter15.ipynb @@ -0,0 +1,99 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:64bc6ed635ee782424e2a9c6d347d0d839901e22bcfed6521e3f281977a7711d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "C Library Functions\n" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 15.1 page no : 655" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"ratsdrowninwordstar is a palindrom\"\n", + "print \"length of \\\"ratsdrowninwordstar is a palindrom\\\" is\",\n", + "Len = len(\"ratsdrowninwordstar\")\n", + "print \"%d\"%Len" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "ratsdrowninwordstar is a palindrom\n", + "length of \"ratsdrowninwordstar is a palindrom\" is 19\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 15.2 Pageno : 656" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "Num = 123.54\n", + "fNum = 0.0\n", + "cNum = 0.0\n", + "fNum = math.floor(Num)\n", + "cNum = math.ceil(Num)\n", + "print \"original number %.2lf\"%Num\n", + "print \"number rounded down %.lf\"%fNum\n", + "print \"number rounded up %.lf\"%cNum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "original number 123.54\n", + "number rounded down 123\n", + "number rounded up 124\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter16.ipynb b/Mastering_C/chapter16.ipynb new file mode 100644 index 00000000..e1edaa97 --- /dev/null +++ b/Mastering_C/chapter16.ipynb @@ -0,0 +1,116 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cdbb2a27d4aeed62bc661e588b07689ef9317248d78908eda6f1423b2cfb2027" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 16: Controlling Complexity" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16.1, page no. 705" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def cube(i):\n", + " return i*i*i\n", + "def beep():\n", + " print \"\\a\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example mainprog.c, page no. 705" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"The cube of 10 is \", cube(10)\n", + "input_int = int(raw_input(\"Input Integer: \"))\n", + "if input_int < 1:\n", + " beep()\n", + " print \"Input is Invalid\"\n", + "else:\n", + " print \"The cube of %d is %d\" %(input_int, 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 Integer: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\u0007\n", + "Input is Invalid\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example make.c, page no. 709" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def cube(i):\n", + " return i*i*i\n", + "def beep():\n", + " print \"I am going to beep!\"\n", + " print \"\\a\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter2.ipynb b/Mastering_C/chapter2.ipynb new file mode 100644 index 00000000..03566b1a --- /dev/null +++ b/Mastering_C/chapter2.ipynb @@ -0,0 +1,981 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:c47c99279bcc1ed930609c37047619e07a7ba5a5f3802de8eeb54d6cf31ebdd4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2: Variables and Expressions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example integer.c, page no. 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "val = 10\n", + "print \"%d\" %val" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example float.c, page no. 36" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i_var = 10\n", + "f_var = 100.3125\n", + "print \"i_var is %d and f_var is %f\" %(i_var, f_var)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i_var is 10 and f_var is 100.312500\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example decimal.c, page no. 37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "inp_int = int(raw_input())\n", + "print \"The input integer (in decimal) is %u\" %inp_int\n", + "print \"The input integer (in hexadecimal) is %x\" %inp_int" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The input integer (in decimal) is 45\n", + "The input integer (in hexadecimal) is 2d\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf.c, page no. 37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "years = float(raw_input(\"Input your age in years: \"))\n", + "secs = years*365*24*60*60\n", + "print \"You have lived for %0.1f seconds\" %secs" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input your age in years: 21\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have lived for 662256000.0 seconds\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example char.c, page no. 38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = '#'\n", + "print \"This is the hash symbol: %s\" %c" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is the hash symbol: #\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example ascii.c, page no. 39 " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "code = int(raw_input(\"Input an ASCII code (0 to 127):\" ))\n", + "symbol = chr(code)\n", + "print \"The symbol corresponding to %d is %s\" %(code,symbol)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input an ASCII code (0 to 127):75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The symbol corresponding to 75 is K\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example string.c, page no. 40" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_string = \"13 is unlucky.\"\n", + "print \"my_string: %s\" %my_string" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "my_string: 13 is unlucky.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example size.c ,page no. 41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "print \"Size of a int is %d\" %sys.getsizeof(int)\n", + "print \"Size of a long int is %d\" %sys.getsizeof(long)\n", + "\n", + "#Note: there is no short interger data type in Python\n", + "#Answers will differ" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of a int is 872\n", + "Size of a long int is 872\n" + ] + } + ], + "prompt_number": 27 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example inout.c, page no. 42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "shorti = int(raw_input(\"Input short int: \"))\n", + "longi = int(raw_input(\"Input long int: \"))\n", + "print \"shorti = %d and longi = %d\" %(shorti, longi)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input short int: 12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input long int: 200000000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "shorti = 12 and longi = 200000000\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example beep.c, page no. 47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"%c\" %('\\x07')\n", + "\n", + "#note: there won't be any beep in Python" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\u0007\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example tax.c, page no. 49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * 0.3\n", + "print \"tax = %.1f\" %tax" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example tax1.c, page no. 49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "tax_rate = 0.3\n", + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * tax_rate\n", + "print \"tax = %.1f\" %tax" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example const.c, page no. 50" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "tax_rate = 0.3\n", + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * tax_rate\n", + "print \"tax = %.1f\" %tax\n", + "\n", + "#Note: there is no constant data type in Python so the program will remain same as above" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example const.c, page no. 50" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "tax_rate = 0.3\n", + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * tax_rate\n", + "print \"tax = %.1f\" %tax\n", + "\n", + "#Note: there is no #define preprocessor in Python so the program will remain same as above" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example relation.c, page no. 53" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input two numbers\"\n", + "i = int(raw_input(\"num1: \"))\n", + "j = int(raw_input(\"num2: \"))\n", + "if (i == j):\n", + " print \"They are equal.\"\n", + "print \"Got the message?\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num1: 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num2: 34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "They are equal.\n", + "Got the message?\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example char.c, page no. 54" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = 255\n", + "d = -1\n", + "if c < 0:\n", + " print \"c is less than 0\"\n", + "else:\n", + " print \"c is not less than 0\"\n", + "if d < 0:\n", + " print \"d is less than 0\"\n", + "else:\n", + " print \"d is not less than 0\"\n", + " \n", + "#the program will not have output as that given in the textbook\n", + "#because, there is no concept of char, signed or unsigned values in Python\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c is not less than 0\n", + "d is less than 0\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example char8.c, page no. 55" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = '255'\n", + "d = '-1'\n", + "if c < 0:\n", + " print \"c is less than 0\"\n", + "else:\n", + " print \"c is not less than 0\"\n", + "if d < 0:\n", + " print \"d is less than 0\"\n", + "else:\n", + " print \"d is not less than 0\"\n", + "\n", + "#the program will not have output as that given in the textbook\n", + "#because, there is no concept of char, signed or unsigned values in Python" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c is not less than 0\n", + "d is not less than 0\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example extract.c, page no. 59" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = int(raw_input(\"Input an integer: \"))\n", + "n = int(raw_input(\"Bit position to extract: \"))\n", + "bit = (i >> n)&1\n", + "print \"The bit is: %d\" %bit" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input an integer: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bit position to extract: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The bit is: 1\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example max.c, page no. 62" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input two numbers\"\n", + "i = int(raw_input(\"num1: \"))\n", + "j = int(raw_input(\"num2: \"))\n", + "larger = i if i > j else j\n", + "print \"The larger of the two is %d\" %larger" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num1: 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num2: 45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The larger of the two is 45\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example interest.c, page no. 65" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "p = float(raw_input(\"Principal: \"))\n", + "r = float(raw_input(\"Rate: \"))\n", + "t = float(raw_input(\"Time: \"))\n", + "ncmp_year = float(raw_input(\"Compoundings per year: \"))\n", + "simple = p * r * t / 100\n", + "cmp_amount = p * pow(1 + r / (ncmp_year * 100), ncmp_year * t)\n", + "compound = cmp_amount - p\n", + "print \"The simple interest is : %f\" %simple\n", + "print \"The compound interest is : %f\" %compound" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Principal: 1000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Time: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Compoundings per year: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The simple interest is : 200.000000\n", + "The compound interest is : 218.402898\n" + ] + } + ], + "prompt_number": 35 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.10, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = float(raw_input(\"Enter temperature in celsius: \"))\n", + "f = 1.8 * c + 32\n", + "print \"Equivalent fahrenheit = %f\" %f\n", + "f = float(raw_input(\"Enter temperature in fahrenheit: \"))\n", + "c = (f - 32) / 1.8\n", + "print \"Equivalent celsius = %f\" %c\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter temperature in celsius: 10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Equivalent fahrenheit = 50.000000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter temperature in fahrenheit: 50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Equivalent celsius = 10.000000\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.11, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "print \"Enter the 3 sides: \"\n", + "a = float(raw_input(\"side1:\"))\n", + "b = float(raw_input(\"side2:\"))\n", + "c = float(raw_input(\"side3:\"))\n", + "peri = a + b + c\n", + "s = peri / 2\n", + "area = math.sqrt(s * (s - a) * (s - b) * (s - c))\n", + "print \"Area of the triangle is %f\" %area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the 3 sides: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "side1:4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "side2:5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "side3:6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of the triangle is 9.921567\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.12, page no. 67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "b = float(raw_input(\"Enter the base: \"))\n", + "h = float(raw_input(\"Enter the height: \"))\n", + "area = b * h / 2\n", + "print \"Area of the triangle is %f\" %area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the base: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the height: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of the triangle is 3.000000\n" + ] + } + ], + "prompt_number": 40 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter3.ipynb b/Mastering_C/chapter3.ipynb new file mode 100644 index 00000000..9d7fda28 --- /dev/null +++ b/Mastering_C/chapter3.ipynb @@ -0,0 +1,584 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:48110bf73660634c54f30da35ad71ab95825c100a9ad798c30a92b3d23049e8e" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3: Basic Input-Output" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example getchar.c, page no. 79" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = raw_input(\"Type one character: \")\n", + "print \"The character you typed is: %c\" %c" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Type one character: w\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The character you typed is: w\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example gets.c, page no. 80" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "s = raw_input(\"Type a string (Less than 80 characters):\")\n", + "print \"The string typed is: %s\" %s" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Type a string (Less than 80 characters):C Book\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The string typed is: C Book\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.1, page no. 81" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "f_var = 10.12576893\n", + "print \"Output 1: %f\" %f_var\n", + "print \"Output 2: %20f\" %f_var\n", + "print \"Output 3: %-20f\" %f_var\n", + "print \"Output 4: %+f\" %f_var\n", + "wid_prec = int(raw_input(\"Input the width: \"))\n", + "print \"Output 5: %*f\" %(wid_prec,f_var)\n", + "print \"Output 6: %.5f\" %f_var\n", + "wid_prec = int(raw_input(\"Input the precision: \"))\n", + "print \"Output 7: %20.*f\" %(wid_prec,f_var)\n", + "print \"Output 8: %-+20.5f\" %(f_var)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output 1: 10.125769\n", + "Output 2: 10.125769\n", + "Output 3: 10.125769 \n", + "Output 4: +10.125769\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the width: 13\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output 5: 10.125769\n", + "Output 6: 10.12577\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the precision: 8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output 7: 10.12576893\n", + "Output 8: +10.12577 \n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf1.c, page no. 84" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input an integer followed by a floating-point number:\"\n", + "int_var = int(raw_input(\"integer:\"))\n", + "float_var=float(raw_input(\"float:\"))\n", + "print \"The Integer: %d\" %int_var\n", + "print \"The floating-point number: %f\" %float_var" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input an integer followed by a floating-point number:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "integer:34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "float:56.57\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Integer: 34\n", + "The floating-point number: 56.570000\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf2.c, page no. 85" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#counting the inputs from user using raw_input() is not possible in Python. Hence, skipping the example" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf3.c, page no. 86" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input the day, followed by the date (dd-mm-yyyy):\"\n", + "day = raw_input(\"day:\")\n", + "date = int(raw_input(\"date:\"))\n", + "month = int(raw_input(\"month:\"))\n", + "year = int(raw_input(\"year:\"))\n", + "print \"Day: %s\" %day\n", + "print \"Date: %s\" %date\n", + "print \"Month: %s\" %month\n", + "print \"Year: %s\" %year" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the day, followed by the date (dd-mm-yyyy):\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "day:Sunday\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "date:27\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "month:1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "year:1974\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: Sunday\n", + "Date: 27\n", + "Month: 1\n", + "Year: 1974\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf4.c, page no. 87" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enter two strings: \"\n", + "string1 = raw_input(\"string1:\")\n", + "string2 = raw_input(\"string2:\")\n", + "print \"You have input:\"\n", + "print string1\n", + "print string2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two strings: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "string1:Hello\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "string2:world\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have input:\n", + "Hello\n", + "world\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf5.c, page no. 88" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#getting input from user the way it is given in textbook is not possible in Python.\n", + "#Hence, output will be different compared to textbook.\n", + "\n", + "print \"Input two string: \"\n", + "string1 = raw_input(\"String 1: \")\n", + "string2 = raw_input(\"String 2: \")\n", + "\n", + "print \"The two strings are: %4s\" %string1\n", + "print \"The two strings are: %5s\" %string2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two string: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "String 1: Morning\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "String 2: Comes\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The two strings are: Morning\n", + "The two strings are: Comes\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf6.c, page no. 89" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input the date:\"\n", + "date = int(raw_input(\"date:\"))\n", + "separator1 = raw_input(\"separator:\")\n", + "month = int(raw_input(\"month:\"))\n", + "separator2 = raw_input(\"separator:\")\n", + "year = int(raw_input(\"year:\"))\n", + "print \"Date: %d\" %date\n", + "print \"Month: %d\" %month\n", + "print \"Year: %d\" %year" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the date:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "date:31\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "separator:-\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "month:12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "separator:/\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "year:1999\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Date: 31\n", + "Month: 12\n", + "Year: 1999\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf7.c, page no. 90" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input the date:\"\n", + "date = int(raw_input(\"date:\"))\n", + "month = int(raw_input(\"month:\"))\n", + "year = int(raw_input(\"year:\"))\n", + "print \"Date: %d\" %date\n", + "print \"Month: %d\" %month\n", + "print \"Year: %d\" %year" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the date:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "date:31\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "month:12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "year:1999\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Date: 31\n", + "Month: 12\n", + "Year: 1999\n" + ] + } + ], + "prompt_number": 9 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter4.ipynb b/Mastering_C/chapter4.ipynb new file mode 100644 index 00000000..fdd2c8c4 --- /dev/null +++ b/Mastering_C/chapter4.ipynb @@ -0,0 +1,1567 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:afd37f358b4faba0dd82fd8fdf176be8389b2a27c7cdec9882b3da534d4a3ec8" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Control Structures" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example if1.c, page no. 101" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = int(raw_input(\"Enter a number: \")) \n", + "if i % 3 == 0 :\n", + " print \"Number entered is divisible by 3.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number: 30\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number entered is divisible by 3.\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf8.c, page no. 102" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#The output in the textbok is 0.000000 because it was not able to convert string to float in C\n", + "#Hence, by default it stored 0.0000000. In Python the same would give error. Hence, the answer differs\n", + "\n", + "years = float(raw_input(\"Input your age in years: \"))\n", + "secs = years * 365 * 24 * 60 * 60\n", + "print \"You have livid for %f seconds \" %secs" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input your age in years: 25\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have livid for 788400000.000000 seconds \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example if2.c, page no. 102" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#the way it is given in textbook is not possible in Python. Doing it in different manner\n", + "\n", + "print \"Input your age in years: \",\n", + "years =(raw_input())\n", + "try:\n", + " float(years)\n", + " secs = float(years) * 365 * 24 * 60 * 60\n", + " print \"you have lived for %f seconds\" %secs\n", + "except:\n", + " print \"the input is not a floating-point number\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input your age in years: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " you have lived for 2365200000.000000 seconds\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example if3.c, page no. 103" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#the program will remain same as the previous one\n", + "\n", + "print \"Input your age in years: \",\n", + "years =(raw_input())\n", + "try:\n", + " float(years)\n", + " secs = float(years) * 365 * 24 * 60 * 60\n", + " print \"you have lived for %f seconds\" %secs\n", + "except:\n", + " print \"the input is not a floating-point number\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input your age in years: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "sundeep\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " the input is not a floating-point number\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example large.c, page no. 105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input a b c\"\n", + "a = float(raw_input(\"a=\"))\n", + "b = float(raw_input(\"b=\"))\n", + "c = float(raw_input(\"c=\"))\n", + "big = a\n", + "if b>big :\n", + " big = b\n", + "if c>big :\n", + " big = c\n", + "print \"Largest of the three numbers = %7.2f\" %big" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input a b c\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a=10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "b=20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "c=45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Largest of the three numbers = 45.00\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example ifelse1.c, page no. 105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#the program will remain same as the previous one\n", + "\n", + "print \"Input your age in years: \",\n", + "years =(raw_input())\n", + "try:\n", + " float(years)\n", + " secs = float(years) * 365 * 24 * 60 * 60\n", + " print \"you have lived for %f seconds\" %secs\n", + "except:\n", + " print \"the input is not a floating-point number\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input your age in years: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " you have lived for 3153600000.000000 seconds\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example ifelse2.c, page no. 107" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "input1 = int(raw_input(\"Input an integer: \"))\n", + "if input1 :\n", + " print \"It is non-zero.\"\n", + "else :\n", + " print \"it is zero.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input an integer: 12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "It is non-zero.\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scope.c, page no. 112" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#The block given in the textbook is not possible in Python. Here the value will remain the same.\n", + "\n", + "i = 10\n", + "print \"In main. i is %d \" %i\n", + "i = 20\n", + "print \"In compound statement. i is %d\" %i\n", + "i = i+1\n", + "print \"After incrementing: i is %d\" %i\n", + "print \"In main again. i is %d\" %i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In main. i is 10 \n", + "In compound statement. i is 20\n", + "After incrementing: i is 21\n", + "In main again. i is 21\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example for.c, page no. 113" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for i in range(1, 11):\n", + " print i*5," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 10 15 20 25 30 35 40 45 50\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example sumsq1.c, page no. 115" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sum = 0\n", + "sum_of_squares = 0\n", + "for i in range(2, 31, 2):\n", + " sum += i\n", + " sum_of_squares += i*i\n", + "print \"Sum of first 15 positive even numbers = \", sum\n", + "print \"Sum of their squares = \", sum_of_squares" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum of first 15 positive even numbers = 240\n", + "Sum of their squares = 4960\n" + ] + } + ], + "prompt_number": 27 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example sumsq2.c, page no. 116" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sum = 0\n", + "sum_of_squares = 0\n", + "for i in range(30, 1, -2):\n", + " sum += i\n", + " sum_of_squares += i*i\n", + "print \"Sum of first 15 positive even numbers = \", sum\n", + "print \"Sum of their squares = \", sum_of_squares" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum of first 15 positive even numbers = 240\n", + "Sum of their squares = 4960\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example for2.c, page no. 116" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for i in range(1, 11):\n", + " print 5*i," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 10 15 20 25 30 35 40 45 50\n" + ] + } + ], + "prompt_number": 30 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.1, page no. 117" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#error in textbook, i should be initialized by 1\n", + "\n", + "sign = 1\n", + "print \"Input X and N: \"\n", + "x = float(raw_input(\"X: \"))\n", + "n = int(raw_input(\"N: \"))\n", + "x *= 3.1412/180.0\n", + "sum, t = x, x\n", + "for i in range(1, n):\n", + " sign *=-1\n", + " t = sign * x * x / (2*i*(2*i+1))\n", + " sum += t\n", + "print \"SIN(X) = %6.2f\" %sum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input X and N: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "X: 60\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N: 12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "SIN(X) = 0.90\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.2, page no. 118" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num = 0\n", + "sum = 0\n", + "print \"Input the marks, -1 at the end\"\n", + "i = int(raw_input())\n", + "while i != -1:\n", + " sum += i\n", + " num += 1\n", + " i = int(raw_input())\n", + "average = sum / num\n", + "print \"The average is %.2f\" %average" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the marks, -1 at the end\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "16\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "18\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The average is 15.00\n" + ] + } + ], + "prompt_number": 34 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example binary.c, page no. 120" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "binary = raw_input(\"Input the binary number: \")\n", + "decimal = int(binary, 2)\n", + "print \"The decimal equivalent of binary number %s is %d\" %(binary, decimal)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the binary number: 1101\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The decimal equivalent of binary number 1101 is 13\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example dowhile.c, page no. 122" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#there is no do while loop in Python. We will use simple while loop\n", + "inchar = ''\n", + "while(inchar != 'y' and inchar != 'n'):\n", + " inchar = raw_input(\"Input y or n: \")\n", + "if inchar == 'y':\n", + " print \"you pressed y.\"\n", + "if inchar == 'n':\n", + " print \"you pressed n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input y or n: j\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input y or n: j\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input y or n: n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "you pressed n\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.4, page no. 123" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num = int(raw_input(\"Input the number: \"))\n", + "n = num\n", + "sum = 0\n", + "rev = 0\n", + "while(num != 0):\n", + " digit = num % 10\n", + " sum += digit\n", + " rev = rev * 10 + digit\n", + " num /= 10\n", + "print \"Sum of the digits of the number = %4d\" %sum\n", + "print \"Reverse of the number = %7d\" %rev\n", + "if n == rev:\n", + " print \"The number is a palindrome\"\n", + "else:\n", + " print \"The number is not a palindrome\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the number: 1221\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum of the digits of the number = 6\n", + "Reverse of the number = 1221\n", + "The number is a palindrome\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example iflese.c, page no. 127" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "print \"Choice of destinations: \"\n", + "print \"\\t1 - Mercury\"\n", + "print \"\\t2 - Venus\"\n", + "print \"\\t3 - Mars\"\n", + "choice = int(raw_input(\"Enter the number corresponding to your choice: \"))\n", + "if choice == 1:\n", + " print \"Mercury is closest to the sun.\"\n", + " print \"So the weather may be quite hot there.\"\n", + " print \"The journey will cost you 5200 IGCs.\"\n", + "elif choice == 2:\n", + " print \"Venus is the second planet from the sun.\"\n", + " print \"The weather may be hot and poisonous.\"\n", + " print \"The journey will cost you 3200 IGCs.\"\n", + "elif choice == 3:\n", + " print \"Mars is the closest planet to the earth.\"\n", + " print \"The weather has been excellent till now.\"\n", + " print \"The journey will cost you 1200 IGCs.\"\n", + "else:\n", + " \"Unknown destination. Unpredictable IGCs\"\n", + "print \"Note: IGC = Ingeer-Galactic Currency\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Choice of destinations: \n", + "\t1 - Mercury\n", + "\t2 - Venus\n", + "\t3 - Mars\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number corresponding to your choice: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Venus is the second planet from the sun.\n", + "The weather may be hot and poisonous.\n", + "The journey will cost you 3200 IGCs.\n", + "Note: IGC = Ingeer-Galactic Currency\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.5, page no. 128" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#there is no switch case in Python, hence, no use of break in if conditions. So, the program will remain same\n", + "\n", + "print \"Choice of destinations: \"\n", + "print \"\\t1 - Mercury\"\n", + "print \"\\t2 - Venus\"\n", + "print \"\\t3 - Mars\"\n", + "choice = int(raw_input(\"Enter the number corresponding to your choice: \"))\n", + "if choice == 1:\n", + " print \"Mercury is closest to the sun.\"\n", + " print \"So the weather may be quite hot there.\"\n", + " print \"The journey will cost you 5200 IGCs.\"\n", + "elif choice == 2:\n", + " print \"Venus is the second planet from the sun.\"\n", + " print \"The weather may be hot and poisonous.\"\n", + " print \"The journey will cost you 3200 IGCs.\"\n", + "elif choice == 3:\n", + " print \"Mars is the closest planet to the earth.\"\n", + " print \"The weather has been excellent till now.\"\n", + " print \"The journey will cost you 1200 IGCs.\"\n", + "else:\n", + " \"Unknown destination. Unpredictable IGCs\"\n", + "print \"Note: IGC = Ingeer-Galactic Currency\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Choice of destinations: \n", + "\t1 - Mercury\n", + "\t2 - Venus\n", + "\t3 - Mars\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number corresponding to your choice: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mars is the closest planet to the earth.\n", + "The weather has been excellent till now.\n", + "The journey will cost you 1200 IGCs.\n", + "Note: IGC = Ingeer-Galactic Currency\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.6, page no. 130" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input a b & c\"\n", + "a = float(raw_input(\"a: \"))\n", + "b = float(raw_input(\"b: \"))\n", + "c = float(raw_input(\"c: \"))\n", + "if a != 0:\n", + " disc = b * b - 4 * a * c\n", + " print \"Discriminant = %5.2f\" %disc\n", + " if disc < 0:\n", + " k = 1\n", + " elif disc == 0:\n", + " k = 2\n", + " elif disc > 0:\n", + " k = 3\n", + " if k == 1:\n", + " print \"Roots are imaginary\"\n", + " real = -b/(2*a)\n", + " disc = -disc\n", + " num = pow(disc, 0.5)\n", + " imag = num/(2*a)\n", + " print \"Root1 = %5.2f +j %5.2f\" %(real, imag)\n", + " print \"Root2 = %5.2f -j %5.2f\" %(real, imag)\n", + " elif k == 2:\n", + " print \"Roots are real equal\"\n", + " root1 = -b/(2*a)\n", + " print \"Root1 = Root2 = %7.2f\" %root1\n", + " elif k == 3:\n", + " print \"Roots are real and unequal\"\n", + " root1 = (-b + sqrt(disc))/(2*a)\n", + " root2 = (-b - sqrt(disc))/(2*a)\n", + " print \"Root1 = %7.2f Root2 = %7.2f\" %(root1, root2)\n", + "else:\n", + " print \"Equation is linear\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input a b & c\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "b: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "c: 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Discriminant = -24.00\n", + "Roots are imaginary\n", + "Root1 = -1.00 +j 2.45\n", + "Root2 = -1.00 -j 2.45\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.7, page no. 132" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sum = 0\n", + "for i in range(5):\n", + " num = int(raw_input(\"Enter an integer: \"))\n", + " if num < 0:\n", + " print \"You have entered a negative number\"\n", + " continue\n", + " sum += num\n", + "print \"The sum of positive integers entered = \", sum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an integer: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an integer: -15\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have entered a negative number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an integer: 15\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an integer: -100\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have entered a negative number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an integer: 30\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of positive integers entered = 55\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.8, page no. 133" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input X & N\"\n", + "x = float(raw_input(\"X: \"))\n", + "n = int(raw_input(\"N: \"))\n", + "t = 1\n", + "sum = 1\n", + "for i in range(1, n):\n", + " ifact = i\n", + " t = t*x/ifact\n", + " sum +=t\n", + "print \"E raies to power x = %10.4f\" %sum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input X & N\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "X: 1.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "E raies to power x = 2.0000\n" + ] + } + ], + "prompt_number": 31 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.9, page no. 134" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "n = int(raw_input(\"Enter the number: \"))\n", + "if n == 0:\n", + " print \"The factorial of 0 is 1\"\n", + "else:\n", + " fact = 1\n", + " for i in range(1, n+1):\n", + " fact = fact * i\n", + " print \"Factorial of %4d is %5d\" %(n, fact)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number: 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial of 5 is 120\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.10, page no. 135" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "binom = 1\n", + "q = 0\n", + "p = int(raw_input(\"Input the number of rows: \"))\n", + "print \"Pascal Triangle\"\n", + "while q < p:\n", + " for r in range(40-3*q, 0, -1):\n", + " print \" \",\n", + " for x in range(0, q+1):\n", + " if x == 0 or q == 0:\n", + " binom = 1\n", + " else:\n", + " binom = (binom*(q-x+1))/x\n", + " print \"%6d\" %binom,\n", + " print \"\\n\"\n", + " q+=1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input the number of rows: 9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Pascal Triangle\n", + " 1 \n", + "\n", + " 1 1 \n", + "\n", + " 1 2 1 \n", + "\n", + " 1 3 3 1 \n", + "\n", + " 1 4 6 4 1 \n", + "\n", + " 1 5 10 10 5 1 \n", + "\n", + " 1 6 15 20 15 6 1 \n", + "\n", + " 1 7 21 35 35 21 7 1 \n", + "\n", + " 1 8 28 56 70 56 28 8 1 \n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.11, page no. 136" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "print \"Enter 1 for triangle(3 sides) & 2 for triangle(base, ht): \",\n", + "k = int(raw_input())\n", + "if k == 1:\n", + " print \"Enter 3 sides - a,b and c\"\n", + " a = float(raw_input(\"a: \"))\n", + " b = float(raw_input(\"b: \"))\n", + " c = float(raw_input(\"c: \"))\n", + " p = a + b + c\n", + " s = (a + b + c)/2\n", + " area = math.sqrt(s*(s-a)*(s-b)*(s-c))\n", + " print \"Perimeter = %6.2f\" %p\n", + " print \"Area of triangle = %7.2f\" %area\n", + "elif k == 2:\n", + " print \"Enter height and base\"\n", + " height = float(raw_input(\"height: \"))\n", + " base = float(raw_input(\"base: \"))\n", + " area = (base*height)/2\n", + " print \"Area of triangle = %7.2f\" %area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 1 for triangle(3 sides) & 2 for triangle(base, ht): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter 3 sides - a,b and c\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a: 4.55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "b: 5.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "c: 6.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Perimeter = 15.55\n", + "Area of triangle = 11.11\n" + ] + } + ], + "prompt_number": 47 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.12, page no. 137" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "print \"Enter the no of lines in the pyramid: \"\n", + "t = int(raw_input())\n", + "print \"\\t\\tREVERSED PYRAMID OF DIGITS\"\n", + "k = 0\n", + "for p in range(t, 0, -1):\n", + " k += 1\n", + " sys.stdout.write (\" \")\n", + " for r in range(0, (k*4)+1):\n", + " sys.stdout.write (\" \")\n", + " for q in range(p, (2*p)-1): \n", + " sys.stdout.write (\"%4d\" %q)\n", + " for s in range((2*p)-2, p-1, -1):\n", + " sys.stdout.write(\"%4d\\b\" %s)\n", + " sys.stdout.write (\"\\n\")\n", + "print \" %d\" %p\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the no of lines in the pyramid: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\t\tREVERSED PYRAMID OF DIGITS\n", + " 7 8 9 10 11 12 12\b 11\b 10\b 9\b 8\b 7\b\n", + " 6 7 8 9 10 10\b 9\b 8\b 7\b 6\b\n", + " 5 6 7 8 8\b 7\b 6\b 5\b\n", + " 4 5 6 6\b 5\b 4\b\n", + " 3 4 4\b 3\b\n", + " 2 2\b\n", + " \n", + " 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.13, page no. 137" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "n = 20\n", + "x = float(raw_input(\"Input x: \"))\n", + "x = x*3.1412/180\n", + "t = 1\n", + "sum = 1\n", + "for i in range(1, n+1):\n", + " t = t*pow(-1, (2*i-1))*x*x/(2*i*(2*i-1))\n", + " sum += t\n", + "print \"COS(X) = %7.3f\" %sum" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input x: 90.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "COS(X) = 0.000\n" + ] + } + ], + "prompt_number": 62 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.14, page no. 138" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "n = int(raw_input(\"Enter the number of lines: \"))\n", + "for p in range(1, n+1):\n", + " for i in range(1, n-p):\n", + " print \" \",\n", + " m = p\n", + " for q in range(1, p+1):\n", + " print \"%4d\" %m,\n", + " m +=1\n", + " m = m - 2\n", + " for q in range(1, p):\n", + " print \"%4d\" %m,\n", + " m-=1\n", + " print \"\\n\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of lines: 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1 \n", + "\n", + "\n", + " 2 3 2 \n", + "\n", + "\n", + " 3 4 5 4 3 \n", + "\n", + "\n", + " 4 5 6 7 6 5 4 \n", + "\n", + "\n", + " 5 6 7 8 9 8 7 6 5 \n", + "\n", + "\n", + " 6 7 8 9 10 11 10 9 8 7 6 \n", + "\n", + "\n", + " 7 8 9 10 11 12 13 12 11 10 9 8 7 \n", + "\n", + "\n" + ] + } + ], + "prompt_number": 67 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter5.ipynb b/Mastering_C/chapter5.ipynb new file mode 100644 index 00000000..277c10b6 --- /dev/null +++ b/Mastering_C/chapter5.ipynb @@ -0,0 +1,1515 @@ +{ + "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": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter6.ipynb b/Mastering_C/chapter6.ipynb new file mode 100644 index 00000000..99bf0cd9 --- /dev/null +++ b/Mastering_C/chapter6.ipynb @@ -0,0 +1,603 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:2b98cee86e6322a41a7dca7a85df32b68e0995a4fde2b6249b408a952c9387e4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Scope & Extent" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scope1.c, page no. 202" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#as mentioned in the textbook, the statement j = 30 will not give an error as in Python you can declare a vairable anywhere\n", + "#however, the statement does not refer to the variable j of the function.\n", + "def func():\n", + " j = 20\n", + " print \"j is a variable in the function\"\n", + " print \"Value of j is \",j\n", + "i = 10\n", + "print \"i is a variable in main.\"\n", + "print \"Value of i is \", i\n", + "print \"Calling func...\"\n", + "func()\n", + "j = 30" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i is a variable in main.\n", + "Value of i is 10\n", + "Calling func...\n", + "j is a variable in the function\n", + "Value of j is 20\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example global.c, page no. 202" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "g = 0\n", + "def func():\n", + " global g\n", + " print \"In func. g is visbile here, since it is global\"\n", + " print \"Incrementing g in func...\"\n", + " g += 1\n", + " return g\n", + "print \"In main. g is visible here, since it is global\"\n", + "print \"assigning 20 to g in main...\"\n", + "g = 20\n", + "print \"Callint func...\"\n", + "func()\n", + "print \"func returned g is \", g" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In main. g is visible here, since it is global\n", + "assigning 20 to g in main...\n", + "Callint func...\n", + "In func. g is visbile here, since it is global\n", + "Incrementing g in func...\n", + "func returned g is 21\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example colcolon.c, page no. 203" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#there is no concept of scope resolution operator in Python. we will do it in different way\n", + "a = 10\n", + "def func():\n", + " global a\n", + " b = 20\n", + " print b\n", + " print a\n", + "func()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n", + "10\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.1, page no. 204" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#there is no concept of block in Python. writing if True is smiliar to it but still, output will differ\n", + "j = 144\n", + "print \"The value of j outside the block is \", j\n", + "if True:\n", + " j = 12\n", + " print \"The value of j inside the block is \", j\n", + "print \"The value of j outside the block is \", j" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The value of j outside the block is 144\n", + "The value of j inside the block is 12\n", + "The value of j outside the block is 12\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.2, page no. 205" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = 144\n", + "j = 132\n", + "print \"i = \", i\n", + "if True:\n", + " k = 12\n", + " k = int(raw_input())\n", + " i = i%k\n", + "if i == 0:\n", + " print \"i is a divisor of %d \" %k" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i = 144\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "25\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.3, page no. 205" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "iNum = 5\n", + "jNum = 30\n", + "if True:\n", + " iNum = 10\n", + " print \"%d %d\" %(iNum, jNum)\n", + "print \"%d %d\" %(iNum, jNum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10 30\n", + "10 30\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7, page no. 207" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "cName = raw_input(\"Enter a string: \")\n", + "print \"The reverse of the string is: \"\n", + "print cName[::-1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a string: this is a string\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The reverse of the string is: \n", + "gnirts a si siht\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.4 pageno : 206" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "iGlobal = 35\n", + "Number = 47\n", + "iVal = 99\n", + "Number = 1000\n", + "print \"The value of local variable Number is %d\"%Number\n", + "print \"The value of global variable iGlobal is %d\"%iGlobal\n", + "print \"The value of global variable Number is %d\"%Number\n", + "print \"The value of static global variable iVal is %d\"%iVal\n", + "\n", + "# Python has no static functionality. and also local variable changes value when value assigned to it." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The value of local variable Number is 1000\n", + "The value of global variable iGlobal is 35\n", + "The value of global variable Number is 1000\n", + "The value of static global variable iVal is 99\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.5, page no. 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Count = 1\n", + "def PrintCount():\n", + " global Count\n", + " print \"Count = %d\" %Count\n", + " Count = Count + 1\n", + "PrintCount()\n", + "PrintCount()\n", + "PrintCount()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Count = 1\n", + "Count = 2\n", + "Count = 3\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.6, page no.208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "uF1 = 0\n", + "uF2 = 1\n", + "def PrintNextFibo():\n", + " global uF1,uF2\n", + " uFpre = uF1 + uF2\n", + " print \"%d\" %uFpre ,\n", + " uF2 = uF1\n", + " uF1 = uFpre\n", + "print \"Fibonacci sequences using static variables :\"\n", + "print \"Enter how many number are to be generated :\",\n", + "itotNum = int(raw_input())\n", + "print \"The first %d fibonacci numbers are\" %itotNum\n", + "for iIndex in range(1,itotNum+1):\n", + " PrintNextFibo()\n", + "\n", + "# it starts with zero but here function created called uses global variable so we need to print/assume manually zero value." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fibonacci sequences using static variables :\n", + "Enter how many number are to be generated :" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The first 5 fibonacci numbers are\n", + "1 1 2 3 5\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 209" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import linsrch\n", + "import bubble\n", + "arr = []\n", + "num = 5\n", + "i = 0\n", + "pos = 1\n", + "print \"Enter five elements\"\n", + "while i < num:\n", + " arr.append(int(raw_input()))\n", + " i+=1\n", + "print \"Enter element to be searched: \",\n", + "key = int(raw_input())\n", + "pos = linsrch.LinSrch(arr, num, key)\n", + "if pos:\n", + " print \"Element %d found at %d position\" %(key, pos)\n", + "else:\n", + " print \"Ellement %d not found\" %key\n", + "bubble.BubSort(arr, num)\n", + "print \"The sorted element are\"\n", + "for ele in arr:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter five elements\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "56\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "48\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter element to be searched: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Element 22 found at 5 position\n", + "The sorted element are\n", + "12 15 22 48 56\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import myglobal\n", + "globalVar = 10\n", + "print \"globalVar before calling = \", globalVar\n", + "myglobal.inc_global(globalVar)\n", + "print \"globalVar after calling \", globalVar" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "globalVar before calling = 10\n", + "globalVar after calling 10\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 212" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import myglobal\n", + "\n", + "globalVar = 10\n", + "print \"globalVar before calling = \", globalVar\n", + "g = myglobal.inc_global(globalVar)\n", + "print \"globalVar after calling \", g" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "globalVar before calling = 10\n", + "globalVar after calling 11\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter7.ipynb b/Mastering_C/chapter7.ipynb new file mode 100644 index 00000000..e41cbc36 --- /dev/null +++ b/Mastering_C/chapter7.ipynb @@ -0,0 +1,2482 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:df255fa9a4e53028d13370030da5fb9293c0b07c60111131baf99b22bd751792" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7 : Array and Strings" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1, page no. 221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sum = 0\n", + "a1 = int(raw_input(\"Age:\"))\n", + "sum += a1\n", + "a2 = int(raw_input(\"Age:\"))\n", + "sum += a2\n", + "a3 = int(raw_input(\"Age:\"))\n", + "sum += a3\n", + "a4 = int(raw_input(\"Age:\"))\n", + "sum += a4\n", + "a5 = int(raw_input(\"Age:\"))\n", + "sum += a5\n", + "print \"The ages that were input are:\"\n", + "print a1\n", + "print a2\n", + "print a3\n", + "print a4\n", + "print a5\n", + "print \"Average = %d\" %(sum/5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ages that were input are:\n", + "10\n", + "20\n", + "30\n", + "40\n", + "50\n", + "Average = 30\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2, page no. 222" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sum = 0.0\n", + "age = []\n", + "n = int(raw_input(\"Number of persons:\"))\n", + "if n<=0 or n>5:\n", + " print \"Invalid number of person entered\"\n", + "else:\n", + " for i in range(n):\n", + " age.append(int(raw_input(\"Age :\")))\n", + " sum += age[i]\n", + " print \"The ages input are:\"\n", + " for i in range (n):\n", + " print age[i]\n", + " print \"The average is :\",(sum/n)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of persons:5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ages input are:\n", + "10\n", + "20\n", + "30\n", + "40\n", + "50\n", + "The average is : 30.0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.1, page no. 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "n = int(raw_input(\"Size of vector ? \"))\n", + "print \"Vector elements ? \"\n", + "for i in range (n):\n", + " a.append(float(raw_input()))\n", + "large = max(a)\n", + "small = min(a)\n", + "print \"Largest element in vector is %8.2f\" %large\n", + "print \"Smallest element in vector is %8.2f\" %small" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ? 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements ? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-9.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-6.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "36.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Largest element in vector is 36.00\n", + "Smallest element in vector is -9.00\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.2, page no. 226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "n = int(raw_input(\"Size of vector ? \"))\n", + "print \"Vector elements ? \"\n", + "for i in range (n):\n", + " a.append(float(raw_input()))\n", + "for i in range (n-1):\n", + " for j in range(i+1,n):\n", + " if a[i]> a[j]:\n", + " a[i],a[j]=a[j],a[i]\n", + "print \"Vector elements in ascending order :\"\n", + "for i in range(n):\n", + " print \"%8.2f\" %a[i]\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ? 8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements ? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-2.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements in ascending order :\n", + " -2.00\n", + " 1.00\n", + " 6.00\n", + " 7.00\n", + " 11.00\n", + " 20.00\n", + " 34.00\n", + " 91.00\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.3, page no. 227" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "n = int(raw_input(\"Size of vector ? \"))\n", + "print \"Vector elements ? \"\n", + "for i in range (n):\n", + " a.append(float(raw_input()))\n", + "item = float(raw_input(\"Element to be inserted ?\"))\n", + "pos = int(raw_input(\"Position of insertion ?\"))\n", + "a.insert(pos-1, item)\n", + "print \"Vector after insertion\"\n", + "for ele in a:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ? 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements ? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Element to be inserted ?10.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Position of insertion ?1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector after insertion\n", + "10.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example arrfunc.c, page no. 230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "rollno = 4\n", + "subject = 3\n", + "def display(studentmarks):\n", + " for r in range(rollno):\n", + " print \"\\nRoll no.: \", r+1,\n", + " for s in range(subject):\n", + " print \"%10.2f\" %studentmarks[r][s],\n", + "marks =[[35.5, 40.5, 45.5],\n", + " [50.5, 55.5, 60.5],\n", + " [65.0, 70.0, 75.5],\n", + " [80.0, 85.0, 90.0]]\n", + "display(marks)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Roll no.: 1 35.50 40.50 45.50 \n", + "Roll no.: 2 50.50 55.50 60.50 \n", + "Roll no.: 3 65.00 70.00 75.50 \n", + "Roll no.: 4 80.00 85.00 90.00\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.4, page no. 240" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "b = numpy.zeros((10, 10))\n", + "c = numpy.zeros((10, 10))\n", + "\n", + "print \"Input row & column of A matrix: \"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "\n", + "print \"Input row & column of B matrix: \"\n", + "p = int(raw_input(\"row: \"))\n", + "q = int(raw_input(\"column: \"))\n", + "\n", + "if n == p and m == q:\n", + " print \"Matrices can be added\"\n", + " print \"Input A matrix: \",\n", + " for i in range(n):\n", + " for j in range(m):\n", + " a[i][j] = int(raw_input())\n", + " print \"\"\n", + " print \"Input B matrix: \",\n", + " for i in range(p):\n", + " for j in range(q):\n", + " b[i][j] = int(raw_input())\n", + " print \"\"\n", + " for i in range(p):\n", + " for j in range(q):\n", + " c[i][j] = a[i][j] + b[i][j]\n", + " print \"Sum of A & B matrices: \"\n", + " for i in range(n):\n", + " for j in range(m):\n", + " print \"%5d\" %c[i][j],\n", + " print \"\"\n", + "else:\n", + " print \"Matricess cannot be added\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of B matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Matrices can be added\n", + "Input A matrix: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Input B matrix: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Sum of A & B matrices: \n", + " 2 4 6 \n", + " 8 10 12 \n", + " 14 16 18 \n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.5, page no. 233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "print \"Enter the order of A matrix: \",\n", + "n = int(raw_input())\n", + "print \"Input A matrix\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " a[i][j] = int(raw_input())\n", + "trace = 0\n", + "for i in range(n):\n", + " trace += a[i][i]\n", + "print \"Trace = \", trace" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the order of A matrix: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Trace = 15.0\n" + ] + } + ], + "prompt_number": 48 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.6, page no. 233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "def read_mat(a, m, n):\n", + " for i in range(m):\n", + " for j in range(n):\n", + " a[i][j] = int(raw_input())\n", + "def write_mat(a, m, n):\n", + " for i in range(m):\n", + " for j in range(n):\n", + " print \"%d\" %a[i][j],\n", + " print \"\"\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "b = numpy.zeros((10, 10))\n", + "c = numpy.zeros((10, 10))\n", + "\n", + "print \"Input row & column of A matrix: \"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "\n", + "print \"Input row & column of b matrix: \"\n", + "k = int(raw_input(\"row: \"))\n", + "q = int(raw_input(\"column: \"))\n", + "\n", + "if n == k:\n", + " print \"Matrices can be multiplied\"\n", + " print \"Resultant Matrix is %d x %d\" %(m, q)\n", + " print \"Input A matrix\"\n", + " read_mat(a, m, n)\n", + " print \"Input B matrix\"\n", + " read_mat(b, k, q)\n", + " for i in range(m):\n", + " for j in range(q):\n", + " c[i][j] = 0\n", + " for ip in range(n):\n", + " c[i][j] = c[i][j]+a[i][ip]*b[ip][j]\n", + " print \"Resultant of A and B matrices: \"\n", + " write_mat(c, m, q)\n", + "else:\n", + " print \"col of matrix A must be equal to row of matrix B\"\n", + " print \"Matrices cannot be multiplied\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of b matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Matrices can be multiplied\n", + "Resultant Matrix is 3 x 3\n", + "Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input B matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Resultant of A and B matrices: \n", + "30 36 42 \n", + "66 81 96 \n", + "102 126 150 \n" + ] + } + ], + "prompt_number": 51 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.7, page no. 235" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "\n", + "print \"Input row & column of A matrix: \"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "\n", + "print \"Input A matrix: \"\n", + "for i in range(n):\n", + " for j in range(m):\n", + " a[i][j] = int(raw_input())\n", + "print \"Transpose of matrix A is\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print \"%5d\" %a[j][i],\n", + " print \"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Transpose of matrix A is\n", + " 1 5 9 \n", + " 2 6 6 \n", + " 3 7 7 \n", + " 4 8 8 \n" + ] + } + ], + "prompt_number": 55 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.8, page no. 237" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "strc = \"This is a string literal\"\n", + "print strc" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is a string literal\n" + ] + } + ], + "prompt_number": 57 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example starray.c, page no. 238" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "names = [\"Tejaswi\",\n", + " \"Prasad\",\n", + " \"Prasanth\",\n", + " \"Prakash\",\n", + " \"Anand\",]\n", + "for name in names:\n", + " print name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tejaswi\n", + "Prasad\n", + "Prasanth\n", + "Prakash\n", + "Anand\n" + ] + } + ], + "prompt_number": 59 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strcat.c, page no. 239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "oneString = \"Fish\"\n", + "twoString = \"face\"\n", + "oneString += twoString\n", + "print oneString" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fishface\n" + ] + } + ], + "prompt_number": 63 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.9, page no. 241" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "print \"Size of vector ?\"\n", + "n = int(raw_input())\n", + "num = n\n", + "print \"Vector Elements ?\"\n", + "for i in range(n):\n", + " a.append(float(raw_input()))\n", + "a = set(a)\n", + "a = list(a)\n", + "length = len(a)\n", + "print length\n", + "if length == n:\n", + " print \"No duplicates found\"\n", + "else:\n", + " print \"Vector has %d duplicates\" %(num - length)\n", + " print \"Vector after deleting duplicates: \",\n", + " for ele in a:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector Elements ?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n", + "Vector has 2 duplicates\n", + "Vector after deleting duplicates: 1.0 2.0 3.0 4.0\n" + ] + } + ], + "prompt_number": 73 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.10, page no. 242" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "grade = ['', '', '', '', '', '', '', '', '', '', '', '', '', '']\n", + "a = numpy.zeros((50,10))\n", + "print \"Number of students ?\",\n", + "n = int(raw_input())\n", + "for i in range(n):\n", + " sum = 0.0\n", + " print \"Enter 3 socres of student %d\" %(i+1)\n", + " for j in range(3):\n", + " a[i][j] = (float(raw_input()))\n", + " sum += a[i][j]\n", + " avg = sum/3\n", + " a[i][3] = avg\n", + " if avg < 30.0:\n", + " grade[i] = 'E'\n", + " elif avg < 60.0:\n", + " grade[i] = 'D'\n", + " elif avg < 75.0:\n", + " grade[i] = 'B'\n", + " else:\n", + " grade[i] = 'A'\n", + "print \"S1.no scores AVERAGE GRADE\"\n", + "print \"---------------------------------------------\"\n", + "for i in range(n):\n", + " print i+1,\n", + " for j in range(4):\n", + " print \"%8.2f\" %a[i][j],\n", + " print \"%6c\" %grade[i],\n", + " print \"\"\n", + " print \"---------------------------------------------\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of students ?" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter 3 socres of student 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "86\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "58\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "23.9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "80\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "97\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "73\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "56.8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "47.9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "62.0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "35\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "40\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "S1.no scores AVERAGE GRADE\n", + "---------------------------------------------\n", + "1 67.00 86.00 58.00 70.33 B \n", + "---------------------------------------------\n", + "2 20.00 30.00 23.90 24.63 E \n", + "---------------------------------------------\n", + "3 80.00 97.00 73.00 83.33 A \n", + "---------------------------------------------\n", + "4 56.80 47.90 62.00 55.57 D \n", + "---------------------------------------------\n", + "5 45.00 35.00 40.00 40.00 D \n", + "---------------------------------------------\n" + ] + } + ], + "prompt_number": 85 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.11, page no. 243" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "sumsq = 0\n", + "sum = 0\n", + "print \"Calculation standard deviation of a\"\n", + "print \"Enter size of the list: \",\n", + "n = int(raw_input())\n", + "x = []\n", + "print \"Enter the %d items \" %n\n", + "for i in range(n):\n", + " x.append(float(raw_input()))\n", + " sum += x[i]\n", + "mean = sum/n\n", + "for i in range(n):\n", + " sumsq = sumsq + (mean-x[i]) * (mean-x[i])\n", + "variance = sumsq/n\n", + "sdn = math.sqrt(variance)\n", + "print \"Mean of %5d items : %10.6f\" %(n, mean)\n", + "print \"Variance : %10.6f\" %(variance)\n", + "print \"Standard Deviation : %10.6f\" %sdn" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Calculation standard deviation of a\n", + "Enter size of the list: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the 7 items \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "52\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mean of 7 items : 35.714286\n", + "Variance : 685.918367\n", + "Standard Deviation : 26.190043\n" + ] + } + ], + "prompt_number": 89 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.12, page no. 244" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "import math\n", + "\n", + "def nrm(a, n, m):\n", + " sum = 0.0\n", + " for i in range(n):\n", + " for j in range(m):\n", + " sum = sum + a[i][j] * a[i][j]\n", + " print \"Sum = %6.2f\" %sum\n", + " return math.sqrt(sum)\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "print \"Input row & column of A matrix\"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "print \"Input A matrix\"\n", + "for i in range(n):\n", + " for j in range(m):\n", + " a[i][j] = float(raw_input())\n", + "norm = nrm(a, n, m)\n", + "print \"Norm = %6.2f\" %norm" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum = 285.00\n", + "Norm = 16.88\n" + ] + } + ], + "prompt_number": 90 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.13, page no. 245" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((5, 5))\n", + "flag = True\n", + "print \"Input size of matrix\"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "print \"Enter the elements of the matrix: \"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " a[i][j] = float(raw_input())\n", + "for i in range(m):\n", + " min = a[i][0]\n", + " p = i\n", + " q = 0\n", + " for j in range(n):\n", + " if min > a[i][j]:\n", + " min = a[i][j]\n", + " p = i\n", + " q = j\n", + " for j in range(m):\n", + " if j != q:\n", + " if a[j][q] > a[p][q]:\n", + " flag = False\n", + " if flag:\n", + " print \"Saddle point a[%d][%d] = %d\" %(p+1, q+1, a[p][q])\n", + " else:\n", + " print \"No saddle is in row %d\" %(i+1)\n", + " flag = True" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input size of matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the elements of the matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Saddle point a[1][2] = 5\n", + "Saddle point a[2][2] = 5\n", + "No saddle is in row 3\n" + ] + } + ], + "prompt_number": 91 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.14, page no. 246" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "b = numpy.zeros((10, 10))\n", + "flag = None\n", + "print \"Input order of A matrix\"\n", + "n = int(raw_input(\"row: \"))\n", + "print \"Input A matrix\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " a[i][j] = int(raw_input())\n", + "for i in range(n):\n", + " for j in range(n):\n", + " b[i][j] = a[j][i]\n", + "print \"Transpose of A matrix\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " print \"%5d\" %b[i][j],\n", + " print \"\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " if a[i][j] != b[i][j]:\n", + " flag = True\n", + "if flag:\n", + " print \"Matrix is not symmetric\"\n", + "else:\n", + " print \"Matrix is symmetric\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input order of A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Transpose of A matrix\n", + " 1 3 \n", + " 2 4 \n", + "Matrix is not symmetric\n" + ] + } + ], + "prompt_number": 96 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter8.ipynb b/Mastering_C/chapter8.ipynb new file mode 100644 index 00000000..b93801ac --- /dev/null +++ b/Mastering_C/chapter8.ipynb @@ -0,0 +1,2252 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:a09d4bcf6c9368fe8d58466e4e36512ef92468a06ab7b1aa67d8417eff2354ac" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Pointers" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example getaddr.c, page no. 261" + ] + }, + { + "cell_type": "heading", + "level": 4, + "metadata": {}, + "source": [ + "Note: There is no concept of pointers in Python. Hence, output may not match for some of the examples." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 100\n", + "b = 200\n", + "c = 300\n", + "\n", + "print \"Address: %x contains value: %i\" %(a, a)\n", + "print \"Address: %x contains value: %i\" %(b, b)\n", + "print \"Address: %x contains value: %i\" %(c, c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address: 64 contains value: 100\n", + "Address: c8 contains value: 200\n", + "Address: 12c contains value: 300\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.1, page no. 263" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = 7\n", + "f = 10.5\n", + "c = 'A'\n", + "print \"%d is stored at address %u\" %(i, id(i))\n", + "print \"%f is stored at address %u\" %(f, id(f))\n", + "print \"%c is stored at address %u\" %(c, id(c))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "7 is stored at address 41297768\n", + "10.500000 is stored at address 53588120\n", + "A is stored at address 140471220157656\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.2, page no. 263" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "var1 = 10\n", + "var2 = 20\n", + "iptr = id(var1)\n", + "print \"Address and contents of var1 is: %d & %d\" %(iptr, var1)\n", + "iptr = id(var2)\n", + "print \"Address and contents of var1 is: %d & %d\" %(iptr, var2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address and contents of var1 is: 41297696 & 10\n", + "Address and contents of var1 is: 41297456 & 20\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example derefer.c, page no. 265" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#note: we have used var1 instead of iptr because there is no point of pointer in python. Hence, instead of *iptr \n", + "#we have to refer to the same variable\n", + "var1 = 0\n", + "iptr = id(var1)\n", + "var1 = 25\n", + "var1 += 10\n", + "print \"Variable var1 contains %i \" %var1\n", + "var2 = var1\n", + "print \"Variable var2 contains: %i\" %var2\n", + "iptr = id(var2)\n", + "var2 += 20\n", + "print \"Variable var2 now contains: %i\" %var2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Variable var1 contains 35 \n", + "Variable var2 contains: 35\n", + "Variable var2 now contains: 55\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.3, page no. 267" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 5\n", + "b = 10\n", + "ptr = id(a)\n", + "print \"Initial values of a & b\"\n", + "print \"a = %d\" %a\n", + "print \"b = %d\" %b\n", + "b = a\n", + "print \"Changed values of a & b\"\n", + "print \"a = %d\" %a\n", + "print \"b = %d\" %b\n", + "print \"Address of variable is \", id(a)\n", + "print \"Address of variable is \", id(b)\n", + "print \"Address assigned to pointer ptr is \", id(ptr)\n", + "print \"Value pointer ptr access is \", a" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Initial values of a & b\n", + "a = 5\n", + "b = 10\n", + "Changed values of a & b\n", + "a = 5\n", + "b = 5\n", + "Address of variable is 41297816\n", + "Address of variable is 41297816\n", + "Address assigned to pointer ptr is 51460736\n", + "Value pointer ptr access is 5\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.4, page no. 268" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def swap(d1, d2):\n", + " return d2, d1\n", + "\n", + "print \"Enter 2 real numbers:\"\n", + "data1 = float(raw_input(\"num1: \"))\n", + "data2 = float(raw_input(\"num2: \"))\n", + "\n", + "print \"Data1 contains: \", data1\n", + "print \"Data2 contains: \", data2\n", + "data1, data2 = swap(data1, data2)\n", + "print \"After swapping.....\"\n", + "print \"Data1 contains: \", data1\n", + "print \"Data2 contains: \", data2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 2 real numbers:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num1: 23.55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num2: -65.73\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data1 contains: 23.55\n", + "Data2 contains: -65.73\n", + "After swapping.....\n", + "Data1 contains: -65.73\n", + "Data2 contains: 23.55\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example voidptr.c, page no. 270" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i1 = 100\n", + "f1 = 200.5\n", + "vptr = id(i1)\n", + "print \"i1 contains \", i1\n", + "vptr = id(f1)\n", + "print \"f1 contians \", f1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i1 contains 100\n", + "f1 contians 200.5\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.5, page no. 271" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#the way it is given in the textbook is not possible as there is no concept of pointers in Python\n", + "a = []\n", + "print \"Size of array ?\"\n", + "n = int(raw_input())\n", + "print \"Array elements ?\"\n", + "for i in range(n):\n", + " a.append(int(raw_input()))\n", + "small = min(a)\n", + "print \"The smallest element is \", small" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of array ?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Array elements ?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-23\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-77\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The smallest element is -77\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.6, page no. 272" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 5\n", + "b = 10\n", + "ptr1 = id(a)\n", + "ptr2 = id(b)\n", + "\n", + "c = a + b\n", + "d = a/b*7\n", + "\n", + "print \"Addresses of variables 'a' and 'b': \"\n", + "print \"a: \", ptr1\n", + "print \"b: \", ptr2\n", + "\n", + "print \"Addresses of pointers 'ptr1' and 'ptr2' hold: \"\n", + "print \"ptr1: \", ptr1\n", + "print \"ptr2: \", ptr2\n", + "\n", + "print \"values of 'a', 'b', 'c', 'd': \"\n", + "print \"a = %2d b = %2d c = %2d d = %2d\" %(a,b,c,d)\n", + "print \"Values accessed by pointers 'ptr1' and 'ptr2': \"\n", + "print \"ptr1: \", ptr1\n", + "print \"ptr2: \", ptr2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Addresses of variables 'a' and 'b': \n", + "a: 37181336\n", + "b: 37181216\n", + "Addresses of pointers 'ptr1' and 'ptr2' hold: \n", + "ptr1: 37181336\n", + "ptr2: 37181216\n", + "values of 'a', 'b', 'c', 'd': \n", + "a = 5 b = 10 c = 15 d = 0\n", + "Values accessed by pointers 'ptr1' and 'ptr2': \n", + "ptr1: 37181336\n", + "ptr2: 37181216\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example farptr.c, page no. 275" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# not possible in Python" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example ptriptr.c, page no. 275" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "data = 0\n", + "iptr = id(data)\n", + "ptriptr = id(iptr)\n", + "data = 100\n", + "print \"Variable 'data' contains: \", data\n", + "ptriptr = 200\n", + "print \"Variable 'data' contains: \", data\n", + "#value of data won't change as there is no pointer\n", + "data = 300\n", + "print \"ptriptr is pointing to: \", data" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Variable 'data' contains: 100\n", + "Variable 'data' contains: 100\n", + "ptriptr is pointing to: 300\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 277" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "iarray = [1, 2, 3, 4, 5]\n", + "for i in range(5):\n", + " print id(iarray[i]), iarray[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "37181432 1\n", + "37181408 2\n", + "37181384 3\n", + "37181360 4\n", + "37181336 5\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 279" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def sort_it(b):\n", + " return sorted(b)\n", + "\n", + "a = []\n", + "print \"Eneter the number of elements in the array (lessthan 21): \"\n", + "n = int(raw_input())\n", + "print \"Enter the elements: \"\n", + "for i in range(n):\n", + " a.append(int(raw_input()))\n", + "arr = sort_it(a)\n", + "print \"The sorted array: \"\n", + "for ele in arr:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Eneter the number of elements in the array (lessthan 21): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the elements: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sorted array: \n", + "-4 0 1 4 7\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.7, page no. 280" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def SortByPtrExchange(person):\n", + " return sorted(person)\n", + "choice = 'y'\n", + "person = []\n", + "while choice == 'y':\n", + " print \"Enter Name: \"\n", + " person.append(raw_input())\n", + " print \"Enter another ? (y/n)\"\n", + " choice = raw_input()\n", + "print \"Unsorted list: \"\n", + "for per in person:\n", + " print per\n", + "person = SortByPtrExchange(person)\n", + "print \"Sorted List: \"\n", + "for per in person:\n", + " print per" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tejaswi\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter another ? (y/n)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Prasad\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter another ? (y/n)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Prakash\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter another ? (y/n)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sudeep\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter another ? (y/n)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Anand\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter another ? (y/n)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Unsorted list: \n", + "Tejaswi\n", + "Prasad\n", + "Prakash\n", + "Sudeep\n", + "Anand\n", + "Sorted List: \n", + "Anand\n", + "Prakash\n", + "Prasad\n", + "Sudeep\n", + "Tejaswi\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example argref.c, page no. 282" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Add10(studentmarks):\n", + " studentmarks += 10\n", + " return studentmarks\n", + "Marks = 25\n", + "print \"Value of Marks = %d\" %Marks\n", + "Marks = Add10(Marks)\n", + "print \"Value of Marks after execution = %d\" %Marks" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of Marks = 25\n", + "Value of Marks after execution = 35\n" + ] + } + ], + "prompt_number": 32 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example accfunc.c, page no. 283" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Max = 5\n", + "def Add10(studentmarks):\n", + " global Max\n", + " for i in range(Max):\n", + " studentmarks[i] += 10\n", + " return studentmarks\n", + "Marks = [25, 43, 70, 80, 76]\n", + "Marks = Add10(Marks)\n", + "for j in range(Max):\n", + " print \"Marks[%d] = %d\" %(j, Marks[j])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Marks[0] = 35\n", + "Marks[1] = 53\n", + "Marks[2] = 80\n", + "Marks[3] = 90\n", + "Marks[4] = 86\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.8, page no. 285" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def exchange(x, y):\n", + " return y, x\n", + "print \"Program swaps values of the variables 'a' and 'b'\"\n", + "print \"Values of a & b: \"\n", + "a = int(raw_input(\"a: \"))\n", + "b = int(raw_input(\"b: \"))\n", + "print \"a: %5d \\t b = %5d\" %(a, b)\n", + "x, y = exchange(a, b)\n", + "print \"After swapping: \"\n", + "print \"a = %5d \\t b = %5d\" %(x, y)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Program swaps values of the variables 'a' and 'b'\n", + "Values of a & b: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a: 78\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "b: 196\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a: 78 \t b = 196\n", + "After swapping: \n", + "a = 196 \t b = 78\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 287" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#not possible in Python. Hence, skipping" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 288" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "arr = [1, 2, 3]\n", + "ptr = id(arr)\n", + "for i in range(3):\n", + " print \"%d %d\" %(id(i), arr[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "37181456 1\n", + "37181432 2\n", + "37181408 3\n" + ] + } + ], + "prompt_number": 38 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.9, page no. 289" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "arr = [1, 2, 3, 4, 5]\n", + "for ele in arr[::-1]:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 4 3 2 1\n" + ] + } + ], + "prompt_number": 40 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.10, page no. 289" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "arr = [-1, -2, -3, -4, -5]\n", + "p = arr\n", + "arr = arr[::-1]\n", + "p = p[::-1]\n", + "for i in range(len(arr)):\n", + " print arr[i], \"\\t\", p[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "-5 \t-5\n", + "-4 \t-4\n", + "-3 \t-3\n", + "-2 \t-2\n", + "-1 \t-1\n" + ] + } + ], + "prompt_number": 42 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 290" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "ia = [2, 5, 9]\n", + "ptr = ia\n", + "for ele in ptr:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2 5 9\n" + ] + } + ], + "prompt_number": 44 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.11, page no. 291" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def FindBig(x, y):\n", + " return x if x > y else y\n", + "print \"Enter two integers: \"\n", + "a = int(raw_input(\"int1: \"))\n", + "b = int(raw_input(\"int2: \"))\n", + "big = FindBig(a, b)\n", + "print \"Variable name \\t address of the variable \\t value\"\n", + "print \"a \\t\\t %d \\t\\t\\t %d\"%(id(a), a)\n", + "print \"The bigger of the two integers\"\n", + "print \"big \\t\\t %d \\t\\t\\t %d\" %(id(big), big)\n", + "print \"The value as botained form pointer: \", big" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "int1: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "int2: 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Variable name \t address of the variable \t value\n", + "a \t\t 37181336 \t\t\t 5\n", + "The bigger of the two integers\n", + "big \t\t 37181288 \t\t\t 7\n", + "The value as botained form pointer: 7\n" + ] + } + ], + "prompt_number": 47 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example string1.c, page no. 292" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "city = \"Bangalore\"\n", + "i = 0\n", + "print \"Address \\t\\t\\t\\t Contents\"\n", + "for i in range(len(city)):\n", + " print id(city[i]), \"\\t\\t\\t \", city[i], city[i], city[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address \t\t\t\t Contents\n", + "140069470495088 \t\t\t B B B\n", + "140069470360920 \t\t\t a a a\n", + "140069470570416 \t\t\t n n n\n", + "140069470363320 \t\t\t g g g\n", + "140069470360920 \t\t\t a a a\n", + "140069470363160 \t\t\t l l l\n", + "140069470363280 \t\t\t o o o\n", + "140069470569656 \t\t\t r r r\n", + "140069470571696 \t\t\t e e e\n" + ] + } + ], + "prompt_number": 53 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 293" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "chArr = \"Pointer and Strings\"\n", + "chPtr = chArr\n", + "print \"Contents pointed by the pointer chPtr is: \", chPtr" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Contents pointed by the pointer chPtr is: Pointer and Strings\n" + ] + } + ], + "prompt_number": 55 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 293" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def usrstrlen(ptr):\n", + " ln = 0\n", + " for ele in ptr:\n", + " ln += 1\n", + " return ln\n", + "s1 = \"Good\"\n", + "print \"Length of string using standard library function\", \n", + "print len(s1)\n", + "length = usrstrlen(s1)\n", + "print \"Length of string using user defined function \", length" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Length of string using standard library function 4\n", + "Length of string using user defined function 4\n" + ] + } + ], + "prompt_number": 62 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.12, page no. 294" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Enger the string to find it's length\"\n", + "string = raw_input()\n", + "print \"String is: \", string\n", + "print \"It's length is : \", len(string)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enger the string to find it's length\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Visvesvaraya College\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "String is: Visvesvaraya College\n", + "It's length is : 20\n" + ] + } + ], + "prompt_number": 63 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 294" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def usrstrcpy(s):\n", + " s2 = []\n", + " for ele in s:\n", + " s2.append(ele)\n", + " s2 = ''.join(s2)\n", + " return s2\n", + "s1 = \"Good\"\n", + "s2 = s1\n", + "print \"Copied string using normal assignment is \", s2\n", + "s2 = usrstrcpy(s1)\n", + "print \"Copied string using user defined function is \", s2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Copied string using normal assignment is Good\n", + "Copied string using user defined function is Good\n" + ] + } + ], + "prompt_number": 66 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 295" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def usrstrcat(s2, s1):\n", + " return s1 + s2\n", + "\n", + "s1 = \"Good\"\n", + "s2 = \" Morning\"\n", + "s3 = s1+s2\n", + "print \"Concatenation using normal Python addition\",\n", + "print s3\n", + "s1 = usrstrcat(s2, s1)\n", + "print \"concatenation using user defined function is \", s1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Concatenation using normal Python addition Good Morning\n", + "concatenation using user defined function is Good Morning\n" + ] + } + ], + "prompt_number": 70 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 296" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#only user defined way is possible in Python. Normal way would return either True or False only\n", + "s1 = \"Good\"\n", + "s2 = \" Morning\"\n", + "if s1 < s2:\n", + " r = -1\n", + "elif s1 > s2:\n", + " r = 1\n", + "else:\n", + " r = 0\n", + "print \"Comparison of strings using user defined way is \", r" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Comparison of strings using normal python way is 1\n" + ] + } + ], + "prompt_number": 72 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 297" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "pet = [\"lion\", \"cat\"]\n", + "for i in range(len(pet)):\n", + " print id(pet[i]), pet[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "52705200 lion\n", + "140069470362560 cat\n" + ] + } + ], + "prompt_number": 76 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.13, page no. 298" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "st = \"hello\"\n", + "for i in range(len(st)):\n", + " print st[i], \"\\t\", st[i], \"\\t\", st[i], \"\\t\", st[i] " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "h \th \th \th\n", + "e \te \te \te\n", + "l \tl \tl \tl\n", + "l \tl \tl \tl\n", + "o \to \to \to\n" + ] + } + ], + "prompt_number": 78 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.14, page no. 299" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "st = \"Good Morning\"\n", + "print st[3:]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "d Morning\n" + ] + } + ], + "prompt_number": 80 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.15, page no. 299" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#not possible in Python. Skipping" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 81 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strchr.c, page no. 300" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "string = raw_input(\"Enter a string: \")\n", + "c = raw_input(\"Enter character to be searched: \")\n", + "try:\n", + " i = list(string).index(c)\n", + " print \"%c was found in position %d\" %(c, i)\n", + "except:\n", + " pass" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a string: House\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter character to be searched: s\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s was found in position 3\n" + ] + } + ], + "prompt_number": 86 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strcspn.c, page no. 302" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "string1 = \"0123456789\"\n", + "string2 = \"A47DCF8\"\n", + "c = 0\n", + "for ele in string2:\n", + " if ele in string1:\n", + " index = string1.index(ele)\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strcspn.c, page no.303" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "string1 = \"0123456789\"\n", + "string2 = \"A32DC014\"\n", + "c = 0\n", + "for ele in string1:\n", + " for ele1 in string2:\n", + " if ele == ele1:\n", + " c += 1\n", + "print c" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strpbrk.c, page no. 303" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "\n", + "print \"Input two Strings: \"\n", + "str1 = raw_input(\"Str1: \")\n", + "str2 = raw_input(\"Str2: \")\n", + "\n", + "for ele in str1:\n", + " if ele in str2:\n", + " print \"Character found: \", ele\n", + " sys.exit()\n", + "print \"Character not found\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two Strings: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Str1: House\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Str2: Sorrow\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Character found: o\n" + ] + }, + { + "ename": "SystemExit", + "evalue": "", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[1;31mSystemExit\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strstr.c, page no. 304" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "\n", + "print \"Input two Strings: \"\n", + "str1 = raw_input(\"Str1: \")\n", + "str2 = raw_input(\"Str2: \")\n", + "\n", + "if str2 in str1:\n", + " str1.index(str2)\n", + " print \"String 2 is found at position 6 in string1\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two Strings: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Str1: I saw the cat click the mouse.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Str2: the\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "String 2 is found at position 6 in string1\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strtok.c, page no. 305" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str = raw_input(\"Input a string, parts separated by , or ;\")\n", + "print \"The tokens found were: \"\n", + "token = ''\n", + "for i in range(len(my_str)):\n", + " if my_str[i] == ',' or my_str[i] == ';':\n", + " if len(token) > 0:\n", + " print token\n", + " token = ''\n", + " else:\n", + " token = token + my_str[i]\n", + "print token" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input a string, parts separated by , or ;;abc,defgh;;hi,jklmn;op\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The tokens found were: \n", + "abc\n", + "defgh\n", + "hi\n", + "jklmn\n", + "op\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.16, pag eno. 307" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def func1(i, f):\n", + " print i, f\n", + "def func2(s):\n", + " print s\n", + " \n", + "i = 5\n", + "f = 5.375\n", + "s = \"string\"\n", + "func1(i, f)\n", + "func2(s)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 5.375\n", + "string\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 308" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def fact(m):\n", + " if m == 1:\n", + " return 1\n", + " else:\n", + " return m*fact(m-1)\n", + "print \"Enter the integer whose factorial is to be found: \",\n", + "n = int(raw_input())\n", + "ans = fact(n)\n", + "print \"Factorial of \", n, \" is \", ans" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the integer whose factorial is to be found: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Factorial of 8 is 40320\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 308" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def large(b):\n", + " return max(b)\n", + "def small(b):\n", + " return min(b)\n", + "print \"Enter the number of integers: \",\n", + "n = int(raw_input())\n", + "print \"Enter the set of integers: \"\n", + "a = []\n", + "for i in range(n):\n", + " a.append(int(raw_input()))\n", + "print \"The largest integer is: \", large(a)\n", + "print \"The smallest integer is: \", small(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of integers: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the set of integers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The largest integer is: 5\n", + "The smallest integer is: -7\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.17, page no. 311" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MAX = 20\n", + "def print_array(a):\n", + " print \"The elements of the array are: \"\n", + " for ele in a:\n", + " print ele,\n", + "print \"Enter the number of elements: \"\n", + "n = int(raw_input())\n", + "print \"Enter the elements: \"\n", + "a = []\n", + "for i in range(n):\n", + " a.append(int(raw_input()))\n", + "if n < 0 or n > MAX:\n", + " print \"Invalid number of elements...\"\n", + "else:\n", + " print_array(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of elements: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the elements: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The elements of the array are: \n", + "1 2 3 4 5\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.18, page no. 313" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def upper(instr):\n", + " return instr.upper()\n", + "def lower(instr):\n", + " return instr.lower()\n", + "instr = raw_input(\"Input a string: \")\n", + "print \"To upper or lower (u/l): \"\n", + "inchar = raw_input()\n", + "if inchar == 'l':\n", + " print \"The converted string is \\n\", lower(instr)\n", + "elif inchar == 'u':\n", + " print \"The conveerted string is \\n\", upper(instr)\n", + "else:\n", + " print \"Ivalid input...\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input a string: UVCE, Bangalore\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "To upper or lower (u/l): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "l\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The converted string is \n", + "uvce, bangalore\n" + ] + } + ], + "prompt_number": 15 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/chapter9.ipynb b/Mastering_C/chapter9.ipynb new file mode 100644 index 00000000..9eacd97f --- /dev/null +++ b/Mastering_C/chapter9.ipynb @@ -0,0 +1,1463 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:2b328a00a423d96b629425895ccbcc90fde898bdf09e6f58382db5eb59593f31" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9: Structures & Unions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.1, page no. 338" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class date:\n", + " dat = 0\n", + " month = 0\n", + " year = 0\n", + "class person:\n", + " name = ''\n", + " lastnam = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "emprec = person()\n", + "birth = emprec.birthday\n", + "emprec.name = \"Tejaswi.V.\"\n", + "emprec.lastnam = \"Rajuk\"\n", + "emprec.birthday.day = 24\n", + "emprec.birthday.month = 7\n", + "emprec.birthday.year = 90\n", + "x = 6500.00\n", + "emprec.salary = x\n", + "print \"Employee Details: \"\n", + "print \"Name: \", emprec.name, \" \", emprec.lastnam\n", + "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n", + "print \"Salary: \", emprec.salary" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Tejaswi.V. Rajuk\n", + "Birthdate: 24 : 7 : 90\n", + "Salary: 6500.0\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.2, page no. 340" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "CURRENT_YEAR = 96\n", + "def increment(sal, year, inc):\n", + " if CURRENT_YEAR - year > 30:\n", + " sal += inc\n", + " return sal\n", + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "n = 500\n", + "emprec = person()\n", + "emprec.name = 'Arun R.'\n", + "emprec.birthday.day = 10\n", + "emprec.birthday.month = 8\n", + "emprec.birthday.year = 64\n", + "emprec.salary = 4000.00\n", + "print \"Employee Details: \"\n", + "print \"Name: \", emprec.name\n", + "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n", + "print \"Salary: \", emprec.salary\n", + "emprec.salary = increment(emprec.salary, emprec.birthday.year, n)\n", + "print \"Employee Details: \"\n", + "print \"Name: \", emprec.name\n", + "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n", + "print \"Salary: \", emprec.salary" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Arun R.\n", + "Birthdate: 10 : 8 : 64\n", + "Salary: 4000.0\n", + "Employee Details: \n", + "Name: Arun R.\n", + "Birthdate: 10 : 8 : 64\n", + "Salary: 4500.0\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.3, page no. 342" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "CURRENT_YEAR = 96\n", + "def increment(emprec):\n", + " if CURRENT_YEAR - emprec.birthday.year > 30:\n", + " emprec.salary += 500\n", + " return emprec\n", + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "emprec = person()\n", + "emprec.name = 'Arun R.'\n", + "emprec.birthday.day = 10\n", + "emprec.birthday.month = 8\n", + "emprec.birthday.year = 64\n", + "emprec.salary = 4000.00\n", + "print \"Employee Details: \"\n", + "print \"Name: \", emprec.name\n", + "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n", + "print \"Salary: \", emprec.salary\n", + "emprec = increment(emprec)\n", + "print \"Employee Details: \"\n", + "print \"Name: \", emprec.name\n", + "print \"Birthdate: \", emprec.birthday.day, \":\", emprec.birthday.month, \":\", emprec.birthday.year\n", + "print \"Salary: \", emprec.salary" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Arun R.\n", + "Birthdate: 10 : 8 : 64\n", + "Salary: 4000.0\n", + "Employee Details: \n", + "Name: Arun R.\n", + "Birthdate: 10 : 8 : 64\n", + "Salary: 4500.0\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.4, page no. 343" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "\n", + "def printout(per):\n", + " print \"Employee Details: \"\n", + " print \"Name: \", per.name\n", + " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n", + " print \"Salary: %6.2f\" %(per.salary)\n", + "\n", + "def readin(record):\n", + " record.name = raw_input(\"Enter the name: \")\n", + " print \"Enter Birthdate\"\n", + " record.birthday.day = int(raw_input(\"Day: \"))\n", + " record.birthday.month = int(raw_input(\"Month: \"))\n", + " record.birthday.year = int(raw_input(\"Year: \"))\n", + " record.salary = float(raw_input(\"Enter the salary: \"))\n", + "\n", + "test = person()\n", + "test.name = \"Arun\"\n", + "test.birthday.day = 10\n", + "test.birthday.month = 8\n", + "test.birthday.year = 75\n", + "test.salary = 4500.00\n", + "temp = person()\n", + "readin(temp)\n", + "printout(test)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Balaji\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 74\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6500.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Arun\n", + "Birthdate: 24: 2: 74\n", + "Salary: 4500.00\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5 page no. 344" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "\n", + "def printout(per):\n", + " print \"Employee Details: \"\n", + " print \"Name: \", per.name\n", + " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n", + " print \"Salary: %6.2f\" %(per.salary)\n", + "\n", + "def readin(record):\n", + " record.name = raw_input(\"Enter the name: \")\n", + " print \"Enter Birthdate\"\n", + " record.birthday.day = int(raw_input(\"Day: \"))\n", + " record.birthday.month = int(raw_input(\"Month: \"))\n", + " record.birthday.year = int(raw_input(\"Year: \"))\n", + " record.salary = float(raw_input(\"Enter the salary: \"))\n", + "\n", + "test = person()\n", + "test.name = \"Arun\"\n", + "test.birthday.day = 10\n", + "test.birthday.month = 8\n", + "test.birthday.year = 75\n", + "test.salary = 4500.00\n", + "temp = person()\n", + "readin(temp)\n", + "printout(temp)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Balaji\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 74\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6500.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Balaji\n", + "Birthdate: 24: 2: 74\n", + "Salary: 6500.00\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.6, page no. 346" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + " \n", + " def __init__(self, n, dt, sal):\n", + " self.name = n\n", + " self.birthday.day = dt[0]\n", + " self.birthday.month = dt[1]\n", + " self.birthday.year = dt[2]\n", + " self.salary = sal\n", + "\n", + "def printout(per):\n", + " print \"Employee Details: \"\n", + " print \"Name: \", per.name\n", + " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n", + " print \"Salary: %6.2f\" %(per.salary)\n", + "\n", + "def highest(sals):\n", + " return max(sals)\n", + " \n", + "record = [[\"Arun R.\", 10, 8, 75, 4000.00],\n", + " [\"vinod S.\", 3, 10, 74, 3000.00],\n", + " [\"Tanuj M.\", 21, 1, 73, 5500.00]]\n", + "\n", + "emp1 = person(record[0][0], (record[0][1], record[0][2], record[0][3]), record[0][4])\n", + "emp2 = person(record[1][0], (record[1][1], record[1][2], record[1][3]), record[1][4])\n", + "emp3 = person(record[2][0], (record[2][1], record[2][2], record[2][3]), record[2][4])\n", + "\n", + "sals = [emp1.salary, emp2.salary, emp3.salary]\n", + "\n", + "highest = highest(sals)\n", + "print \"The highest salary being paid is %6.2f\" %highest\n", + "printout(emp3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The highest salary being paid is 5500.00\n", + "Employee Details: \n", + "Name: Tanuj M.\n", + "Birthdate: 21: 1: 73\n", + "Salary: 5500.00\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7, page no. 347" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "\n", + "def readin(rec, list1):\n", + " rec.name = list1[0]\n", + " rec.birthday.day = list1[1]\n", + " rec.birthday.month = list1[2]\n", + " rec.birthday.year = list1[3]\n", + " rec.salary = list1[4]\n", + "\n", + " \n", + "def printout(per):\n", + " print \"Employee Details: \"\n", + " print \"Name: \", per.name\n", + " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n", + " print \"Salary: %6.2f\" %(per.salary)\n", + "\n", + "def highest(sals):\n", + " return max(sals)\n", + " \n", + "record = [[\"Tejaswi\", 24, 7, 70, 7000.00],\n", + " [\"vinod S.\", 3, 10, 74, 3000.00],\n", + " [\"Tanuj M.\", 21, 1, 73, 5500.00]]\n", + "\n", + "emp1 = person()\n", + "readin(emp1, record[0])\n", + "print emp1.birthday.day\n", + "emp2 = person()\n", + "readin(emp2, record[1])\n", + "emp3 = person()\n", + "readin(emp3, record[2])\n", + "\n", + "highest = highest([emp1.salary, emp2.salary, emp3.salary])\n", + "temp = emp1\n", + "print \"Highest salary being paid is \", highest\n", + "printout(temp)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "24\n", + "Highest salary being paid is 7000.0\n", + "Employee Details: \n", + "Name: Tejaswi\n", + "Birthdate: 21: 1: 73\n", + "Salary: 7000.00\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.8, page no. 349" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "\n", + "def readin():\n", + " record = []\n", + " for i in range(3):\n", + " print \"Employee no. \", i+1\n", + " temp = person()\n", + " temp.name = raw_input(\"Enter the name: \")\n", + " print \"Enter Birthdate\"\n", + " temp.birthday.day = int(raw_input(\"Day: \"))\n", + " temp.birthday.month = int(raw_input(\"Month: \"))\n", + " temp.birthday.year = int(raw_input(\"Year: \"))\n", + " temp.salary = float(raw_input(\"Enter the salary: \"))\n", + " record.append(temp)\n", + " return record\n", + "\n", + "def printout(per):\n", + " print \"Employee Details: \"\n", + " print \"Name: \", per.name\n", + " print \"Birthdate: %3d:%3d:%3d\" %(per.birthday.day, per.birthday.month, per.birthday.year)\n", + " print \"Salary: %6.2f\" %(per.salary)\n", + "\n", + "record = readin()\n", + "for i in range(3):\n", + " printout(record[i])\n", + " print \"hit enter to continue...\"\n", + " raw_input()\n", + " \n", + "#there is a printing mistake in the book" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Balaji\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 74\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6500.50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Tarun\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 69\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6890.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Vinod\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6700.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Balaji\n", + "Birthdate: 7: 6: 70\n", + "Salary: 6500.50\n", + "hit enter to continue...\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Tarun\n", + "Birthdate: 7: 6: 70\n", + "Salary: 6890.00\n", + "hit enter to continue...\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Vinod\n", + "Birthdate: 7: 6: 70\n", + "Salary: 6700.00\n", + "hit enter to continue...\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.9, page no. 351" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + "\n", + "class person:\n", + " name = ''\n", + " birthday = date()\n", + " salary = 0.0\n", + "\n", + "def readin(n):\n", + " record = []\n", + " for i in range(n):\n", + " print \"Employee no. \", i+1\n", + " temp = person()\n", + " temp.name = raw_input(\"Enter the name: \")\n", + " print \"Enter Birthdate\"\n", + " temp.birthday.day = int(raw_input(\"Day: \"))\n", + " temp.birthday.month = int(raw_input(\"Month: \"))\n", + " temp.birthday.year = int(raw_input(\"Year: \"))\n", + " temp.salary = float(raw_input(\"Enter the salary: \"))\n", + " record.append(temp)\n", + " return record\n", + "\n", + "def printout(record):\n", + " for i in range(n):\n", + " print \"Employee Details: \"\n", + " print \"Name: \", record[i].name\n", + " print \"Birthdate: %3d:%3d:%3d\" %(record[i].birthday.day, record[i].birthday.month, record[i].birthday.year)\n", + " print \"Salary: %6.2f\" %(record[i].salary)\n", + "\n", + "def sortalpha(rec):\n", + " for i in range(n-1):\n", + " for j in range(n-i-1):\n", + " if rec[j].name > rec[j+1].name:\n", + " rec[j], rec[j+1] = rec[j+1], rec[j]\n", + " return rec\n", + "\n", + "def sortage(rec):\n", + " for i in range(n-1):\n", + " for j in range(n-i-1):\n", + " if (rec[j].birthday.year > rec[j+1].birthday.year or rec[j].birthday.year == rec[j+1].birthday.year and\n", + " rec[j].birthday.month > rec[j+1].birthday.month or rec[j].birthday.month == rec[j+1].birthday.month and\n", + " rec[j].birthday.day > rec[j+1].birthday.day or rec[j].birthday.day == rec[j+1].birthday.day):\n", + " rec[j], rec[j+1] = rec[j+1], rec[j]\n", + " return rec\n", + "\n", + "def sortsal(rec):\n", + " for i in range(n-1):\n", + " for j in range(n-i-1):\n", + " if rec[j].salary > rec[j+1].salary:\n", + " rec[j], rec[j+1] = rec[j+1], rec[j]\n", + " return rec\n", + "\n", + "n = int(raw_input(\"Enter the number of records: \"))\n", + "record = readin(n)\n", + "\n", + "flag = True\n", + "while(flag):\n", + " print \"1: SORT BY NAME\"\n", + " print \"2: SORT BY AGE\"\n", + " print \"3: SORT BY SALARY\"\n", + " print \"4: QUIT \"\n", + " i = int(raw_input(\"Enter your choice: \"))\n", + " if i == 1:\n", + " sorted_rec = sortalpha(record)\n", + " printout(sorted_rec)\n", + " elif i == 2:\n", + " sorted_rec = sortage(record)\n", + " printout(sorted_rec)\n", + " elif i == 3:\n", + " sorted_rec = sortsal(record)\n", + " printout(sorted_rec)\n", + " elif i == 4:\n", + " flag = False\n", + " break\n", + " else:\n", + " print \"invalid choice...\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of records: 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Vinod\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 23\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 74\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6500\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Arun\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 7000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Puneet\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 74\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6900\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Tanuj\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 7700\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee no. 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name: Rohit\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 17\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 73\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the salary: 6800\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1: SORT BY NAME\n", + "2: SORT BY AGE\n", + "3: SORT BY SALARY\n", + "4: QUIT \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your choice: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Arun\n", + "Birthdate: 17: 12: 73\n", + "Salary: 7000.00\n", + "Employee Details: \n", + "Name: Puneet\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6900.00\n", + "Employee Details: \n", + "Name: Rohit\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6800.00\n", + "Employee Details: \n", + "Name: Tanuj\n", + "Birthdate: 17: 12: 73\n", + "Salary: 7700.00\n", + "Employee Details: \n", + "Name: Vinod\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6500.00\n", + "1: SORT BY NAME\n", + "2: SORT BY AGE\n", + "3: SORT BY SALARY\n", + "4: QUIT \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your choice: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Vinod\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6500.00\n", + "Employee Details: \n", + "Name: Tanuj\n", + "Birthdate: 17: 12: 73\n", + "Salary: 7700.00\n", + "Employee Details: \n", + "Name: Rohit\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6800.00\n", + "Employee Details: \n", + "Name: Puneet\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6900.00\n", + "Employee Details: \n", + "Name: Arun\n", + "Birthdate: 17: 12: 73\n", + "Salary: 7000.00\n", + "1: SORT BY NAME\n", + "2: SORT BY AGE\n", + "3: SORT BY SALARY\n", + "4: QUIT \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your choice: 33\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "invalid choice...\n", + "1: SORT BY NAME\n", + "2: SORT BY AGE\n", + "3: SORT BY SALARY\n", + "4: QUIT \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your choice: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "Name: Vinod\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6500.00\n", + "Employee Details: \n", + "Name: Rohit\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6800.00\n", + "Employee Details: \n", + "Name: Puneet\n", + "Birthdate: 17: 12: 73\n", + "Salary: 6900.00\n", + "Employee Details: \n", + "Name: Arun\n", + "Birthdate: 17: 12: 73\n", + "Salary: 7000.00\n", + "Employee Details: \n", + "Name: Tanuj\n", + "Birthdate: 17: 12: 73\n", + "Salary: 7700.00\n", + "1: SORT BY NAME\n", + "2: SORT BY AGE\n", + "3: SORT BY SALARY\n", + "4: QUIT \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your choice: 4\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example mem.c, page no. 359" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Note: There is no concept of unions in Python. We will implment using classes\n", + "\n", + "import sys\n", + "\n", + "class emp:\n", + " name = ''\n", + " idno = 0\n", + " salary = 0.0\n", + "\n", + "class desc:\n", + " name = ''\n", + " idno = 0\n", + " salary = 0.0\n", + "\n", + "e = emp()\n", + "d = desc()\n", + " \n", + "print \"The size of the structure is \", sys.getsizeof(e)\n", + "print \"The size of the union is \", sys.getsizeof(d)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The size of the structure is 72\n", + "The size of the union is 72\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page no. 359" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class desc:\n", + " name = ''\n", + " idno = 0\n", + " salary = 0.0\n", + "\n", + "des = desc()\n", + "des.name = 'Vinod'\n", + "print \"Employee Details: \"\n", + "print \"The name is \", des.name\n", + "print \"The idno is \", des.idno\n", + "print \"The salary is \", des.salary\n", + "des.idno = 10\n", + "print \"Employee Details: \"\n", + "print \"The name is \", des.name\n", + "print \"The idno is \", des.idno\n", + "print \"The salary is \", des.salary\n", + "des.salary = 6500.00\n", + "print \"Employee Details: \"\n", + "print \"The name is \", des.name\n", + "print \"The idno is \", des.idno\n", + "print \"The salary is \", des.salary\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee Details: \n", + "The name is Vinod\n", + "The idno is 0\n", + "The salary is 0.0\n", + "Employee Details: \n", + "The name is Vinod\n", + "The idno is 10\n", + "The salary is 0.0\n", + "Employee Details: \n", + "The name is Vinod\n", + "The idno is 10\n", + "The salary is 6500.0\n" + ] + } + ], + "prompt_number": 2 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Mastering_C/screenshots/interest.png b/Mastering_C/screenshots/interest.png Binary files differnew file mode 100644 index 00000000..1ac244be --- /dev/null +++ b/Mastering_C/screenshots/interest.png diff --git a/Mastering_C/screenshots/matrix.png b/Mastering_C/screenshots/matrix.png Binary files differnew file mode 100644 index 00000000..4225f87e --- /dev/null +++ b/Mastering_C/screenshots/matrix.png diff --git a/Mastering_C/screenshots/stack.png b/Mastering_C/screenshots/stack.png Binary files differnew file mode 100644 index 00000000..6de0cc8b --- /dev/null +++ b/Mastering_C/screenshots/stack.png |