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')
-rwxr-xr-xC++_from_the_Ground/Chapter_21(1).ipynb1187
1 files changed, 0 insertions, 1187 deletions
diff --git a/C++_from_the_Ground/Chapter_21(1).ipynb b/C++_from_the_Ground/Chapter_21(1).ipynb
deleted file mode 100755
index 76345922..00000000
--- a/C++_from_the_Ground/Chapter_21(1).ipynb
+++ /dev/null
@@ -1,1187 +0,0 @@
-{
- "metadata": {
- "name": "",
- "signature": "sha256:1f6065242a1ea235eb4712b9ec147fb29625df8861301b52ad6c880a94467811"
- },
- "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": [
- " \n",
- "v=[] #Create a zero length vector\n",
- " \n",
- "print \"Size =\",len(v)\n",
- " \n",
- "\n",
- "for i in range(10):\n",
- " v.append(i)\n",
- " \n",
- "#display current size of v\n",
- "print \"Current contents: \"\n",
- "print \"Size now =\",len(v)\n",
- "\n",
- "#display contents of vector\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- " \n",
- "print\n",
- " \n",
- "#put more values onto end of vector\n",
- "#again, vector will grow as needed.\n",
- "for i in range(10):\n",
- " v.append(i+10)\n",
- "#display current size of v\n",
- "print \"Size now =\",len(v)\n",
- "\n",
- "#display contents of vector\n",
- "print \"Current contents:\"\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- "print\n",
- " \n",
- "#change contents of vector\n",
- "for i in range(len(v)):\n",
- " v[i]=v[i]+v[i]\n",
- " \n",
- "#display contents of vector\n",
- "print \"Contents doubled:\"\n",
- "for i in range(len(v)):\n",
- " print v[i],"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Size = 0\n",
- "Current contents: \n",
- "Size now = 10\n",
- "0 1 2 3 4 5 6 7 8 9\n",
- "Size now = 20\n",
- "Current contents:\n",
- "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n",
- "Contents doubled:\n",
- "0 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": [
- "\n",
- "#Variable declaration\n",
- "v=[] #Create a zero length vector\n",
- "\n",
- "#put values onto end of vector\n",
- "for i in range(10):\n",
- " v.append(chr(ord('A')+i))\n",
- " \n",
- "#can access vector contents using subscripts\n",
- "for i in range(len(v)):\n",
- " print v[i], \n",
- "print\n",
- " \n",
- "#access via iterator\n",
- "for 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\n",
- "A 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": [
- "\n",
- "\n",
- "#Variable declaration\n",
- "v=[] #Create a zero length vector\n",
- "\n",
- "#put values onto end of vector\n",
- "for i in range(10):\n",
- " v.append(chr(ord('A')+i))\n",
- " \n",
- "#Display original contents of vector\n",
- "print \"Size =\",len(v)\n",
- "print \"Original contents:\"\n",
- "for i in range(len(v)):\n",
- " print v[i], \n",
- "print \"\\n\"\n",
- " \n",
- "p=2 #point to 3rd element\n",
- "for i in range(10):\n",
- " v.insert(p+i,'X')\n",
- " \n",
- "#display contents after insertion\n",
- "print \"Size after insert =\",len(v)\n",
- "print \"Contents after insert:\"\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- "print \"\\n\"\n",
- "\n",
- "#remove those elements\n",
- "p=2 #point to 3rd element\n",
- "for i in range(10):\n",
- " v.pop(p)\n",
- " \n",
- "#display contents after insertion\n",
- "print \"Size after erase =\",len(v)\n",
- "print \"Contents after insert:\"\n",
- "for i in range(len(v)):\n",
- " print v[i],\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Size = 10\n",
- "Original contents:\n",
- "A B C D E F G H I J \n",
- "\n",
- "Size after insert = 20\n",
- "Contents after insert:\n",
- "A B X X X X X X X X X X C D E F G H I J \n",
- "\n",
- "Size after erase = 10\n",
- "Contents after insert:\n",
- "A 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": [
- "\n",
- "class 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\n",
- "v=[]\n",
- "\n",
- "#add objects to the vector\n",
- "for i in range(10):\n",
- " v.append(three_d(i,i+2,i-3))\n",
- " \n",
- "#Display contents of vector\n",
- "for i in range(len(v)):\n",
- " print v[i], \n",
- "print\n",
- "\n",
- "\n",
- "#Modify objects in a vector\n",
- "for i in range(len(v)):\n",
- " v[i]=v[i]+10 \n",
- "\n",
- "#Display modified vector\n",
- "for 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",
- "\n",
- "10, 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": [
- " \n",
- "v=[]\n",
- "v2=[]\n",
- "\n",
- "for i in range(10):\n",
- " v.append(chr(ord('A')+i))\n",
- " \n",
- "#Display original contents of vector\n",
- "print \"Size =\",len(v)\n",
- "print \"Original contents:\"\n",
- "for i in range(len(v)):\n",
- " print v[i], \n",
- "print \"\\n\"\n",
- "\n",
- "#initialze second vector\n",
- "str=\"-STL Power-\"\n",
- "for 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.\n",
- "p=5\n",
- "p2start=0\n",
- "p2end=len(v2)-1\n",
- "\n",
- "#insert v2 into v\n",
- "for i in range(p2end):\n",
- " v.insert(p+i,v2[p2start+i])\n",
- " \n",
- "#display result\n",
- "print \"Contents of v after inserton:\"\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Size = 10\n",
- "Original contents:\n",
- "A B C D E F G H I J \n",
- "\n",
- "Contents of v after inserton:\n",
- "A 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": [
- "\n",
- "\n",
- "#Variable declaration\n",
- "lst=[] #create an empty list\n",
- "\n",
- "for i in range(10):\n",
- " lst.append(chr(ord('A')+i))\n",
- " \n",
- "print \"Size =\",len(lst)\n",
- "\n",
- "print \"Contents:\",\n",
- "for p in lst:\n",
- " print p, \n",
- "print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Size = 10\n",
- "Contents: 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": [
- "\n",
- "\n",
- "#Variable declaration\n",
- "lst=[] \n",
- "revlst=[]\n",
- "\n",
- "for i in range(10):\n",
- " lst.append(chr(ord('A')+i))\n",
- " \n",
- "print \"Size of list =\",len(lst)\n",
- "\n",
- "print \"Original Contents:\",\n",
- "#Remove elements from lst and put them into revlst in reverse order.\n",
- "for p in lst:\n",
- " print p, \n",
- " revlst.insert(0,p) \n",
- "for i in range(10):\n",
- " lst.pop(0)\n",
- "print \"\\n\"\n",
- "\n",
- "print \"Size of revlst =\",len(revlst)\n",
- "\n",
- "print \"Reversed Contents:\",\n",
- "for p in revlst:\n",
- " print p,"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Size of list = 10\n",
- "Original Contents: A B C D E F G H I J \n",
- "\n",
- "Size of revlst = 10\n",
- "Reversed 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": [
- "\n",
- "\n",
- "import random\n",
- "\n",
- "#Variable declaration\n",
- "lst=[] \n",
- "\n",
- "#create a list of random integers\n",
- "for i in range(10):\n",
- " lst.append(random.randint(0,100))\n",
- "\n",
- "print \"Original Contents:\",\n",
- "for p in lst:\n",
- " print p, \n",
- "print \"\\n\"\n",
- "\n",
- "#sort the list\n",
- "lst.sort()\n",
- "\n",
- "print \"Sorted Contents:\",\n",
- "for 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",
- "\n",
- "Sorted 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": [
- "\n",
- "\n",
- "#Variable declaration\n",
- "lst1=[] \n",
- "lst2=[]\n",
- "\n",
- "for i in xrange(0,10,2):\n",
- " lst1.append(chr(ord('A')+i))\n",
- "for i in xrange(1,11,2):\n",
- " lst2.append(chr(ord('A')+i))\n",
- "\n",
- "print \"Contents of lst1:\",\n",
- "for p in lst1:\n",
- " print p, \n",
- "print \"\\n\"\n",
- "\n",
- "print \"Contents of lst2:\",\n",
- "for p in lst2:\n",
- " print p, \n",
- "print \"\\n\"\n",
- "\n",
- "#merge the lists\n",
- "lst1=lst1+lst2\n",
- "lst1.sort()\n",
- "lst2=[]\n",
- "\n",
- "if lst2==[]:\n",
- " print \"lst2 is now empty\"\n",
- "\n",
- "print \"Contentsof lst1 after merge:\"\n",
- "for 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",
- "\n",
- "Contents of lst2: B D F H J \n",
- "\n",
- "lst2 is now empty\n",
- "Contentsof lst1 after merge:\n",
- "A 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": [
- "\n",
- "class 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\n",
- "lst1=[]\n",
- "for i in range(10):\n",
- " lst1.append(myclass(i,i))\n",
- " \n",
- "print \"First list:\",\n",
- "for p in lst1:\n",
- " print p.getsum(),\n",
- "print\n",
- "\n",
- "#create second list\n",
- "lst2=[]\n",
- "for i in range(10):\n",
- " lst2.append(myclass(i*2,i*3))\n",
- " \n",
- "print \"First list:\",\n",
- "for p in lst2:\n",
- " print p.getsum(),\n",
- "print\n",
- " \n",
- "#Now merge list\n",
- "lst1=lst1+lst2\n",
- "lst1.sort()\n",
- "\n",
- "#Display merge list\n",
- "print \"Merged list:\",\n",
- "for p in lst1:\n",
- " print p.getsum(),\n",
- "print"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "First list: 0 2 4 6 8 10 12 14 16 18\n",
- "First list: 0 5 10 15 20 25 30 35 40 45\n",
- "Merged 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": [
- "\n",
- "#Variable declaration\n",
- "m=[] \n",
- "\n",
- "#define the function find\n",
- "def 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\n",
- "for i in range(10):\n",
- " m.append([chr(ord('A')+i),i])\n",
- " \n",
- "#User Input\n",
- "ch='D'\n",
- "\n",
- "#find value of the given key\n",
- "p=find(m,ch)\n",
- "\n",
- "if not(p==-1):\n",
- " print p[1]\n",
- "else:\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": [
- " \n",
- "def find(x,ch):\n",
- " for p in x:\n",
- " if p[0].get()==ch.get():\n",
- " return p\n",
- " return -1\n",
- "\n",
- "\n",
- "class 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",
- "\n",
- "class meaning:\n",
- " def __init__(self,s=\"\"):\n",
- " self.str=s\n",
- " def get(self):\n",
- " return self.str\n",
- "\n",
- "dictionary=[]\n",
- "\n",
- "dictionary.append([word(\"house\"),meaning(\"A place of dwelling\")])\n",
- "dictionary.append([word(\"keyboard\"),meaning(\"An input device\")])\n",
- "dictionary.append([word(\"programming\"),meaning(\"The act of writing a program\")])\n",
- "dictionary.append([word(\"STL\"),meaning(\"Standard Template Library\")])\n",
- "\n",
- "#given a word, find meaning\n",
- "print \"Enter word:\"\n",
- "str=\"house\" #User input\n",
- "\n",
- "p=find(dictionary,word(str))\n",
- "\n",
- "if not(p==-1):\n",
- " print \"Definition:\",p[1].get()\n",
- "else:\n",
- " print \"Word not in the dictionary.\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter word:\n",
- "Definition: 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": [
- "def 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",
- "\n",
- "str=\"STL programming is powerful.\"\n",
- "v=[]\n",
- "\n",
- "for i in range(len(str)):\n",
- " v.append(str[i])\n",
- " \n",
- "print \"Sequence:\",\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- "print\n",
- "\n",
- "n=str.count('p')\n",
- "print n,\"characters are p\"\n",
- "\n",
- "#count if vowel\n",
- "n=0\n",
- "for i in v:\n",
- " if isvowel(i):\n",
- " n+=1\n",
- " \n",
- "print 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 .\n",
- "2 characters are p\n",
- "7 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": [
- "\n",
- "\n",
- "str=\"This is a test\"\n",
- "v=[]\n",
- "v2=[]\n",
- "\n",
- "for i in range(len(str)):\n",
- " v.append(str[i])\n",
- " \n",
- "# ***implement remove_copy***\n",
- "print \"Input sequence:\",\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- "print \n",
- "\n",
- "#Remove all i's\n",
- "v2 = str.replace(\"i\", \"\")\n",
- "\n",
- "print \"Result after removing i's: \",\n",
- "print v2,\"\\n\"\n",
- "\n",
- "\n",
- "# ***implement replace_copy***\n",
- "print \"Input sequence:\",\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- "print \n",
- "\n",
- "#Replace s's with X's\n",
- "v2 = str.replace(\"s\", \"X\")\n",
- "\n",
- "print \"Result after replacning s's with X's: \",\n",
- "print v2"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Input sequence: T h i s i s a t e s t\n",
- "Result after removing i's: Ths s a test \n",
- "\n",
- "Input sequence: T h i s i s a t e s t\n",
- "Result 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": [
- " \n",
- "v=[]\n",
- "\n",
- "for i in range(10):\n",
- " v.append(i)\n",
- " \n",
- "print \"Initial:\",\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- "print\n",
- "\n",
- "#Reversing the list\n",
- "v.reverse()\n",
- "\n",
- "print \"Reversed:\",\n",
- "for i in range(len(v)):\n",
- " print v[i],\n",
- "print \n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Initial: 0 1 2 3 4 5 6 7 8 9\n",
- "Reversed: 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": [
- " \n",
- "def xform(i):\n",
- " return i*i #square original value\n",
- "\n",
- "#the transorm function\n",
- "def transform(x,f):\n",
- " for i in range(len(x)):\n",
- " x[i]= f(x[i])\n",
- " \n",
- "#Variable declaration\n",
- "x1=[]\n",
- "\n",
- "#put values into list\n",
- "for i in range(10):\n",
- " x1.append(i)\n",
- "\n",
- "print \"Original contents of x1: \",\n",
- "for p in x1:\n",
- " print p,\n",
- "print \n",
- "\n",
- "#transform x1\n",
- "p=transform(x1,xform)\n",
- "\n",
- "print \"Transformed contents of x1:\",\n",
- "for 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\n",
- "Transformed 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": [
- "\n",
- "str1=\"The string class gives \"\n",
- "str2=\"C++ high strng handlng.\"\n",
- "\n",
- "#assign a string\n",
- "str3=str1\n",
- "print str1,\"\\n\",str3\n",
- "\n",
- "#Concatenate two strings\n",
- "str3=str1+str2\n",
- "print str3\n",
- "\n",
- "#Compare strings\n",
- "if str3>str1:\n",
- " print \"str3 > str1\"\n",
- "if str3==str1+str2:\n",
- " print \"str3 == str1+str2\"\n",
- " \n",
- "str1=\"This is a null-terminated string.\"\n",
- "print str1\n",
- "\n",
- "#create a string object using another string object\n",
- "str4=str1\n",
- "print str4\n",
- "\n",
- "#nput a string\n",
- "print \"Enter a string:\"\n",
- "str4=\"Hello\"\n",
- "print str4"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The string class gives \n",
- "The string class gives \n",
- "The string class gives C++ high strng handlng.\n",
- "str3 > str1\n",
- "str3 == str1+str2\n",
- "This is a null-terminated string.\n",
- "This is a null-terminated string.\n",
- "Enter a string:\n",
- "Hello\n"
- ]
- }
- ],
- "prompt_number": 19
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 21.18, Page Number:542<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "str1=\"This is a test\"\n",
- "str2=\"ABCDEFG\"\n",
- "\n",
- "print \"Initial strings:\"\n",
- "print \"str1:\",str1\n",
- "print \"str2:\",str2\n",
- "print\n",
- "\n",
- "#demonstrate insert\n",
- "print \"Insert str2 into str1:\"\n",
- "str1=str1[:5]+str2+str1[5:]\n",
- "print str1,\"\\n\"\n",
- "\n",
- "#demonstrate erase\n",
- "print \"Remove 7 charecters from str1:\"\n",
- "str1=str[:5]+str[5:]\n",
- "print str1,\"\\n\"\n",
- "\n",
- "#demonstrate replace\n",
- "print \"Replace 2 characters in str1 with str2:\"\n",
- "str1=str1[:5]+str2+str1[7:]\n",
- "print str1\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Initial strings:\n",
- "str1: This is a test\n",
- "str2: ABCDEFG\n",
- "\n",
- "Insert str2 into str1:\n",
- "This ABCDEFGis a test \n",
- "\n",
- "Remove 7 charecters from str1:\n",
- "This is a test \n",
- "\n",
- "Replace 2 characters in str1 with str2:\n",
- "This 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": [
- "\n",
- "import string\n",
- "\n",
- "#Variable declaration \n",
- "s1=\"The string class makes string handling easy.\"\n",
- "\n",
- "i=string.find(s1,\"class\")\n",
- "if 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\n",
- "Remaining 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": [
- " \n",
- "def find(x,ch):\n",
- " for p in x:\n",
- " if p[0]==ch:\n",
- " return p\n",
- " return -1\n",
- "\n",
- "\n",
- "dictionary=[]\n",
- "\n",
- "dictionary.append([\"house\",\"A place of dwelling\"])\n",
- "dictionary.append([\"keyboard\",\"An input device\"])\n",
- "dictionary.append([\"programming\",\"The act of writing a program\"])\n",
- "dictionary.append([\"STL\",\"Standard Template Library\"])\n",
- "\n",
- "#given a word, find meaning\n",
- "print \"Enter word:\"\n",
- "str=\"house\" #User input\n",
- "\n",
- "p=find(dictionary,str)\n",
- "\n",
- "if not(p==-1):\n",
- " print \"Definition:\",p[1]\n",
- "else:\n",
- " print \"Word not in the dictionary.\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter word:\n",
- "Definition: 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