"
]
},
{
"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": [
"
Example 21.2, Page Number:508
"
]
},
{
"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": [
"
Example 21.3, Page Number:509
"
]
},
{
"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": [
"
"
]
},
{
"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": [
"
Example 21.6, Page Number:517
"
]
},
{
"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": [
"
Example 21.7, Page Number:518
"
]
},
{
"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": [
"
"
]
},
{
"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": [
"
"
]
},
{
"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": [
"
Example 21.12, Page Number:528
"
]
},
{
"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.strExample 21.13, Page Number:532
"
]
},
{
"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": [
"
Example 21.14, Page Number:534
"
]
},
{
"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": [
"