diff options
Diffstat (limited to 'Mastering_C/chapter8.ipynb')
-rw-r--r-- | Mastering_C/chapter8.ipynb | 2252 |
1 files changed, 2252 insertions, 0 deletions
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 |