summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_21(1).ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_21(1).ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_21(1).ipynb427
1 files changed, 427 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_21(1).ipynb b/C++_from_the_Ground/Chapter_21(1).ipynb
new file mode 100644
index 00000000..a37db7f8
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_21(1).ipynb
@@ -0,0 +1,427 @@
+{
+ "metadata": {
+ "name": "Chapter 21"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h1>Chapter 21: Introducing the Standard Template Library<h1>"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.1, Page Number:507<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''An implementation of vector in python'''\n\n#Variable declaratio\nv=[] #Create a zero length vector\n\n#display original size of v\nprint \"Size =\",len(v)\n\n#put values onto end of vector\n#vector will grow as needed\n\nfor i in range(10):\n v.append(i)\n \n#display current size of v\nprint \"Current contents: \"\nprint \"Size now =\",len(v)\n\n#display contents of vector\nfor i in range(len(v)):\n print v[i],\n \nprint\n \n#put more values onto end of vector\n#again, vector will grow as needed.\nfor i in range(10):\n v.append(i+10)\n#display current size of v\nprint \"Size now =\",len(v)\n\n#display contents of vector\nprint \"Current contents:\"\nfor i in range(len(v)):\n print v[i],\nprint\n \n#change contents of vector\nfor i in range(len(v)):\n v[i]=v[i]+v[i]\n \n#display contents of vector\nprint \"Contents doubled:\"\nfor i in range(len(v)):\n print v[i],",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Size = 0\nCurrent contents: \nSize now = 10\n0 1 2 3 4 5 6 7 8 9\nSize now = 20\nCurrent contents:\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\nContents doubled:\n0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.2, Page Number:508<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Access vector usng an terator'''\n\n#Variable declaration\nv=[] #Create a zero length vector\n\n#put values onto end of vector\nfor i in range(10):\n v.append(chr(ord('A')+i))\n \n#can access vector contents using subscripts\nfor i in range(len(v)):\n print v[i], \nprint\n \n#access via iterator\nfor p in v:\n print p,\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "A B C D E F G H I J\nA B C D E F G H I J\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.3, Page Number:509<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstrate insert and erase'''\n\n#Variable declaration\nv=[] #Create a zero length vector\n\n#put values onto end of vector\nfor i in range(10):\n v.append(chr(ord('A')+i))\n \n#Display original contents of vector\nprint \"Size =\",len(v)\nprint \"Original contents:\"\nfor i in range(len(v)):\n print v[i], \nprint \"\\n\"\n \np=2 #point to 3rd element\nfor i in range(10):\n v.insert(p+i,'X')\n \n#display contents after insertion\nprint \"Size after insert =\",len(v)\nprint \"Contents after insert:\"\nfor i in range(len(v)):\n print v[i],\nprint \"\\n\"\n\n#remove those elements\np=2 #point to 3rd element\nfor i in range(10):\n v.pop(p)\n \n#display contents after insertion\nprint \"Size after erase =\",len(v)\nprint \"Contents after insert:\"\nfor i in range(len(v)):\n print v[i],\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Size = 10\nOriginal contents:\nA B C D E F G H I J \n\nSize after insert = 20\nContents after insert:\nA B X X X X X X X X X X C D E F G H I J \n\nSize after erase = 10\nContents after insert:\nA B C D E F G H I J\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.4, Page Number:511<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Store a class object in a vector'''\n\nclass three_d:\n def __init__(self,a,b,c): #3D coordinates\n self.x=a\n self.y=b\n self.z=c\n #Display x,y,z coordinates - three_d inserter.\n def __repr__(self):\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n def __add__(self,a):\n self.x+=a\n self.y+=a\n self.z+=a\n return self\n def __lt__(self,b):\n return (self.x+self.y+self.z)<(b.x+b.y+b.z)\n def __eq__(self,b):\n return (self.x+self.y+self.z)==(b.x+b.y+b.z) \n \n#Variable declaration\nv=[]\n\n#add objects to the vector\nfor i in range(10):\n v.append(three_d(i,i+2,i-3))\n \n#Display contents of vector\nfor i in range(len(v)):\n print v[i], \nprint\n\n\n#Modify objects in a vector\nfor i in range(len(v)):\n v[i]=v[i]+10 \n\n#Display modified vector\nfor i in range(len(v)):\n print v[i], \n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0, 2, -3\n 1, 3, -2\n 2, 4, -1\n 3, 5, 0\n 4, 6, 1\n 5, 7, 2\n 6, 8, 3\n 7, 9, 4\n 8, 10, 5\n 9, 11, 6\n\n10, 12, 7\n 11, 13, 8\n 12, 14, 9\n 13, 15, 10\n 14, 16, 11\n 15, 17, 12\n 16, 18, 13\n 17, 19, 14\n 18, 20, 15\n 19, 21, 16\n\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.5, Page Number:513<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Store a class object in a vector'''\n \n#Variable declaration\nv=[]\nv2=[]\n\nfor i in range(10):\n v.append(chr(ord('A')+i))\n \n#Display original contents of vector\nprint \"Size =\",len(v)\nprint \"Original contents:\"\nfor i in range(len(v)):\n print v[i], \nprint \"\\n\"\n\n#initialze second vector\nstr=\"-STL Power-\"\nfor i in range(len(str)):\n v2.append(str[i])\n \n#get iterators to the middle of v and to the start and end of v2.\np=5\np2start=0\np2end=len(v2)-1\n\n#insert v2 into v\nfor i in range(p2end):\n v.insert(p+i,v2[p2start+i])\n \n#display result\nprint \"Contents of v after inserton:\"\nfor i in range(len(v)):\n print v[i],\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Size = 10\nOriginal contents:\nA B C D E F G H I J \n\nContents of v after inserton:\nA B C D E - S T L P o w e r F G H I J\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.6, Page Number:517<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstrate list basics'''\n\n#Variable declaration\nlst=[] #create an empty list\n\nfor i in range(10):\n lst.append(chr(ord('A')+i))\n \nprint \"Size =\",len(lst)\n\nprint \"Contents:\",\nfor p in lst:\n print p, \nprint \"\\n\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Size = 10\nContents: A B C D E F G H I J \n\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.7, Page Number:518<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''elements can be put on the front or end of a list'''\n\n#Variable declaration\nlst=[] \nrevlst=[]\n\nfor i in range(10):\n lst.append(chr(ord('A')+i))\n \nprint \"Size of list =\",len(lst)\n\nprint \"Original Contents:\",\n#Remove elements from lst and put them into revlst in reverse order.\nfor p in lst:\n print p, \n revlst.insert(0,p) \nfor i in range(10):\n lst.pop(0)\nprint \"\\n\"\n\nprint \"Size of revlst =\",len(revlst)\n\nprint \"Reversed Contents:\",\nfor p in revlst:\n print p,",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Size of list = 10\nOriginal Contents: A B C D E F G H I J \n\nSize of revlst = 10\nReversed Contents: J I H G F E D C B A\n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.8, Page Number:519<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Sort a list'''\n\nimport random\n\n#Variable declaration\nlst=[] \n\n#create a list of random integers\nfor i in range(10):\n lst.append(random.randint(0,100))\n\nprint \"Original Contents:\",\nfor p in lst:\n print p, \nprint \"\\n\"\n\n#sort the list\nlst.sort()\n\nprint \"Sorted Contents:\",\nfor p in lst:\n print p,",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Original Contents: 75 73 72 4 88 7 85 21 67 42 \n\nSorted Contents: 4 7 21 42 67 72 73 75 85 88\n"
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.9, Page Number:520<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Merge two lists'''\n\n#Variable declaration\nlst1=[] \nlst2=[]\n\nfor i in xrange(0,10,2):\n lst1.append(chr(ord('A')+i))\nfor i in xrange(1,11,2):\n lst2.append(chr(ord('A')+i))\n\nprint \"Contents of lst1:\",\nfor p in lst1:\n print p, \nprint \"\\n\"\n\nprint \"Contents of lst2:\",\nfor p in lst2:\n print p, \nprint \"\\n\"\n\n#merge the lists\nlst1=lst1+lst2\nlst1.sort()\nlst2=[]\n\nif lst2==[]:\n print \"lst2 is now empty\"\n\nprint \"Contentsof lst1 after merge:\"\nfor p in lst1:\n print p,",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Contents of lst1: A C E G I \n\nContents of lst2: B D F H J \n\nlst2 is now empty\nContentsof lst1 after merge:\nA B C D E F G H I J\n"
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.10, Page Number:521<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Store class objects in a list'''\n\nclass myclass:\n def __init__(self,i=0,j=0):\n self.__a=i\n self.__b=j\n self.sum=self.__a+self.__b\n def getsum(self):\n return self.sum\n def __lt__(self,o2):\n return self.sum<o2.sum\n def __gt__(self,o2):\n return self.sum>o2.sum\n def __eq__(self,o2):\n return self.sum==o2.sum\n def __ne__(self, other):\n return not self.__eq__(self)\n \n#create first list\nlst1=[]\nfor i in range(10):\n lst1.append(myclass(i,i))\n \nprint \"First list:\",\nfor p in lst1:\n print p.getsum(),\nprint\n\n#create second list\nlst2=[]\nfor i in range(10):\n lst2.append(myclass(i*2,i*3))\n \nprint \"First list:\",\nfor p in lst2:\n print p.getsum(),\nprint\n \n#Now merge list\nlst1=lst1+lst2\nlst1.sort()\n\n#Display merge list\nprint \"Merged list:\",\nfor p in lst1:\n print p.getsum(),\nprint",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "First list: 0 2 4 6 8 10 12 14 16 18\nFirst list: 0 5 10 15 20 25 30 35 40 45\nMerged list: 0 0 2 4 5 6 8 10 10 12 14 15 16 18 20 25 30 35 40 45\n"
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.11, Page Number:527<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A simple map implementation'''\n\n#Variable declaration\nm=[] \n\n#define the function find\ndef find(x,ch):\n for p in x:\n if p[0]==ch:\n return p\n return -1\n\n#put pairs into map\nfor i in range(10):\n m.append([chr(ord('A')+i),i])\n \n#User Input\nch='D'\n\n#find value of the given key\np=find(m,ch)\n\nif not(p==-1):\n print p[1]\nelse:\n print \"Key not in the map\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "3\n"
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.12, Page Number:528<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Using a map to create a dictionary'''\n\n#define the function find\ndef find(x,ch):\n for p in x:\n if p[0].get()==ch.get():\n return p\n return -1\n\n\nclass word:\n def __init__(self,s=\"\"):\n self.str=s\n def get(self):\n return self.str\n #must define less than relative to word objects\n def __lt__(self,b):\n return self.str<b.str\n\nclass meaning:\n def __init__(self,s=\"\"):\n self.str=s\n def get(self):\n return self.str\n\ndictionary=[]\n\ndictionary.append([word(\"house\"),meaning(\"A place of dwelling\")])\ndictionary.append([word(\"keyboard\"),meaning(\"An input device\")])\ndictionary.append([word(\"programming\"),meaning(\"The act of writing a program\")])\ndictionary.append([word(\"STL\"),meaning(\"Standard Template Library\")])\n\n#given a word, find meaning\nprint \"Enter word:\"\nstr=\"house\" #User input\n\np=find(dictionary,word(str))\n\nif not(p==-1):\n print \"Definition:\",p[1].get()\nelse:\n print \"Word not in the dictionary.\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter word:\nDefinition: A place of dwelling\n"
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.13, Page Number:532<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstrate count and count_if'''\n\n#This is a unary precdate that determines if character is a vowel\ndef isvowel(ch):\n ch=ch.lower()\n if (ch=='a' or ch=='e'or ch=='i' or ch=='o' or ch=='u'):\n return 1\n else:\n return 0\n\nstr=\"STL programming is powerful.\"\nv=[]\n\nfor i in range(len(str)):\n v.append(str[i])\n \nprint \"Sequence:\",\nfor i in range(len(v)):\n print v[i],\nprint\n\nn=str.count('p')\nprint n,\"characters are p\"\n\n#count if vowel\nn=0\nfor i in v:\n if isvowel(i):\n n+=1\n \nprint n,\"characters are vowels.\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Sequence: S T L p r o g r a m m i n g i s p o w e r f u l .\n2 characters are p\n7 characters are vowels.\n"
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.14, Page Number:534<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstrate remove_copy and replace_copy'''\n\nstr=\"This is a test\"\nv=[]\nv2=[]\n\nfor i in range(len(str)):\n v.append(str[i])\n \n# ***implement remove_copy***\nprint \"Input sequence:\",\nfor i in range(len(v)):\n print v[i],\nprint \n\n#Remove all i's\nv2 = str.replace(\"i\", \"\")\n\nprint \"Result after removing i's: \",\nprint v2,\"\\n\"\n\n\n# ***implement replace_copy***\nprint \"Input sequence:\",\nfor i in range(len(v)):\n print v[i],\nprint \n\n#Replace s's with X's\nv2 = str.replace(\"s\", \"X\")\n\nprint \"Result after replacning s's with X's: \",\nprint v2",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Input sequence: T h i s i s a t e s t\nResult after removing i's: Ths s a test \n\nInput sequence: T h i s i s a t e s t\nResult after replacning s's with X's: ThiX iX a teXt\n"
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.15, Page Number:535<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Reversing a list'''\n\n#Variable declaration\nv=[]\n\nfor i in range(10):\n v.append(i)\n \nprint \"Initial:\",\nfor i in range(len(v)):\n print v[i],\nprint\n\n#Reversing the list\nv.reverse()\n\nprint \"Reversed:\",\nfor i in range(len(v)):\n print v[i],\nprint \n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Initial: 0 1 2 3 4 5 6 7 8 9\nReversed: 9 8 7 6 5 4 3 2 1 0\n"
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.16, Page Number:536<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''An implementation of transform algorithm'''\n\n#A simple transformaton function\ndef xform(i):\n return i*i #square original value\n\n#the transorm function\ndef transform(x,f):\n for i in range(len(x)):\n x[i]= f(x[i])\n \n#Variable declaration\nx1=[]\n\n#put values into list\nfor i in range(10):\n x1.append(i)\n\nprint \"Original contents of x1: \",\nfor p in x1:\n print p,\nprint \n\n#transform x1\np=transform(x1,xform)\n\nprint \"Transformed contents of x1:\",\nfor p in x1:\n print p,",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Original contents of x1: 0 1 2 3 4 5 6 7 8 9\nTransformed contents of x1: 0 1 4 9 16 25 36 49 64 81\n"
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.17, Page Number:540<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A short string demonstration'''\n\nstr1=\"The string class gives \"\nstr2=\"C++ high strng handlng.\"\n\n#assign a string\nstr3=str1\nprint str1,\"\\n\",str3\n\n#Concatenate two strings\nstr3=str1+str2\nprint str3\n\n#Compare strings\nif str3>str1:\n print \"str3 > str1\"\nif str3==str1+str2:\n print \"str3 == str1+str2\"\n \nstr1=\"This is a null-terminated string.\"\nprint str1\n\n#create a string object using another string object\nstr4=str1\nprint str4\n\n#nput a string\nprint \"Enter a string:\"\nstr4=\"Hello\"\nprint str4",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The string class gives \nThe string class gives \nThe string class gives C++ high strng handlng.\nstr3 > str1\nstr3 == str1+str2\nThis is a null-terminated string.\nThis is a null-terminated string.\nEnter a string:\nHello\n"
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.18, Page Number:542<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstrate insert(), erase(), and replace()'''\n\nstr1=\"This is a test\"\nstr2=\"ABCDEFG\"\n\nprint \"Initial strings:\"\nprint \"str1:\",str1\nprint \"str2:\",str2\nprint\n\n#demonstrate insert\nprint \"Insert str2 into str1:\"\nstr1=str1[:5]+str2+str1[5:]\nprint str1,\"\\n\"\n\n#demonstrate erase\nprint \"Remove 7 charecters from str1:\"\nstr1=str[:5]+str[5:]\nprint str1,\"\\n\"\n\n#demonstrate replace\nprint \"Replace 2 characters in str1 with str2:\"\nstr1=str1[:5]+str2+str1[7:]\nprint str1\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Initial strings:\nstr1: This is a test\nstr2: ABCDEFG\n\nInsert str2 into str1:\nThis ABCDEFGis a test \n\nRemove 7 charecters from str1:\nThis is a test \n\nReplace 2 characters in str1 with str2:\nThis ABCDEFG a test\n"
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.19, Page Number:543<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Implementation of find()'''\n\nimport string\n\n#Variable declaration \ns1=\"The string class makes string handling easy.\"\n\ni=string.find(s1,\"class\")\nif not(i==-1):\n print \"Match found at\",i\n print \"Remaining string is:\",\n s2=s1[i:]\n print s2",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Match found at 11\nRemaining string is: class makes string handling easy.\n"
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 21.20, Page Number:545<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Using a map to create a dictionary'''\n\n#define the function find\ndef find(x,ch):\n for p in x:\n if p[0]==ch:\n return p\n return -1\n\n\ndictionary=[]\n\ndictionary.append([\"house\",\"A place of dwelling\"])\ndictionary.append([\"keyboard\",\"An input device\"])\ndictionary.append([\"programming\",\"The act of writing a program\"])\ndictionary.append([\"STL\",\"Standard Template Library\"])\n\n#given a word, find meaning\nprint \"Enter word:\"\nstr=\"house\" #User input\n\np=find(dictionary,str)\n\nif not(p==-1):\n print \"Definition:\",p[1]\nelse:\n print \"Word not in the dictionary.\"",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter word:\nDefinition: A place of dwelling\n"
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file