summaryrefslogtreecommitdiff
path: root/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb')
-rw-r--r--Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb408
1 files changed, 386 insertions, 22 deletions
diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb
index 1618010d..2b06bed6 100644
--- a/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb
+++ b/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch14"
+ "name": "",
+ "signature": "sha256:a2642ced32385d2b1020b66a0cd2578b114803009d5270f03f2b2323505ad39e"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.1 Using a vector of strings\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)",
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n"
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
}
],
"prompt_number": 1
@@ -25,14 +54,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.2 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)",
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n"
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
}
],
"prompt_number": 2
@@ -40,14 +97,43 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.3 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in range(len(v)):\n print v[i]\n\nv = []\nload(v)\nprint_(v)",
+ "input": [
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in range(len(v)):\n",
+ " print v[i]\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n"
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
}
],
"prompt_number": 3
@@ -55,14 +141,45 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.4 Using the Generic sort() Algorithm\n'''\n\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\n",
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\n"
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n"
+ ]
}
],
"prompt_number": 4
@@ -70,14 +187,52 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.5 Using the Assignment Operator to Duplicate a vector\n'''\n#This program demonstrates that one vector can be assigned to another.\n\n\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nw = v\nprint_(v)\nprint_(w)",
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "w = v\n",
+ "print_(v)\n",
+ "print_(w)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\nJapan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n"
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n",
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n"
+ ]
}
],
"prompt_number": 5
@@ -85,14 +240,58 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.6 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\nprint \"v.front() = \" + v[0]\nprint \"v.back() = \" + v.pop(-1) \nprint \"v.back() = \" + v.pop(-1) \nprint \"v.back() = \" + v[-1] \nprint_(v)",
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n",
+ "print \"v.front() = \" + v[0]\n",
+ "print \"v.back() = \" + v.pop(-1) \n",
+ "print \"v.back() = \" + v.pop(-1) \n",
+ "print \"v.back() = \" + v[-1] \n",
+ "print_(v)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nv.front() = Chile\nv.back() = Zaire\nv.back() = Spain\nv.back() = Nepal\nChile\nEgypt\nItaly\nJapan\nKenya\nNepal\n"
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n",
+ "v.front() = Chile\n",
+ "v.back() = Zaire\n",
+ "v.back() = Spain\n",
+ "v.back() = Nepal\n",
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n"
+ ]
}
],
"prompt_number": 6
@@ -100,14 +299,52 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.7 Using the erase() Function\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\nv.pop(2) # removes Italy\nv.pop(-2) # removes Spain\nprint_(v)",
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n",
+ "v.pop(2) # removes Italy\n",
+ "v.pop(-2) # removes Spain\n",
+ "print_(v)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nChile\nEgypt\nJapan\nKenya\nNepal\nZaire\n"
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n",
+ "Chile\n",
+ "Egypt\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Zaire\n"
+ ]
}
],
"prompt_number": 7
@@ -115,14 +352,60 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.8 Using the insert() Function\n'''\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\nv = []\nload(v)\nv.sort()\nprint_(v)\nr = []\nfor i in range(2,len(v)-2):\n r.append(v[i]) #removes the segment Italy..Nepal\n \nfor i in r:\n v.remove(i)\nprint_(v)\nv.insert(2,\"India\")\nprint_(v)",
+ "input": [
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "v = []\n",
+ "load(v)\n",
+ "v.sort()\n",
+ "print_(v)\n",
+ "r = []\n",
+ "for i in range(2,len(v)-2):\n",
+ " r.append(v[i]) #removes the segment Italy..Nepal\n",
+ " \n",
+ "for i in r:\n",
+ " v.remove(i)\n",
+ "print_(v)\n",
+ "v.insert(2,\"India\")\n",
+ "print_(v)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nChile\nEgypt\nSpain\nZaire\nChile\nEgypt\nIndia\nSpain\nZaire\n"
+ "text": [
+ "Chile\n",
+ "Egypt\n",
+ "Italy\n",
+ "Japan\n",
+ "Kenya\n",
+ "Nepal\n",
+ "Spain\n",
+ "Zaire\n",
+ "Chile\n",
+ "Egypt\n",
+ "Spain\n",
+ "Zaire\n",
+ "Chile\n",
+ "Egypt\n",
+ "India\n",
+ "Spain\n",
+ "Zaire\n"
+ ]
}
],
"prompt_number": 8
@@ -130,14 +413,64 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.9 Using the find() Function\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n v.append(\"India\")\n v.append(\"China\")\n v.append(\"Malta\")\n v.append(\"Syria\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)\negypt = v.index('Egypt')\nmalta = v.index('Malta')\nw = v[egypt:malta+1]\nw.sort()\nprint_(w)",
+ "input": [
+ "\n",
+ "\n",
+ "def load(v):\n",
+ " v.append(\"Japan\")\n",
+ " v.append(\"Italy\")\n",
+ " v.append(\"Spain\")\n",
+ " v.append(\"Egypt\")\n",
+ " v.append(\"Chile\")\n",
+ " v.append(\"Zaire\")\n",
+ " v.append(\"Nepal\")\n",
+ " v.append(\"Kenya\")\n",
+ " v.append(\"India\")\n",
+ " v.append(\"China\")\n",
+ " v.append(\"Malta\")\n",
+ " v.append(\"Syria\")\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i\n",
+ "\n",
+ "v = []\n",
+ "load(v)\n",
+ "print_(v)\n",
+ "egypt = v.index('Egypt')\n",
+ "malta = v.index('Malta')\n",
+ "w = v[egypt:malta+1]\n",
+ "w.sort()\n",
+ "print_(w)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\nIndia\nChina\nMalta\nSyria\nChile\nChina\nEgypt\nIndia\nKenya\nMalta\nNepal\nZaire\n"
+ "text": [
+ "Japan\n",
+ "Italy\n",
+ "Spain\n",
+ "Egypt\n",
+ "Chile\n",
+ "Zaire\n",
+ "Nepal\n",
+ "Kenya\n",
+ "India\n",
+ "China\n",
+ "Malta\n",
+ "Syria\n",
+ "Chile\n",
+ "China\n",
+ "Egypt\n",
+ "India\n",
+ "Kenya\n",
+ "Malta\n",
+ "Nepal\n",
+ "Zaire\n"
+ ]
}
],
"prompt_number": 9
@@ -145,14 +478,45 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 14.10\n'''\ndef copy(v,x,n):\n for i in x:\n v.append(i)\n\ndef projection(v,b):\n v_size = len(v)\n w = []\n for i in range(0,v_size):\n if b[i]:\n w.append(v[i])\n return w\n\ndef print_(v):\n for i in v:\n print i,\n print ''\n\nx = [ 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 ]\nv = []\ncopy(v, x, 8)\ny = [ False, True, False, True, True, True, False, True ]\nb = []\ncopy(b, y, 8)\nw = projection(v, b)\nprint_(v)\nprint_(w)",
+ "input": [
+ "\n",
+ "def copy(v,x,n):\n",
+ " for i in x:\n",
+ " v.append(i)\n",
+ "\n",
+ "def projection(v,b):\n",
+ " v_size = len(v)\n",
+ " w = []\n",
+ " for i in range(0,v_size):\n",
+ " if b[i]:\n",
+ " w.append(v[i])\n",
+ " return w\n",
+ "\n",
+ "def print_(v):\n",
+ " for i in v:\n",
+ " print i,\n",
+ " print ''\n",
+ "\n",
+ "x = [ 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 ]\n",
+ "v = []\n",
+ "copy(v, x, 8)\n",
+ "y = [ False, True, False, True, True, True, False, True ]\n",
+ "b = []\n",
+ "copy(b, y, 8)\n",
+ "w = projection(v, b)\n",
+ "print_(v)\n",
+ "print_(w)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n33.3 55.5 66.6 77.7 99.9 \n"
+ "text": [
+ "22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n",
+ "33.3 55.5 66.6 77.7 99.9 \n"
+ ]
}
],
"prompt_number": 10
@@ -160,7 +524,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []