{ "metadata": { "name": "ch8" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": "\"\"\"\nEXAMPLE 8.1\n\"\"\"\n\nn= [44] # n holds the int 44\nprint \"int n=44; // n holds the int 44:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\npn = n \nprint \"int* pn=&n; // pn holds the address of n:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\n\npn[0] = 77 # changes the value of n to 77\nprint \"*pn = 77; // changes the value of n to 77:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\n\nq = n \nprint \"int* q=&n; // q also holds the address of n:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\nprint \"\\t\\t q = \" , hex(id(q))\nprint \"\\t\\t &q = \" , hex(id(hex(id(hex(id(pn))))))\nprint \"\\t\\t *q = \" , q \n\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "int n=44; // n holds the int 44:\n\n\t\t n = [44]\n\t\t &n = 0x9bfb92c\nint* pn=&n; // pn holds the address of n:\n\n\t\t n = [44]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9bf5aa0\n\t\t *pn = [44]\n*pn = 77; // changes the value of n to 77:\n\n\t\t n = [77]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9c6a760\n\t\t *pn = [77]\nint* q=&n; // q also holds the address of n:\n\n\t\t n = [77]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9bf5c80\n\t\t *pn = [77]\n\t\t q = 0x9bfb92c\n\t\t &q = 0x9c6a760\n\t\t *q = [77]\n" } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.2\nNote : Python does not append '\\0' at the end of string.\n'''\n\ns = \"ABCD\"\nfor i in range(4):\n print \"s[\" , i , \"] = '\" , s[i] , \"'\\n\";\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "s[ 0 ] = ' A '\n\ns[ 1 ] = ' B '\n\ns[ 2 ] = ' C '\n\ns[ 3 ] = ' D '\n\n" } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.3\n'''\n\nwhile True:\n word = raw_input()\n if len(word) < 2:\n break\n l = word.split(' ')\n for i in l:\n print '\\t\"' , i , '\"'\n", "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Today's date is March 12, 2000.\n" }, { "output_type": "stream", "stream": "stdout", "text": "\t\" Today's \"\n\t\" date \"\n\t\" is \"\n\t\" March \"\n\t\" 12, \"\n\t\" 2000. \"\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Tomorrow is Monday.\n" }, { "output_type": "stream", "stream": "stdout", "text": "\t\" Tomorrow \"\n\t\" is \"\n\t\" Monday. \"\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "\n" } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.4\nThis program echoes the input, line by line:\n'''\n\nwhile True:\n line = raw_input()\n if len(line) < 2:\n break\n print \"\\t[\" , line , \"]\"\n", "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" }, { "output_type": "stream", "stream": "stdout", "text": "\t[ Once upon a midnight dreary, while I pondered, weak and weary, ]\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Over a many quaint and curious volume of forgotten lore,\n" }, { "output_type": "stream", "stream": "stdout", "text": "\t[ Over a many quaint and curious volume of forgotten lore, ]\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "\n" } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.5\n'''\n\n\nwhile True:\n word = raw_input()\n if len(word) < 2:\n break\n l = word.split(',')\n for i in range(len(l)-1):\n print '\\t[' , l[i] , ']'\n\n", "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" }, { "output_type": "stream", "stream": "stdout", "text": "\t[ Once upon a midnight dreary ]\n\t[ while I pondered ]\n\t[ weak and weary ]\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Over a many quaint and curious volume of forgotten lore,\n" }, { "output_type": "stream", "stream": "stdout", "text": "\t[ Over a many quaint and curious volume of forgotten lore ]\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "\n" } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.6 \nThis program counts the number of occurrences of the letter 'e' in the input stream. \n'''\n\ncount = 0\nwhile True:\n a = raw_input()\n if len(a) < 1:\n break\n for ch in a:\n if (ch == 'e'): count+=1\n \nprint count , \" e's were counted.\\n\"\n", "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Over many a quaint and curious volume of forgotten lore,\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "\n" }, { "output_type": "stream", "stream": "stdout", "text": "11 e's were counted.\n\n" } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.7 \nThis program echoes the input stream, capitalizing each word:\n'''\n\nwhile True:\n a = raw_input()\n if len(a) < 1:\n break\n print a.title()\n", "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Fourscore and seven years ago our fathers\n" }, { "output_type": "stream", "stream": "stdout", "text": "Fourscore And Seven Years Ago Our Fathers\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "brought forth upon this continent a new nation,\n" }, { "output_type": "stream", "stream": "stdout", "text": "Brought Forth Upon This Continent A New Nation,\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "\n" } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.8\nThis tests a function that extracts the integers from the input stream:\n'''\n\na = raw_input()\nl = a.split(' ')\nnos = []\nfor i in l:\n try:\n i = int(i)\n nos.append(i)\n except:\n continue\nm = nos[0]\nn = nos[1] \nprint m , \" + \" , n , \" = \" , m+n", "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "what is 305 plus 9416 ?\n" }, { "output_type": "stream", "stream": "stdout", "text": "305 + 9416 = 9721\n" } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.10 An Array of Strings\n'''\n\nname = []\ncount=0\n\nprint \"Enter at most 4 names with at most 19 characters:\\n\";\nwhile (True):\n n = raw_input()\n if len(n) < 1:\n break\n name.append(n)\n count += 1\n \nprint \"The names are:\\n\"\nfor i in range(count):\n print \"\\t\" , i , \". [\" , name[i] , \"]\" \n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter at most 4 names with at most 19 characters:\n\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "George Washington\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "John Adams\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Thomas Jefferson\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "\n" }, { "output_type": "stream", "stream": "stdout", "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.11 A String Array\n'''\n\nname = []\ncount=0\n\nprint \"Enter at most 4 names with at most 19 characters:\\n\";\nwhile (True):\n n = raw_input()\n if len(n) < 1:\n break\n name.append(n)\n count += 1\n \nprint \"The names are:\\n\"\nfor i in range(count):\n print \"\\t\" , i , \". [\" , name[i] , \"]\" ", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter at most 4 names with at most 19 characters:\n\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "George Washington\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "John Adams\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "Thomas Jefferson\n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "\n" }, { "output_type": "stream", "stream": "stdout", "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.12 Initializing a String Array\nThis program is nearly equivalent to those in the previous two examples. It initializes the array/list\nname and then prints its contents:\n'''\n\nname = [ \"George Washington\", \"John Adams\", \"Thomas Jefferson\"]\nprint \"The names are:\\n\"\nfor i in range(3):\n print \"\\t\" , i , \". [\" , name[i] , \"]\"\n\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.13 The len() Function\n'''\n\ns = \"ABCDEFG\"\nprint \"len(\" , s , \") = \" , len(s) \nprint \"len(\\\"\\\") = \" , len(\"\")\nprint \"Enter string: \"\nb = raw_input()\nprint \"len(\" , b , \") = \" , len(b)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "len( ABCDEFG ) = 7\nlen(\"\") = 0\nEnter string: \n" }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": "hello how are you !!!\n" }, { "output_type": "stream", "stream": "stdout", "text": "len( hello how are you !!! ) = 21\n" } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.14 \ngives index of substring.\n'''\ns = \"The Mississippi is a long river.\"\nprint 's = \"' , s , '\"'\np = s.find(' ')\nprint \"find(s, ' ') points to s[\" , p , \"].\"\np = s.find('s')\nprint \"find(s, 's') points to s[\" , p , \"].\"\np = s.rfind('s')\nprint \"reverse find(s, 's') points to s[\" , p , \"].\"\np = s.find(\"is\")\nprint \"strstr(s, \\\"is\\\") points to s[\" , p , \"].\"\np = s.find(\"isi\")\nif p== -1:\n print 's.find(\"isi\") returns NULL'\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "s = \" The Mississippi is a long river. \"\nfind(s, ' ') points to s[ 3 ].\nfind(s, 's') points to s[ 6 ].\nreverse find(s, 's') points to s[ 17 ].\nstrstr(s, \"is\") points to s[ 5 ].\ns.find(\"isi\") returns NULL\n" } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.15 string copy\n'''\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before strcpy(s1,s2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 = s2\nprint \"After strcpy(s1,s2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Before strcpy(s1,s2):\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter strcpy(s1,s2):\n\n\ts1 = [ XYZ ], length = 3\n\ts2 = [ XYZ ], length = 3\n" } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.16 string copy\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before strcpy(s1,s2,2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 = s2[:2] + s1[2:]\nprint \"After strcpy(s1,s2,2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n\n\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Before strcpy(s1,s2,2):\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter strcpy(s1,s2,2):\n\n\ts1 = [ XYCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\n" } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.17 The String Concatenation\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before string concatination :\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 += s2\nprint \"After string concatination :\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) ", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Before string concatination :\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter string concatination :\n\ts1 = [ ABCDEFGXYZ ], length = 10\n\ts2 = [ XYZ ], length = 3\n" } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.18 The Second String Concatenation (no. of characters)\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before string concatination :\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 += s2[:2]\nprint \"After string concatination :\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) ", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Before string concatination :\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter string concatination :\n\ts1 = [ ABCDEFGXY ], length = 9\n\ts2 = [ XYZ ], length = 3\n" } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.19 The String Tokenize \n'''\ns = \"Today's date is March 12, 2000.\"\n\nprint \"The string is: [\" , s , \"] \\nIts tokens are: \"\np = s.split(\" \")\n\nfor i in p:\n print \"\\t[\" , i , \"] \"\n\nprint \"Now the string is: [\" , p[0] , \"] \";\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "The string is: [ Today's date is March 12, 2000. ] \nIts tokens are: \n\t[ Today's ] \n\t[ date ] \n\t[ is ] \n\t[ March ] \n\t[ 12, ] \n\t[ 2000. ] \nNow the string is: [ Today's ] \n" } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": "'''\nEXAMPLE 8.20 The strpbrk() Function\nC provides inbuilt strpbrk(), Here i have written mine.\n'''\n\ndef strpbrk(s,s1):\n found = []\n for i in range(len(s1)):\n if s1[i] in s:\n index = s.find(s1[i])\n found.append(index)\n if found:\n return min(found)\n return None\n \n\ns = \"The Mississippi is a long river.\"\nprint 's = \"' , s , '\"'\np = strpbrk(s, \"nopqr\")\nprint 'strpbrk(s, \"nopqr\") points to s[' , p , \"].\"\np = strpbrk(s, \"NOPQR\")\nif (p == None):\n print 'strpbrk(s, \"NOPQR\") returns NULL.\\n'", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "s = \" The Mississippi is a long river. \"\nstrpbrk(s, \"nopqr\") points to s[ 12 ].\nstrpbrk(s, \"NOPQR\") returns NULL.\n\n" } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }