summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)
diff options
context:
space:
mode:
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)')
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb201
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb532
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb529
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb589
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb259
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb410
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb150
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb88
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb395
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb597
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb899
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb1058
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb675
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb394
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb599
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb224
16 files changed, 6627 insertions, 972 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb
index f6589ebe..6d38324f 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch1"
+ "name": "",
+ "signature": "sha256:2fa8b19d5030cf6dee0c92c2ee4889d5f3659cfd258460db8d8dc9ca2e443363"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,20 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "\"\"\"\nExample 1.1\n\"\"\"\nprint \"Hello, World!\\n\"\n",
+ "input": [
+ "\n",
+ "print \"Hello, World!\\n\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Hello, World!\n\n"
+ "text": [
+ "Hello, World!\n",
+ "\n"
+ ]
}
],
"prompt_number": 1
@@ -25,14 +32,22 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nexample 1.2\nprints 'hello world' with comments.\n'''\n\n# prints \"Hello, World!\":\nprint \"Hello, World!\\n\"",
+ "input": [
+ "\n",
+ "\n",
+ "# prints \"Hello, World!\":\n",
+ "print \"Hello, World!\\n\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Hello, World!\n\n"
+ "text": [
+ "Hello, World!\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -40,14 +55,22 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nexample 1.3\nanother version of hello world.\n'''\n\n# prints \"Hello, World!\":\nprint \"Hel\" + \"lo, Wo\" + \"rld!\" \n\n",
+ "input": [
+ "\n",
+ "\n",
+ "# prints \"Hello, World!\":\n",
+ "print \"Hel\" + \"lo, Wo\" + \"rld!\" \n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Hello, World!\n"
+ "text": [
+ "Hello, World!\n"
+ ]
}
],
"prompt_number": 3
@@ -55,14 +78,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "\"\"\"\nexample 1.4\nanother version of hello world.\n\"\"\"\n\n# prints \"Hello, World!\":\nprint \"Hello, W\" + 'o' + \"rld\" + '!' ",
+ "input": [
+ "\n",
+ "\n",
+ "# prints \"Hello, World!\":\n",
+ "print \"Hello, W\" + 'o' + \"rld\" + '!' "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Hello, World!\n"
+ "text": [
+ "Hello, World!\n"
+ ]
}
],
"prompt_number": 4
@@ -70,14 +100,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 1.5 Inserting Numeric Literals into the Standard Output Stream\n'''\n\n# prints \"The Millennium ends Dec 31 2000.\":\nprint \"The Millennium ends Dec %d %d \" %(31,2000)",
+ "input": [
+ "\n",
+ "\n",
+ "# prints \"The Millennium ends Dec 31 2000.\":\n",
+ "print \"The Millennium ends Dec %d %d \" %(31,2000)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The Millennium ends Dec 31 2000 \n"
+ "text": [
+ "The Millennium ends Dec 31 2000 \n"
+ ]
}
],
"prompt_number": 5
@@ -85,14 +122,24 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 1.6 Using Integer Variables\nIn this example, the integer 44 is assigned to the variable m, and the value of the expression m + 33\nis assigned to the variable n:\n'''\n# prints \"m = 44 and n = 77\":\n\nm = 44 # assigns the value 44 to the variable m\nprint \"m = %d \" % m,\nn = m + 33 # assigns the value 77 to the variable n\nprint \"and n = %d \" % n",
+ "input": [
+ "\n",
+ "# prints \"m = 44 and n = 77\":\n",
+ "\n",
+ "m = 44 # assigns the value 44 to the variable m\n",
+ "print \"m = %d \" % m,\n",
+ "n = m + 33 # assigns the value 77 to the variable n\n",
+ "print \"and n = %d \" % n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "m = 44 and n = 77 \n"
+ "text": [
+ "m = 44 and n = 77 \n"
+ ]
}
],
"prompt_number": 6
@@ -100,14 +147,22 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 1.7 A Program's Tokens\n'''\n\n# prints \"n = 44:\nn=44\nprint \"n = %d\" % n",
+ "input": [
+ "\n",
+ "\n",
+ "# prints \"n = 44:\n",
+ "n=44\n",
+ "print \"n = %d\" % n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 44\n"
+ "text": [
+ "n = 44\n"
+ ]
}
],
"prompt_number": 7
@@ -115,14 +170,22 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 1.8 \n'''\n\n# Python does not have semicolons so wont give any errors.\nn=44\nprint \"n = %d\" % n ",
+ "input": [
+ "\n",
+ "\n",
+ "# Python does not have semicolons so wont give any errors.\n",
+ "n=44\n",
+ "print \"n = %d\" % n "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 44\n"
+ "text": [
+ "n = 44\n"
+ ]
}
],
"prompt_number": 8
@@ -130,14 +193,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 1.9 Initializing Variables\nThis program contains one variable that is not initialized and one that is initialized.\nNote : This program would give you differ output then c gives.\n'''\n\n# prints \"m = ?? and n = 44\":\nm = 0 #In python we do not have declaration of variables, we just initialize it and use it.\nn=44\nprint \"m = %d and n = %d\" %(m,n)",
+ "input": [
+ "\n",
+ "\n",
+ "# prints \"m = ?? and n = 44\":\n",
+ "m = 0 #In python we do not have declaration of variables, we just initialize it and use it.\n",
+ "n=44\n",
+ "print \"m = %d and n = %d\" %(m,n)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "m = 0 and n = 44\n"
+ "text": [
+ "m = 0 and n = 44\n"
+ ]
}
],
"prompt_number": 9
@@ -145,7 +217,16 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 1.10 The const Specifier\nThis program illustrates constant definitions:\n'''\n\n# defines constants; has no output:\nBEEP = '\\b'\nMAXINT = 2147483647\nN = MAXINT/2\nKM_PER_MI = 1.60934\nPI = 3.14159265358979323846\n",
+ "input": [
+ "\n",
+ "\n",
+ "# defines constants; has no output:\n",
+ "BEEP = '\\b'\n",
+ "MAXINT = 2147483647\n",
+ "N = MAXINT/2\n",
+ "KM_PER_MI = 1.60934\n",
+ "PI = 3.14159265358979323846\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -154,83 +235,133 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 1.11 Using the Input Operator\n'''\n\n# tests the input of integers, floats, and characters:\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\nprint \"m = %d , n = %d \" %(m,n)\n\nprint \"Enter three decimal numbers: \"\nx = float(raw_input())\ny = float(raw_input())\nz = float(raw_input())\n\nprint \"x = %f , y = %f , z = %f\" %(x,y,z)\n\nprint \"Enter four characters: \";\nc1 = raw_input()\nc2 = raw_input()\nc3 = raw_input()\nc4 = raw_input()\nprint \"c1 = \" + c1 + \", c2 = \" + c2 + \", c3 = \" + c3 + \", c4 = \" + c4 ",
+ "input": [
+ "\n",
+ "\n",
+ "# tests the input of integers, floats, and characters:\n",
+ "print \"Enter two integers: \"\n",
+ "m = int(raw_input())\n",
+ "n = int(raw_input())\n",
+ "print \"m = %d , n = %d \" %(m,n)\n",
+ "\n",
+ "print \"Enter three decimal numbers: \"\n",
+ "x = float(raw_input())\n",
+ "y = float(raw_input())\n",
+ "z = float(raw_input())\n",
+ "\n",
+ "print \"x = %f , y = %f , z = %f\" %(x,y,z)\n",
+ "\n",
+ "print \"Enter four characters: \";\n",
+ "c1 = raw_input()\n",
+ "c2 = raw_input()\n",
+ "c3 = raw_input()\n",
+ "c4 = raw_input()\n",
+ "print \"c1 = \" + c1 + \", c2 = \" + c2 + \", c3 = \" + c3 + \", c4 = \" + c4 "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two integers: \n"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "22\n"
+ "text": [
+ "22\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "m = 22 , n = 44 \nEnter three decimal numbers: \n"
+ "text": [
+ "m = 22 , n = 44 \n",
+ "Enter three decimal numbers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2.2\n"
+ "text": [
+ "2.2\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4.4\n"
+ "text": [
+ "4.4\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "6.6\n"
+ "text": [
+ "6.6\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 2.200000 , y = 4.400000 , z = 6.600000\nEnter four characters: \n"
+ "text": [
+ "x = 2.200000 , y = 4.400000 , z = 6.600000\n",
+ "Enter four characters: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "A\n"
+ "text": [
+ "A\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "B\n"
+ "text": [
+ "B\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "C\n"
+ "text": [
+ "C\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "D\n"
+ "text": [
+ "D\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "c1 = A, c2 = B, c3 = C, c4 = D\n"
+ "text": [
+ "c1 = A, c2 = B, c3 = C, c4 = D\n"
+ ]
}
],
"prompt_number": 11
@@ -238,7 +369,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb
index 4b6ac8a8..11bfc69a 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch10"
+ "name": "",
+ "signature": "sha256:80ec9afde7f244065f945a9a64c1b3fb1bcbaba0a8461385eab8b48cd3e4fc70"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,41 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.1 Implementing the Ratio Class\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n \nx = Ratio()\nx.assign(22,7)\nprint \"x = \",\nx.print_()\nprint \" = \" , x.convert() \nx.invert()\nprint \"1/x = \",\nx.print_()\n",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self):\n",
+ " self.num = 0\n",
+ " self.den = 0\n",
+ " def assign(self,n,d):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def convert(self):\n",
+ " return float(self.num)/self.den\n",
+ " def invert(self):\n",
+ " self.num,self.den = self.den,self.num\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ " \n",
+ "x = Ratio()\n",
+ "x.assign(22,7)\n",
+ "print \"x = \",\n",
+ "x.print_()\n",
+ "print \" = \" , x.convert() \n",
+ "x.invert()\n",
+ "print \"1/x = \",\n",
+ "x.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 22 / 7 = 3.14285714286\n1/x = 7 / 22\n"
+ "text": [
+ "x = 22 / 7 = 3.14285714286\n",
+ "1/x = 7 / 22\n"
+ ]
}
],
"prompt_number": 1
@@ -25,7 +53,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.2 Class example\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n",
+ "input": [
+ "class Ratio:\n",
+ " def __init__(self):\n",
+ " self.num = 0\n",
+ " self.den = 0\n",
+ " def assign(self,n,d):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def convert(self):\n",
+ " return float(self.num)/self.den\n",
+ " def invert(self):\n",
+ " self.num,self.den = self.den,self.num\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -34,14 +76,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.3\nA Constructor Function for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n,d):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio(-1,3)\ny = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \" and y = \",\ny.print_()\n",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n,d):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n",
+ "x = Ratio(-1,3)\n",
+ "y = Ratio(22,7)\n",
+ "print \"x = \",\n",
+ "x.print_()\n",
+ "print \" and y = \",\n",
+ "y.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = -1 / 3 and y = 22 / 7\n"
+ "text": [
+ "x = -1 / 3 and y = 22 / 7\n"
+ ]
}
],
"prompt_number": 3
@@ -49,14 +108,43 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.4 Constructors to the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \"\\ny = \",\ny.print_()\nprint \"\\nz = \",\nz.print_()",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " if n==None:\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " elif d==None:\n",
+ " self.num = n\n",
+ " self.den = 1\n",
+ " else:\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n",
+ "x = Ratio()\n",
+ "y = Ratio(4)\n",
+ "z = Ratio(22,7)\n",
+ "print \"x = \",\n",
+ "x.print_()\n",
+ "print \"\\ny = \",\n",
+ "y.print_()\n",
+ "print \"\\nz = \",\n",
+ "z.print_()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 0 / 1 \ny = 4 / 1 \nz = 22 / 7\n"
+ "text": [
+ "x = 0 / 1 \n",
+ "y = 4 / 1 \n",
+ "z = 22 / 7\n"
+ ]
}
],
"prompt_number": 4
@@ -64,7 +152,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.5\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " if n==None:\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " elif d==None:\n",
+ " self.num = n\n",
+ " self.den = 1\n",
+ " else:\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -73,7 +177,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.6 Using Default Parameter Values in the Ratio Class Constructor\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)",
+ "input": [
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n",
+ "\n",
+ "x = Ratio()\n",
+ "y = Ratio(4)\n",
+ "z = Ratio(22,7)"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -82,14 +200,32 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.7 Access Functions in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio(22,7)\nprint x.numerator() , '/' , x.denominator() ",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def numerator(self):\n",
+ " return self.num\n",
+ " def denominator(self):\n",
+ " return self.den\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "print x.numerator() , '/' , x.denominator() "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "22 / 7\n"
+ "text": [
+ "22 / 7\n"
+ ]
}
],
"prompt_number": 7
@@ -97,14 +233,64 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,-360)\nx.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def numerator(self):\n",
+ " return self.num\n",
+ " def denominator(self):\n",
+ " return self.den\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(100,-360)\n",
+ "x.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "-5 / 18\n"
+ "text": [
+ "-5 / 18\n"
+ ]
}
],
"prompt_number": 8
@@ -112,14 +298,72 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,360)\ny = Ratio(x)\nprint \"x = \",\nx.print_()\nprint \"y = \",\ny.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=None):\n",
+ " if d == None:\n",
+ " self.num = n.num\n",
+ " self.den = n.den\n",
+ " else: \n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def numerator(self):\n",
+ " return self.num\n",
+ " def denominator(self):\n",
+ " return self.den\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(100,360)\n",
+ "y = Ratio(x)\n",
+ "print \"x = \",\n",
+ "x.print_()\n",
+ "print \"y = \",\n",
+ "y.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 5 / 18 y = 5 / 18\n"
+ "text": [
+ "x = 5 / 18 y = 5 / 18\n"
+ ]
}
],
"prompt_number": 9
@@ -127,14 +371,74 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.10 Tracing Calls to the Copy Constructor\n'''\n'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\n'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n print \"COPY CONSTRUCTOR CALLED\"\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\ndef f(r):\n s = Ratio(r)\n\nx = Ratio(22,7)\ny = Ratio(x) #calls the copy constructor, copying x to y\nf(y)\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=None):\n",
+ " if d == None:\n",
+ " print \"COPY CONSTRUCTOR CALLED\"\n",
+ " self.num = n.num\n",
+ " self.den = n.den\n",
+ " else: \n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def numerator(self):\n",
+ " return self.num\n",
+ " def denominator(self):\n",
+ " return self.den\n",
+ " def print_(self):\n",
+ " print self.num , '/' , self.den ,\n",
+ "\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "def f(r):\n",
+ " s = Ratio(r)\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(x) #calls the copy constructor, copying x to y\n",
+ "f(y)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "COPY CONSTRUCTOR CALLED\nCOPY CONSTRUCTOR CALLED\n"
+ "text": [
+ "COPY CONSTRUCTOR CALLED\n",
+ "COPY CONSTRUCTOR CALLED\n"
+ ]
}
],
"prompt_number": 12
@@ -142,14 +446,33 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.11 Including a Destructor in the Ratio Class\nNote : Python objects die when program gets exit.\n'''\n\nclass Ratio:\n def __init__(self):\n print \"OBJECT IS BORN.\"\n def __del__(self):\n print \"OBJECT DIES.\"\n\nx = Ratio()\nprint \"Now x is alive.\"\nprint \"Now between blocks.\"\ny = Ratio()\nprint \"Now y is alive.\"\n",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self):\n",
+ " print \"OBJECT IS BORN.\"\n",
+ " def __del__(self):\n",
+ " print \"OBJECT DIES.\"\n",
+ "\n",
+ "x = Ratio()\n",
+ "print \"Now x is alive.\"\n",
+ "print \"Now between blocks.\"\n",
+ "y = Ratio()\n",
+ "print \"Now y is alive.\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "OBJECT IS BORN.\nNow x is alive.\nNow between blocks.\nOBJECT IS BORN.\nNow y is alive.\n"
+ "text": [
+ "OBJECT IS BORN.\n",
+ "Now x is alive.\n",
+ "Now between blocks.\n",
+ "OBJECT IS BORN.\n",
+ "Now y is alive.\n"
+ ]
}
],
"prompt_number": 13
@@ -157,14 +480,28 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.12\n'''\nclass X:\n def __init(self):\n data = 0\n\np = X()\np.data = 22\nprint \"p.data = \" , p.data , \" = \" , p.data\np.data = 44\nprint \" p.data = \" , p.data , \" = \" , p.data ",
+ "input": [
+ "\n",
+ "class X:\n",
+ " def __init(self):\n",
+ " data = 0\n",
+ "\n",
+ "p = X()\n",
+ "p.data = 22\n",
+ "print \"p.data = \" , p.data , \" = \" , p.data\n",
+ "p.data = 44\n",
+ "print \" p.data = \" , p.data , \" = \" , p.data "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "p.data = 22 = 22\n p.data = 44 = 44\n"
+ "text": [
+ "p.data = 22 = 22\n",
+ " p.data = 44 = 44\n"
+ ]
}
],
"prompt_number": 14
@@ -172,7 +509,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.13 A Node Class for Linked Lists\n'''\nclass Node:\n def __init__(self,d,q=None):\n self.data = d\n self.next = q\n\nn = int(raw_input())\nq = Node(n)\nwhile True:\n n = int(raw_input())\n if n<=0:\n break\n p = Node(n, q)\n q = p\nk = p\nwhile k != None:\n print k.data , '->' , \n k = k.next\nprint '*'\n",
+ "input": [
+ "\n",
+ "class Node:\n",
+ " def __init__(self,d,q=None):\n",
+ " self.data = d\n",
+ " self.next = q\n",
+ "\n",
+ "n = int(raw_input())\n",
+ "q = Node(n)\n",
+ "while True:\n",
+ " n = int(raw_input())\n",
+ " if n<=0:\n",
+ " break\n",
+ " p = Node(n, q)\n",
+ " q = p\n",
+ "k = p\n",
+ "while k != None:\n",
+ " print k.data , '->' , \n",
+ " k = k.next\n",
+ "print '*'\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -180,48 +537,64 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "22\n"
+ "text": [
+ "22\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "66\n"
+ "text": [
+ "66\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *\n"
+ "text": [
+ "77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *\n"
+ ]
}
],
"prompt_number": 15
@@ -229,14 +602,41 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.14 \nPython does not support static data type.\nPython automatically handles local variable so we need not to delete it.\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , count , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , count , 'widgets' \nprint \"Now there are \" , count , 'widgets'\ny = Widget()\nprint \"Now there are \" , count , 'widgets'\n",
+ "input": [
+ "\n",
+ "count = 0\n",
+ "class Widget:\n",
+ " def __init__(self):\n",
+ " global count\n",
+ " count += 1\n",
+ " \n",
+ "w = Widget()\n",
+ "x = Widget()\n",
+ "print \"Now there are \" , count , 'widgets'\n",
+ "if True:\n",
+ " w = Widget()\n",
+ " x = Widget()\n",
+ " y = Widget()\n",
+ " z = Widget()\n",
+ " print \"Now there are\" , count , 'widgets' \n",
+ "print \"Now there are \" , count , 'widgets'\n",
+ "y = Widget()\n",
+ "print \"Now there are \" , count , 'widgets'\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "OBJECT DIES.\nNow there are 2 widgets\nOBJECT DIES.\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n"
+ "text": [
+ "OBJECT DIES.\n",
+ "Now there are 2 widgets\n",
+ "OBJECT DIES.\n",
+ "Now there are 6 widgets\n",
+ "Now there are 6 widgets\n",
+ "Now there are 7 widgets\n"
+ ]
}
],
"prompt_number": 16
@@ -244,14 +644,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.15 \n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\n",
+ "input": [
+ "\n",
+ "count = 0\n",
+ "class Widget:\n",
+ " def __init__(self):\n",
+ " global count\n",
+ " count += 1\n",
+ " def numWidgets(self):\n",
+ " global count\n",
+ " return count\n",
+ " \n",
+ "w = Widget()\n",
+ "x = Widget()\n",
+ "print \"Now there are \" , w.numWidgets() , 'widgets'\n",
+ "if True:\n",
+ " w = Widget()\n",
+ " x = Widget()\n",
+ " y = Widget()\n",
+ " z = Widget()\n",
+ " print \"Now there are\" , w.numWidgets() , 'widgets' \n",
+ "print \"Now there are \" , w.numWidgets() , 'widgets'\n",
+ "y = Widget()\n",
+ "print \"Now there are \" , w.numWidgets() , 'widgets'\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n"
+ "text": [
+ "Now there are 2 widgets\n",
+ "Now there are 6 widgets\n",
+ "Now there are 6 widgets\n",
+ "Now there are 7 widgets\n"
+ ]
}
],
"prompt_number": 17
@@ -259,14 +687,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 10.16\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'",
+ "input": [
+ "\n",
+ "count = 0\n",
+ "class Widget:\n",
+ " def __init__(self):\n",
+ " global count\n",
+ " count += 1\n",
+ " def numWidgets(self):\n",
+ " global count\n",
+ " return count\n",
+ " \n",
+ "w = Widget()\n",
+ "x = Widget()\n",
+ "print \"Now there are \" , w.numWidgets() , 'widgets'\n",
+ "if True:\n",
+ " w = Widget()\n",
+ " x = Widget()\n",
+ " y = Widget()\n",
+ " z = Widget()\n",
+ " print \"Now there are\" , w.numWidgets() , 'widgets' \n",
+ "print \"Now there are \" , w.numWidgets() , 'widgets'\n",
+ "y = Widget()\n",
+ "print \"Now there are \" , w.numWidgets() , 'widgets'"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n"
+ "text": [
+ "Now there are 2 widgets\n",
+ "Now there are 6 widgets\n",
+ "Now there are 6 widgets\n",
+ "Now there are 7 widgets\n"
+ ]
}
],
"prompt_number": 18
@@ -274,7 +730,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
index 3ddc5480..34fb61dc 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch11"
+ "name": "",
+ "signature": "sha256:bdd5f9e441ccb7e3a87f4d8c5492d0aa1d228caba299fc1b0c667abb12780a1e"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,7 +11,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.1\nAdding an Assignment Operator to the Ratio Class\nHere is a class interface for the Ratio class, showing the default constructor, the copy constructor,\nand the assignment operator:\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n def equals(self):\n return self # retuns calling object.",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " if d==None:\n",
+ " self.num = n.num\n",
+ " self.den = n.den\n",
+ " elif n==None:\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " else:\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " \n",
+ " def equals(self):\n",
+ " return self # retuns calling object."
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -19,7 +36,15 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.2 The Preferred Prototype for an Overloaded Assignment Operator\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n pass\n \n def equals(self):\n pass\n",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " pass\n",
+ " \n",
+ " def equals(self):\n",
+ " pass\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -28,7 +53,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.3 Implementation of the Assignment Operator for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n\nz = Ratio(22,7)\ny = z\nx = z\n\nx = Ratio(22,7)\ny = Ratio(x)\nz = x\nw = x",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " if d==None:\n",
+ " self.num = n.num\n",
+ " self.den = n.den\n",
+ " elif n==None:\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " else:\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " \n",
+ "\n",
+ "z = Ratio(22,7)\n",
+ "y = z\n",
+ "x = z\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(x)\n",
+ "z = x\n",
+ "w = x"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -37,7 +85,16 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.4\nmultiplication operator overload\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __mul__(self,y):\n pass\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def __mul__(self,y):\n",
+ " pass\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -46,14 +103,68 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.5 The Ratio Class with Assignment and Multiplication Operators\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\nx = Ratio(22,7)\ny = Ratio(-3,8)\nz = x\nz.print_()\nx = y*z\nx.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(-3,8)\n",
+ "z = x\n",
+ "z.print_()\n",
+ "x = y*z\n",
+ "x.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "22 / 7\n-33 / 28\n"
+ "text": [
+ "22 / 7\n",
+ "-33 / 28\n"
+ ]
}
],
"prompt_number": 5
@@ -61,7 +172,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.6 The Ratio Class with an Overloaded *= Operator\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def print_(self):\n print self.num , '/', self.den",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __imul__(self,y):\n",
+ " self.num = self.num * y.num\n",
+ " self.den = self.den * y.den\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -70,7 +204,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.7 Overloading the Equality Operator == in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __imul__(self,y):\n",
+ " self.num = self.num * y.num\n",
+ " self.den = self.den * y.den\n",
+ " def __eq__(self,y):\n",
+ " return (x.num * y.den == y.num * x.den)\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -79,20 +229,44 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.8 \nPython does not use << operator for printing. So here we are just declaring function name as print_.\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n\nx = Ratio(22,7)\ny = Ratio(-3,8)\nx.print_() , y.print_()\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def __imul__(self,y):\n",
+ " self.num = self.num * y.num\n",
+ " self.den = self.den * y.den\n",
+ " def __eq__(self,y):\n",
+ " return (x.num * y.den == y.num * x.den)\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ "\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(-3,8)\n",
+ "x.print_() , y.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "22 / 7\n-3 / 8\n"
+ "text": [
+ "22 / 7\n",
+ "-3 / 8\n"
+ ]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
- "text": "(None, None)"
+ "text": [
+ "(None, None)"
+ ]
}
],
"prompt_number": 8
@@ -100,7 +274,62 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.9 \nPython does not have >> for input. so we will use input function.\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\n\nx = Ratio()\ny = Ratio()\nx.input()\ny.input()\nx.print_()\ny.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def input(self):\n",
+ " self.num = int(raw_input('Numerator : '))\n",
+ " self.den = int(raw_input('Denominator : '))\n",
+ " self.reduce()\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "\n",
+ "x = Ratio()\n",
+ "y = Ratio()\n",
+ "x.input()\n",
+ "y.input()\n",
+ "x.print_()\n",
+ "y.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -108,30 +337,41 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Numerator : -10\n"
+ "text": [
+ "Numerator : -10\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Denominator : -24\n"
+ "text": [
+ "Denominator : -24\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Numerator : 36\n"
+ "text": [
+ "Numerator : 36\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Denominator : -20\n"
+ "text": [
+ "Denominator : -20\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "5 / 12\n-9 / 5\n"
+ "text": [
+ "5 / 12\n",
+ "-9 / 5\n"
+ ]
}
],
"prompt_number": 1
@@ -139,14 +379,78 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.10\nAdding a Conversion Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def __float__(self):\n return float(self.num)/self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 15.py\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(-5,8)\nprint \"x = \" , \nx.print_() \nprint \", float(x) = \" , float(x) \nP = Ratio(22,7)\nPI = float(P)\nprint \"P = \" ,\nP.print_() \nprint \", PI = \" , PI\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def input(self):\n",
+ " self.num = int(raw_input('Numerator : '))\n",
+ " self.den = int(raw_input('Denominator : '))\n",
+ " self.reduce()\n",
+ " def __float__(self):\n",
+ " return float(self.num)/self.den\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 15.py\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(-5,8)\n",
+ "print \"x = \" , \n",
+ "x.print_() \n",
+ "print \", float(x) = \" , float(x) \n",
+ "P = Ratio(22,7)\n",
+ "PI = float(P)\n",
+ "print \"P = \" ,\n",
+ "P.print_() \n",
+ "print \", PI = \" , PI\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = -5 / 8\n, float(x) = -0.625\nP = 22 / 7\n, PI = 3.14285714286\n"
+ "text": [
+ "x = -5 / 8\n",
+ ", float(x) = -0.625\n",
+ "P = 22 / 7\n",
+ ", PI = 3.14285714286\n"
+ ]
}
],
"prompt_number": 3
@@ -154,14 +458,71 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.11 Adding a Pre-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nx += 1\ny = x\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def __iadd__(self,n):\n",
+ " self.num += self.den\n",
+ " return self\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "x += 1\n",
+ "y = x\n",
+ "print \"y = \" ,\n",
+ "y.print_()\n",
+ "print \", x = \",\n",
+ "x.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "y = 29 / 7\n, x = 29 / 7\n"
+ "text": [
+ "y = 29 / 7\n",
+ ", x = 29 / 7\n"
+ ]
}
],
"prompt_number": 5
@@ -169,14 +530,72 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.12 Adding a Post-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\ny = Ratio(x.num,x.den)\nx += 1\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def __iadd__(self,n):\n",
+ " self.num += self.den\n",
+ " return self\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(x.num,x.den)\n",
+ "x += 1\n",
+ "print \"y = \" ,\n",
+ "y.print_()\n",
+ "print \", x = \",\n",
+ "x.print_()\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "y = 22 / 7\n, x = 29 / 7\n"
+ "text": [
+ "y = 22 / 7\n",
+ ", x = 29 / 7\n"
+ ]
}
],
"prompt_number": 9
@@ -184,14 +603,70 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.13\nAdding a Subscript Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __getitem__(self,k):\n if k == 1:\n return self.num\n else:\n return self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nprint \"x = \" ,\nx.print_()\nprint \"x[1] = \" , x[1] , \", x[2] = \" , x[2]\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def __getitem__(self,k):\n",
+ " if k == 1:\n",
+ " return self.num\n",
+ " else:\n",
+ " return self.den\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "print \"x = \" ,\n",
+ "x.print_()\n",
+ "print \"x[1] = \" , x[1] , \", x[2] = \" , x[2]\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 22 / 7\nx[1] = 22 , x[2] = 7\n"
+ "text": [
+ "x = 22 / 7\n",
+ "x[1] = 22 , x[2] = 7\n"
+ ]
}
],
"prompt_number": 7
@@ -199,7 +674,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb
index 304a3ccb..2fc62abb 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch12"
+ "name": "",
+ "signature": "sha256:f377691760a60edd5db7fbe78ee867c7be9afd9802ecd81bd9e073874d13c99c"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,35 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.1 A Person Class\nHere is a simple definition for a class to represent people.\n'''\nclass Person:\n def __init__(self,n=\"\",nat=\"U.S.A.\",s=1):\n self.name = n\n self.nationality = nat\n self.sex = s\n\n def printName(self):\n print self.name,\n \n def printNationality(self):\n print self.nationality,\n\ncreator = Person(\"Bjarne Stroustrup\", \"Denmark\")\nprint \"The creator of C++ was \" ,\ncreator.printName() \nprint \", who was born in \" ,\ncreator.printNationality() \n",
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",nat=\"U.S.A.\",s=1):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ "\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " \n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ "\n",
+ "creator = Person(\"Bjarne Stroustrup\", \"Denmark\")\n",
+ "print \"The creator of C++ was \" ,\n",
+ "creator.printName() \n",
+ "print \", who was born in \" ,\n",
+ "creator.printNationality() \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The creator of C++ was Bjarne Stroustrup , who was born in Denmark\n"
+ "text": [
+ "The creator of C++ was Bjarne Stroustrup , who was born in Denmark\n"
+ ]
}
],
"prompt_number": 1
@@ -25,37 +47,86 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.2 A Date Class\n'''\n\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\npeace = Date(11,11,1918)\nprint \"World War I ended on \" ,\npeace.print_()\npeace.setDate(8,14,1945)\nprint \"World War II ended on \" ,\npeace.print_()\nprint \"Enter month, day, and year: \"\ndate = Date()\ndate.input()\nprint \"The date is \" , \ndate.print_()\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "peace = Date(11,11,1918)\n",
+ "print \"World War I ended on \" ,\n",
+ "peace.print_()\n",
+ "peace.setDate(8,14,1945)\n",
+ "print \"World War II ended on \" ,\n",
+ "peace.print_()\n",
+ "print \"Enter month, day, and year: \"\n",
+ "date = Date()\n",
+ "date.input()\n",
+ "print \"The date is \" , \n",
+ "date.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "World War I ended on November 11 , 1918\nWorld War II ended on August 14 , 1945\nEnter month, day, and year: \n"
+ "text": [
+ "World War I ended on November 11 , 1918\n",
+ "World War II ended on August 14 , 1945\n",
+ "Enter month, day, and year: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4\n"
+ "text": [
+ "4\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1976\n"
+ "text": [
+ "1976\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The date is July 4 , 1976\n"
+ "text": [
+ "The date is July 4 , 1976\n"
+ ]
}
],
"prompt_number": 2
@@ -63,14 +134,73 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.3 Composing the Date Class with the Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nauthor = Person(\"Thomas Jefferson\", 1)\nauthor.setDOB(4,13,1743)\nauthor.setDOD(7,4,1826)\nprint \"The author of the Declaration of Independence was \",\nauthor.printName()\nprint \".\\nHe was born on \",\nauthor.printDOB()\nprint \" and died on \",\nauthor.printDOD()\n\n",
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "author = Person(\"Thomas Jefferson\", 1)\n",
+ "author.setDOB(4,13,1743)\n",
+ "author.setDOD(7,4,1826)\n",
+ "print \"The author of the Declaration of Independence was \",\n",
+ "author.printName()\n",
+ "print \".\\nHe was born on \",\n",
+ "author.printDOB()\n",
+ "print \" and died on \",\n",
+ "author.printDOD()\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The author of the Declaration of Independence was Thomas Jefferson .\nHe was born on April 13 , 1743\n and died on July 4 , 1826\n"
+ "text": [
+ "The author of the Declaration of Independence was Thomas Jefferson .\n",
+ "He was born on April 13 , 1743\n",
+ " and died on July 4 , 1826\n"
+ ]
}
],
"prompt_number": 3
@@ -78,14 +208,85 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.4 Deriving a Student Class from the Person Class\nStudents are people. So it is natural to use the Person class to derive a Student class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.printName()\nprint \"\\n\\t Born: \" ,\nx.printDOB()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n",
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,n,s=0,i=\"\"):\n",
+ " Person.__init__(self,n,s)\n",
+ " self.id = i\n",
+ " self.credits = 0\n",
+ " self.gpa = 0\n",
+ " self.dom = Date()\n",
+ "\n",
+ " def setDOM(self,m,d,y):\n",
+ " self.dom.setDate(m, d, y)\n",
+ " def printDOM(self):\n",
+ " self.dom.print_()\n",
+ "\n",
+ "x = Student(\"Ann Jones\", 0, \"219360061\")\n",
+ "x.setDOB(5, 13, 1977)\n",
+ "x.setDOM(8, 29, 1995)\n",
+ "x.printName()\n",
+ "print \"\\n\\t Born: \" ,\n",
+ "x.printDOB()\n",
+ "print \"\\n\\tMatriculated: \",\n",
+ "x.printDOM()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\tMatriculated: August 29 , 1995\n"
+ "text": [
+ "Ann Jones \n",
+ "\t Born: May 13 , 1977\n",
+ "\n",
+ "\tMatriculated: August 29 , 1995\n"
+ ]
}
],
"prompt_number": 4
@@ -93,14 +294,95 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.5 The Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n def printSex(self):\n if self.sex == 1:\n print \"male\"\n else:\n print 'female'\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.setDOD(7,4,1826)\nx.printName()\nprint \"\\n\\t Born: \" , \nx.printDOB()\nprint \"\\n\\t Sex: \" ,\nx.printSex()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n",
+ "input": [
+ "\n",
+ "class Date:\n",
+ " def __init__(self,m=0,d=0,y=0):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " \n",
+ " def setDate(self,m,d,y):\n",
+ " self.month = m\n",
+ " self.day = d\n",
+ " self.year = y\n",
+ " # Python doesn't have >> operator for input so we are just using input function\n",
+ " def input(self):\n",
+ " self.month = int(raw_input()) \n",
+ " self.day = int(raw_input())\n",
+ " self.year = int(raw_input()) \n",
+ " \n",
+ " # Python doesn't have << operator for output so we are just using print function\n",
+ " def print_(self):\n",
+ " monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n",
+ " \"July\", \"August\",\"September\", \"October\", \"November\",\\\n",
+ " \"December\"]\n",
+ " print monthName[self.month] , self.day , \",\" , self.year\n",
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,n,s=0,i=\"\"):\n",
+ " Person.__init__(self,n,s)\n",
+ " self.id = i\n",
+ " self.credits = 0\n",
+ " self.gpa = 0\n",
+ " self.dom = Date()\n",
+ "\n",
+ " def setDOM(self,m,d,y):\n",
+ " self.dom.setDate(m, d, y)\n",
+ " def printDOM(self):\n",
+ " self.dom.print_()\n",
+ " def printSex(self):\n",
+ " if self.sex == 1:\n",
+ " print \"male\"\n",
+ " else:\n",
+ " print 'female'\n",
+ "\n",
+ "x = Student(\"Ann Jones\", 0, \"219360061\")\n",
+ "x.setDOB(5, 13, 1977)\n",
+ "x.setDOM(8, 29, 1995)\n",
+ "x.setDOD(7,4,1826)\n",
+ "x.printName()\n",
+ "print \"\\n\\t Born: \" , \n",
+ "x.printDOB()\n",
+ "print \"\\n\\t Sex: \" ,\n",
+ "x.printSex()\n",
+ "print \"\\n\\tMatriculated: \",\n",
+ "x.printDOM()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\t Sex: female\n\n\tMatriculated: August 29 , 1995\n"
+ "text": [
+ "Ann Jones \n",
+ "\t Born: May 13 , 1977\n",
+ "\n",
+ "\t Sex: female\n",
+ "\n",
+ "\tMatriculated: August 29 , 1995\n"
+ ]
}
],
"prompt_number": 5
@@ -108,14 +390,51 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.6 Dominating a Data Member and Overriding a Member Function\nHere are two classes, X and Y, with Y inheriting from X.\n'''\nclass X:\n def __init__(self):\n self.a = 0\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def __init__(self):\n self.a = 0\n def f(self):\n print \"Y::f() executing\"\nx = X()\nx.a = 22\nx.f()\nprint \"x.a = \" , x.a\ny = Y()\ny.a = 44\n# assigns 44 to the a defined in Y\ny._X__a = 66\n# assigns 66 to the a defined in X\ny.f()\n# invokes the f() defined in Y\nX.f(x)\n# invokes the f() defined in X\nprint \"y.a = \" , y.a \nprint \"y._X__a = \" , y._X__a \nz = y\nprint \"z.a = \" , z._X__a \n",
+ "input": [
+ "\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " self.a = 0\n",
+ " def f(self):\n",
+ " print \"X::f() executing\"\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " self.a = 0\n",
+ " def f(self):\n",
+ " print \"Y::f() executing\"\n",
+ "x = X()\n",
+ "x.a = 22\n",
+ "x.f()\n",
+ "print \"x.a = \" , x.a\n",
+ "y = Y()\n",
+ "y.a = 44\n",
+ "# assigns 44 to the a defined in Y\n",
+ "y._X__a = 66\n",
+ "# assigns 66 to the a defined in X\n",
+ "y.f()\n",
+ "# invokes the f() defined in Y\n",
+ "X.f(x)\n",
+ "# invokes the f() defined in X\n",
+ "print \"y.a = \" , y.a \n",
+ "print \"y._X__a = \" , y._X__a \n",
+ "z = y\n",
+ "print \"z.a = \" , z._X__a \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "X::f() executing\nx.a = 22\nY::f() executing\nX::f() executing\ny.a = 44\ny._X__a = 66\nz.a = 66\n"
+ "text": [
+ "X::f() executing\n",
+ "x.a = 22\n",
+ "Y::f() executing\n",
+ "X::f() executing\n",
+ "y.a = 44\n",
+ "y._X__a = 66\n",
+ "z.a = 66\n"
+ ]
}
],
"prompt_number": 6
@@ -123,14 +442,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.7 Parent Constructors and Destructors\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass X:\n def __init__(self):\n print \"X::X() constructor executing \"\n def __del__(self):\n print \"X::X() destructor executing \"\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n print \"Y::Y() constructor executing \"\n def __del__(self):\n print \"Y::Y() destructor executing \"\n\nclass Z(Y):\n def __init__(self,i):\n Y.__init__(self)\n print \"Z::Z(\" , i , \") constructor executing \"\n def __del__(self):\n print \"Z::Z() destructor executing \"\n \n\nZ = Z(44)\n",
+ "input": [
+ "\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " print \"X::X() constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"X::X() destructor executing \"\n",
+ "\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " X.__init__(self)\n",
+ " print \"Y::Y() constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"Y::Y() destructor executing \"\n",
+ "\n",
+ "class Z(Y):\n",
+ " def __init__(self,i):\n",
+ " Y.__init__(self)\n",
+ " print \"Z::Z(\" , i , \") constructor executing \"\n",
+ " def __del__(self):\n",
+ " print \"Z::Z() destructor executing \"\n",
+ " \n",
+ "\n",
+ "Z = Z(44)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "X::X() constructor executing \nY::Y() constructor executing \nZ::Z( 44 ) constructor executing \n"
+ "text": [
+ "X::X() constructor executing \n",
+ "Y::Y() constructor executing \n",
+ "Z::Z( 44 ) constructor executing \n"
+ ]
}
],
"prompt_number": 7
@@ -138,7 +485,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.8 Parent Constructors and Destructors\nHere is a demo program that uses a base class Person and a derived class Student:\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass Person:\n def __init__(self,s):\n self.name = s\n def __del__(self):\n pass\n\nclass Student(Person):\n def __init__(self,s,m):\n Person.__init__(self,s)\n self.major = m\n def __del__(self):\n pass\nx = Person(\"Bob\")\ny = Student(\"Sarah\", \"Biology\")\n",
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,s):\n",
+ " self.name = s\n",
+ " def __del__(self):\n",
+ " pass\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,s,m):\n",
+ " Person.__init__(self,s)\n",
+ " self.major = m\n",
+ " def __del__(self):\n",
+ " pass\n",
+ "x = Person(\"Bob\")\n",
+ "y = Student(\"Sarah\", \"Biology\")\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -147,7 +510,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.9\n'''\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n def setHSgraduate(self,g):\n self.hs = g\n def isHSgraduate(self):\n return hs\n",
+ "input": [
+ "class Person:\n",
+ " def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n",
+ " self.name = n\n",
+ " self.nationality = nat\n",
+ " self.sex = s\n",
+ " self.dob = Date()\n",
+ " self.dod = Date()\n",
+ " def setDOB(self,m,d,y):\n",
+ " self.dob.setDate(m,d,y)\n",
+ " def setDOD(self,m,d,y):\n",
+ " self.dod.setDate(m,d,y)\n",
+ " def printName(self):\n",
+ " print self.name,\n",
+ " def printNationality(self):\n",
+ " print self.nationality,\n",
+ " def printDOB(self):\n",
+ " self.dob.print_()\n",
+ " def printDOD(self):\n",
+ " self.dod.print_()\n",
+ " def setHSgraduate(self,g):\n",
+ " self.hs = g\n",
+ " def isHSgraduate(self):\n",
+ " return hs\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -156,14 +543,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.10 Using virtual Functions\nThis demo program declares p to be a pointer to objects of the base class X. First it assigns p to\npoint to an instance x of class X. Then it assigns p to point to an instance y of the derived class Y.\nNote : By default all methods in python are virtual. so output would be differ than c.\n'''\nclass X:\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def f(self):\n print \"Y::f() executing\"\nx = X()\ny = Y()\np = x\np.f()\np = y\np.f()\n",
+ "input": [
+ "\n",
+ "class X:\n",
+ " def f(self):\n",
+ " print \"X::f() executing\"\n",
+ "class Y(X):\n",
+ " def f(self):\n",
+ " print \"Y::f() executing\"\n",
+ "x = X()\n",
+ "y = Y()\n",
+ "p = x\n",
+ "p.f()\n",
+ "p = y\n",
+ "p.f()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "X::f() executing\nY::f() executing\n"
+ "text": [
+ "X::f() executing\n",
+ "Y::f() executing\n"
+ ]
}
],
"prompt_number": 10
@@ -171,14 +575,49 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.11 Polymorphism through virtual Functions\nHere is a Person class with a Student subclass and a Professor subclass:\n'''\nclass Person:\n def __init__(self,n):\n self.name = n\n def print_(self):\n print 'My name is' , self.name\n\nclass Student(Person):\n def __init__(self,s,g):\n Person.__init__(self,s)\n self.gpa = g\n def print_(self):\n print 'My name is ',self.name, ' and my G.P.A. is', self.gpa\n\nclass Professor(Person):\n def __init__(self,s,n):\n Person.__init__(self,s)\n self.publs = n\n def print_(self):\n print 'My name is ', self.name,' and i have ' , self.publs,' publications.'\n\nx = Person(\"Bob\")\np = x\np.print_()\ny = Student(\"Tom\", 3.47)\np = y\np.print_()\nz = Professor(\"Ann\", 7)\np = z\np.print_()",
+ "input": [
+ "\n",
+ "class Person:\n",
+ " def __init__(self,n):\n",
+ " self.name = n\n",
+ " def print_(self):\n",
+ " print 'My name is' , self.name\n",
+ "\n",
+ "class Student(Person):\n",
+ " def __init__(self,s,g):\n",
+ " Person.__init__(self,s)\n",
+ " self.gpa = g\n",
+ " def print_(self):\n",
+ " print 'My name is ',self.name, ' and my G.P.A. is', self.gpa\n",
+ "\n",
+ "class Professor(Person):\n",
+ " def __init__(self,s,n):\n",
+ " Person.__init__(self,s)\n",
+ " self.publs = n\n",
+ " def print_(self):\n",
+ " print 'My name is ', self.name,' and i have ' , self.publs,' publications.'\n",
+ "\n",
+ "x = Person(\"Bob\")\n",
+ "p = x\n",
+ "p.print_()\n",
+ "y = Student(\"Tom\", 3.47)\n",
+ "p = y\n",
+ "p.print_()\n",
+ "z = Professor(\"Ann\", 7)\n",
+ "p = z\n",
+ "p.print_()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "My name is Bob\nMy name is Tom and my G.P.A. is 3.47\nMy name is Ann and i have 7 publications.\n"
+ "text": [
+ "My name is Bob\n",
+ "My name is Tom and my G.P.A. is 3.47\n",
+ "My name is Ann and i have 7 publications.\n"
+ ]
}
],
"prompt_number": 11
@@ -186,14 +625,44 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.12 Memory Leaks\nThis program is similar to Example 12.6:\n'''\nclass X:\n def __init__(self):\n self.p = [0,0]\n print 'X().',\n def __del__(self):\n print '~X().'\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n self.q = []\n for i in range(1023):\n self.q.append(0)\n print 'Y() : Y::q = ', hex(id(self.q)) ,'.',\n def __del__(self):\n print '~Y().'\n\nfor i in range(8):\n r = Y()",
+ "input": [
+ "\n",
+ "class X:\n",
+ " def __init__(self):\n",
+ " self.p = [0,0]\n",
+ " print 'X().',\n",
+ " def __del__(self):\n",
+ " print '~X().'\n",
+ "\n",
+ "class Y(X):\n",
+ " def __init__(self):\n",
+ " X.__init__(self)\n",
+ " self.q = []\n",
+ " for i in range(1023):\n",
+ " self.q.append(0)\n",
+ " print 'Y() : Y::q = ', hex(id(self.q)) ,'.',\n",
+ " def __del__(self):\n",
+ " print '~Y().'\n",
+ "\n",
+ "for i in range(8):\n",
+ " r = Y()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "X(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\n"
+ "text": [
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n",
+ "X(). Y() : Y::q = 0x90d91cc . ~Y().\n",
+ "X(). Y() : Y::q = 0x90c944c . ~Y().\n"
+ ]
}
],
"prompt_number": 13
@@ -201,14 +670,74 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 12.13 A Hierarchy of Media Classes\nHere is a hierarchy of classes to represent various media objects:\nMedia Audio Book Periodical CD Tape Record Magazine Newspaper Journal Newsletter\nThe primary ABC is the Media class:\n'''\nclass Media:\n def __init__(self):\n self.title = ''\n def print_(self):\n pass\n def id(self):\n pass\n\nclass Book(Media):\n def __init__(self,a='',t=\"\",p=\"\",i=\"\"):\n self.author = a\n self.publisher = p\n self.isbn = i\n self.title = t\n def print_(self):\n print self.title , \" by \" , self.author\n def id(self):\n return self.isbn\n\nclass CD(Media):\n def __init__(self,t=\"\",c=\"\",m=\"\",n=\"\"):\n self.composer = c\n self.make = m\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \", \" , self.composer\n def id(self):\n s = str(self.make) + ' ' + str(self.number)\n return s\n\nclass Magazine(Media):\n def __init__(self,t=\"\",i=\"\",v=0, n=0):\n self.issn = i\n self.volume = v\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \" Magazine, Vol. \", self.volume , \", No.\" , self.number\n def id(self):\n return self.issn\n\nbook = Book(\"Bjarne Stroustrup\", \"The C++ Programming Language\",\"Addison-Wesley\", \"0-201-53992-6\")\nmagazine = Magazine(\"TIME\", \"0040-781X\", 145, 23)\ncd = CD(\"BACH CANTATAS\", \"Johann Sebastian Bach\",\"ARCHIV\", \"D120541\")\nbook.print_()\nprint \"\\tid: \" , book.id() \nmagazine.print_()\nprint \"\\tid: \" , magazine.id() \ncd.print_()\nprint \"\\tid: \" , cd.id()",
+ "input": [
+ "\n",
+ "class Media:\n",
+ " def __init__(self):\n",
+ " self.title = ''\n",
+ " def print_(self):\n",
+ " pass\n",
+ " def id(self):\n",
+ " pass\n",
+ "\n",
+ "class Book(Media):\n",
+ " def __init__(self,a='',t=\"\",p=\"\",i=\"\"):\n",
+ " self.author = a\n",
+ " self.publisher = p\n",
+ " self.isbn = i\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \" by \" , self.author\n",
+ " def id(self):\n",
+ " return self.isbn\n",
+ "\n",
+ "class CD(Media):\n",
+ " def __init__(self,t=\"\",c=\"\",m=\"\",n=\"\"):\n",
+ " self.composer = c\n",
+ " self.make = m\n",
+ " self.number = n\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \", \" , self.composer\n",
+ " def id(self):\n",
+ " s = str(self.make) + ' ' + str(self.number)\n",
+ " return s\n",
+ "\n",
+ "class Magazine(Media):\n",
+ " def __init__(self,t=\"\",i=\"\",v=0, n=0):\n",
+ " self.issn = i\n",
+ " self.volume = v\n",
+ " self.number = n\n",
+ " self.title = t\n",
+ " def print_(self):\n",
+ " print self.title , \" Magazine, Vol. \", self.volume , \", No.\" , self.number\n",
+ " def id(self):\n",
+ " return self.issn\n",
+ "\n",
+ "book = Book(\"Bjarne Stroustrup\", \"The C++ Programming Language\",\"Addison-Wesley\", \"0-201-53992-6\")\n",
+ "magazine = Magazine(\"TIME\", \"0040-781X\", 145, 23)\n",
+ "cd = CD(\"BACH CANTATAS\", \"Johann Sebastian Bach\",\"ARCHIV\", \"D120541\")\n",
+ "book.print_()\n",
+ "print \"\\tid: \" , book.id() \n",
+ "magazine.print_()\n",
+ "print \"\\tid: \" , magazine.id() \n",
+ "cd.print_()\n",
+ "print \"\\tid: \" , cd.id()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The C++ Programming Language by Bjarne Stroustrup\n\tid: 0-201-53992-6\nTIME Magazine, Vol. 145 , No. 23\n\tid: 0040-781X\nBACH CANTATAS , Johann Sebastian Bach\n\tid: ARCHIV D120541\n"
+ "text": [
+ "The C++ Programming Language by Bjarne Stroustrup\n",
+ "\tid: 0-201-53992-6\n",
+ "TIME Magazine, Vol. 145 , No. 23\n",
+ "\tid: 0040-781X\n",
+ "BACH CANTATAS , Johann Sebastian Bach\n",
+ "\tid: ARCHIV D120541\n"
+ ]
}
],
"prompt_number": 14
@@ -216,7 +745,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb
index 827142fe..5261a124 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch13"
+ "name": "",
+ "signature": "sha256:ca23888d127466a3b56ff597f51112b17faee9e92f25f41708b3d49e2ac19a08"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,7 +11,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.1 The swap Function Template\n'''\ndef swap(x,y):\n x[0],y[0] = y[0],x[0]\n\nm = [22]\nn = [66]\nswap(m, n)\ns1 = [\"John Adams\"]\ns2 = [\"James Madison\"]\nswap(s1, s2)\nx = [22/7]\ny = [-3]\nswap(x, y)\n",
+ "input": [
+ "\n",
+ "def swap(x,y):\n",
+ " x[0],y[0] = y[0],x[0]\n",
+ "\n",
+ "m = [22]\n",
+ "n = [66]\n",
+ "swap(m, n)\n",
+ "s1 = [\"John Adams\"]\n",
+ "s2 = [\"James Madison\"]\n",
+ "swap(s1, s2)\n",
+ "x = [22/7]\n",
+ "y = [-3]\n",
+ "swap(x, y)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -19,14 +34,41 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.2 The Bubble Sort Template\n'''\n\ndef sort(v,n):\n for i in range(1,n):\n for j in range(n-i):\n if v[j] > v[j+1]:\n v[j],v[j+1] = v[j+1],v[j]\n\ndef print_( v,n):\n for i in range(n):\n print v[i],\n print \"\"\n \na = [55, 33, 88, 11, 44, 99, 77, 22, 66]\nprint_(a,9);\nsort(a,9)\nprint_(a,9)\ns = [\"Tom\", \"Hal\", \"Dan\", \"Bob\", \"Sue\", \"Ann\", \"Gus\"]\nprint_(s,7)\nsort(s,7)\nprint_(s,7)\n",
+ "input": [
+ "\n",
+ "\n",
+ "def sort(v,n):\n",
+ " for i in range(1,n):\n",
+ " for j in range(n-i):\n",
+ " if v[j] > v[j+1]:\n",
+ " v[j],v[j+1] = v[j+1],v[j]\n",
+ "\n",
+ "def print_( v,n):\n",
+ " for i in range(n):\n",
+ " print v[i],\n",
+ " print \"\"\n",
+ " \n",
+ "a = [55, 33, 88, 11, 44, 99, 77, 22, 66]\n",
+ "print_(a,9);\n",
+ "sort(a,9)\n",
+ "print_(a,9)\n",
+ "s = [\"Tom\", \"Hal\", \"Dan\", \"Bob\", \"Sue\", \"Ann\", \"Gus\"]\n",
+ "print_(s,7)\n",
+ "sort(s,7)\n",
+ "print_(s,7)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "55 33 88 11 44 99 77 22 66 \n11 22 33 44 55 66 77 88 99 \nTom Hal Dan Bob Sue Ann Gus \nAnn Bob Dan Gus Hal Sue Tom \n"
+ "text": [
+ "55 33 88 11 44 99 77 22 66 \n",
+ "11 22 33 44 55 66 77 88 99 \n",
+ "Tom Hal Dan Bob Sue Ann Gus \n",
+ "Ann Bob Dan Gus Hal Sue Tom \n"
+ ]
}
],
"prompt_number": 2
@@ -34,14 +76,51 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.3 A Stack Class Template\n'''\nclass Stack:\n def __init__(self,s=100):\n self.size = s\n self.top = -1\n self.data = []\n def push(self,x):\n self.data.append( x)\n self.top += 1\n def pop(self):\n d = self.data[self.top]\n self.data.pop(self.top)\n self.top -= 1\n return d \n def isEmpty(self):\n return self.top == -1\n def isFull(self):\n return self.top==self.size-1\n \nintStack1 = Stack(5)\nintStack2 = Stack(10)\ncharStack = Stack(8)\nintStack1.push(77)\ncharStack.push('A')\nintStack2.push(22)\ncharStack.push('E')\ncharStack.push('K')\nintStack2.push(44)\nprint intStack2.pop() \nprint intStack2.pop() \nif (intStack2.isEmpty()):\n print \"intStack2 is empty.\"\n",
+ "input": [
+ "\n",
+ "class Stack:\n",
+ " def __init__(self,s=100):\n",
+ " self.size = s\n",
+ " self.top = -1\n",
+ " self.data = []\n",
+ " def push(self,x):\n",
+ " self.data.append( x)\n",
+ " self.top += 1\n",
+ " def pop(self):\n",
+ " d = self.data[self.top]\n",
+ " self.data.pop(self.top)\n",
+ " self.top -= 1\n",
+ " return d \n",
+ " def isEmpty(self):\n",
+ " return self.top == -1\n",
+ " def isFull(self):\n",
+ " return self.top==self.size-1\n",
+ " \n",
+ "intStack1 = Stack(5)\n",
+ "intStack2 = Stack(10)\n",
+ "charStack = Stack(8)\n",
+ "intStack1.push(77)\n",
+ "charStack.push('A')\n",
+ "intStack2.push(22)\n",
+ "charStack.push('E')\n",
+ "charStack.push('K')\n",
+ "intStack2.push(44)\n",
+ "print intStack2.pop() \n",
+ "print intStack2.pop() \n",
+ "if (intStack2.isEmpty()):\n",
+ " print \"intStack2 is empty.\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "44\n22\nintStack2 is empty.\n"
+ "text": [
+ "44\n",
+ "22\n",
+ "intStack2 is empty.\n"
+ ]
}
],
"prompt_number": 3
@@ -49,14 +128,33 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.4 A Vector Class Template\n= operator does work in python by defaultly.\n'''\nclass Vector:\n def __init__(self,n=None):\n if type(n) == int :\n self.size = n\n self.data = []\n else:\n self.size = 8\n self.data = []\n for i in range(self.size):\n self.data.append(0)\nv = Vector()\nv.data[5] = 127\nw = v\nx = Vector(3)\nprint w.size\n",
+ "input": [
+ "\n",
+ "class Vector:\n",
+ " def __init__(self,n=None):\n",
+ " if type(n) == int :\n",
+ " self.size = n\n",
+ " self.data = []\n",
+ " else:\n",
+ " self.size = 8\n",
+ " self.data = []\n",
+ " for i in range(self.size):\n",
+ " self.data.append(0)\n",
+ "v = Vector()\n",
+ "v.data[5] = 127\n",
+ "w = v\n",
+ "x = Vector(3)\n",
+ "print w.size\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
}
],
"prompt_number": 4
@@ -64,14 +162,56 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.5 A Subclass Template for Vectors\n'''\nclass Vector:\n def __init__(self,n=None):\n if type(n) == int :\n self.size = n\n self.data = []\n else:\n self.size = 8\n self.data = []\n for i in range(self.size):\n self.data.append(0)\n\nclass Array(Vector):\n def __init__(self,i,j):\n Vector.__init__(self,j-i+1)\n self.i0= i\n def __setitem__(self,k,v):\n self.data[k-self.i0] = v\n def __getitem__(self,i):\n return self.data[self.i0-i]\n def firstSubscript(self):\n return self.i0\n def lastSubscript(self):\n return self.i0+self.size-1\n\nx = Array(1,3)\nx.data[0] = 3.14159\nx.data[1] = 0.08516\nx.data[2] = 5041.92\nprint \"x.size() = \" , x.size \nprint \"x.firstSubscript() = \" , x.firstSubscript() \nprint \"x.lastSubscript() = \" , x.lastSubscript()\nfor i in range(0,3):\n print \"x[\" , i + 1 , \"] = \" , x.data[i]\n",
+ "input": [
+ "\n",
+ "class Vector:\n",
+ " def __init__(self,n=None):\n",
+ " if type(n) == int :\n",
+ " self.size = n\n",
+ " self.data = []\n",
+ " else:\n",
+ " self.size = 8\n",
+ " self.data = []\n",
+ " for i in range(self.size):\n",
+ " self.data.append(0)\n",
+ "\n",
+ "class Array(Vector):\n",
+ " def __init__(self,i,j):\n",
+ " Vector.__init__(self,j-i+1)\n",
+ " self.i0= i\n",
+ " def __setitem__(self,k,v):\n",
+ " self.data[k-self.i0] = v\n",
+ " def __getitem__(self,i):\n",
+ " return self.data[self.i0-i]\n",
+ " def firstSubscript(self):\n",
+ " return self.i0\n",
+ " def lastSubscript(self):\n",
+ " return self.i0+self.size-1\n",
+ "\n",
+ "x = Array(1,3)\n",
+ "x.data[0] = 3.14159\n",
+ "x.data[1] = 0.08516\n",
+ "x.data[2] = 5041.92\n",
+ "print \"x.size() = \" , x.size \n",
+ "print \"x.firstSubscript() = \" , x.firstSubscript() \n",
+ "print \"x.lastSubscript() = \" , x.lastSubscript()\n",
+ "for i in range(0,3):\n",
+ " print \"x[\" , i + 1 , \"] = \" , x.data[i]\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x.size() = 3\nx.firstSubscript() = 1\nx.lastSubscript() = 3\nx[ 1 ] = 3.14159\nx[ 2 ] = 0.08516\nx[ 3 ] = 5041.92\n"
+ "text": [
+ "x.size() = 3\n",
+ "x.firstSubscript() = 1\n",
+ "x.lastSubscript() = 3\n",
+ "x[ 1 ] = 3.14159\n",
+ "x[ 2 ] = 0.08516\n",
+ "x[ 3 ] = 5041.92\n"
+ ]
}
],
"prompt_number": 5
@@ -79,14 +219,45 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.6 A Matrix Class Template\n'''\n\nclass Matrix:\n def __init__(self,r=1,c=1):\n self.rows = r\n self.columns = c\n self.vector = []\n for i in range(r):\n a = []\n for j in range(c):\n a.append(0)\n self.vector.append(a)\n\na = Matrix(2,3)\na.vector[0][0] = 0.0\na.vector[0][1] = 0.1\na.vector[0][2] = 0.2\na.vector[1][0] = 1.0\na.vector[1][1] = 1.1\na.vector[1][2] = 1.2\n\nprint \"The matrix a has \" , a.rows , \" rows and \", a.columns , \" columns:\"\nfor i in range(2):\n for j in range(3):\n print a.vector[i][j] ,\n print \"\"\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Matrix:\n",
+ " def __init__(self,r=1,c=1):\n",
+ " self.rows = r\n",
+ " self.columns = c\n",
+ " self.vector = []\n",
+ " for i in range(r):\n",
+ " a = []\n",
+ " for j in range(c):\n",
+ " a.append(0)\n",
+ " self.vector.append(a)\n",
+ "\n",
+ "a = Matrix(2,3)\n",
+ "a.vector[0][0] = 0.0\n",
+ "a.vector[0][1] = 0.1\n",
+ "a.vector[0][2] = 0.2\n",
+ "a.vector[1][0] = 1.0\n",
+ "a.vector[1][1] = 1.1\n",
+ "a.vector[1][2] = 1.2\n",
+ "\n",
+ "print \"The matrix a has \" , a.rows , \" rows and \", a.columns , \" columns:\"\n",
+ "for i in range(2):\n",
+ " for j in range(3):\n",
+ " print a.vector[i][j] ,\n",
+ " print \"\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The matrix a has 2 rows and 3 columns:\n0.0 0.1 0.2 \n1.0 1.1 1.2 \n"
+ "text": [
+ "The matrix a has 2 rows and 3 columns:\n",
+ "0.0 0.1 0.2 \n",
+ "1.0 1.1 1.2 \n"
+ ]
}
],
"prompt_number": 6
@@ -94,14 +265,38 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.7 A List Class Template\nA list consists of a linked sequence of nodes. Each node contains one data item and a link to the next\nnode. So we begin by defining a ListNode class template:\n'''\n\nfriends = []\n\n\nfriends.insert(0,\"Bowen, Van\")\nfriends.insert(0,\"Dixon, Tom\")\nfriends.insert(0,\"Mason, Joe\")\nfriends.insert(0,\"White, Ann\")\n\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\nfriends.remove('White, Ann')\nprint \"Removed: \" , 'White, Ann'\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "friends = []\n",
+ "\n",
+ "\n",
+ "friends.insert(0,\"Bowen, Van\")\n",
+ "friends.insert(0,\"Dixon, Tom\")\n",
+ "friends.insert(0,\"Mason, Joe\")\n",
+ "friends.insert(0,\"White, Ann\")\n",
+ "\n",
+ "for i in range(len(friends)):\n",
+ " print friends[i], '->' ,\n",
+ "print '*'\n",
+ "friends.remove('White, Ann')\n",
+ "print \"Removed: \" , 'White, Ann'\n",
+ "for i in range(len(friends)):\n",
+ " print friends[i], '->' ,\n",
+ "print '*'\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "White, Ann -> Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\nRemoved: White, Ann\nMason, Joe -> Dixon, Tom -> Bowen, Van -> *\n"
+ "text": [
+ "White, Ann -> Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\n",
+ "Removed: White, Ann\n",
+ "Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\n"
+ ]
}
],
"prompt_number": 7
@@ -109,14 +304,44 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 13.8\n'''\nfriends = []\nfriends.append(\"Bowen, Van\")\nfriends.append(\"Dixon, Tom\")\nfriends.append(\"Mason, Joe\")\nfriends.append(\"White, Ann\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.remove(\"Mason, Joe\")\nfriends[1] = \"Davis, Jim\"\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.insert(2,\"Morse, Sam\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfor i in range(len(friends)):\n print \"[\" ,friends[i] , \"]\" , '->' ,\nprint '*'\n",
+ "input": [
+ "\n",
+ "friends = []\n",
+ "friends.append(\"Bowen, Van\")\n",
+ "friends.append(\"Dixon, Tom\")\n",
+ "friends.append(\"Mason, Joe\")\n",
+ "friends.append(\"White, Ann\")\n",
+ "for i in range(len(friends)):\n",
+ " print friends[i], '->' ,\n",
+ "print '*'\n",
+ "\n",
+ "friends.remove(\"Mason, Joe\")\n",
+ "friends[1] = \"Davis, Jim\"\n",
+ "for i in range(len(friends)):\n",
+ " print friends[i], '->' ,\n",
+ "print '*'\n",
+ "\n",
+ "friends.insert(2,\"Morse, Sam\")\n",
+ "for i in range(len(friends)):\n",
+ " print friends[i], '->' ,\n",
+ "print '*'\n",
+ "\n",
+ "for i in range(len(friends)):\n",
+ " print \"[\" ,friends[i] , \"]\" , '->' ,\n",
+ "print '*'\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Bowen, Van -> Dixon, Tom -> Mason, Joe -> White, Ann -> *\nBowen, Van -> Davis, Jim -> White, Ann -> *\nBowen, Van -> Davis, Jim -> Morse, Sam -> White, Ann -> *\n[ Bowen, Van ] -> [ Davis, Jim ] -> [ Morse, Sam ] -> [ White, Ann ] -> *\n"
+ "text": [
+ "Bowen, Van -> Dixon, Tom -> Mason, Joe -> White, Ann -> *\n",
+ "Bowen, Van -> Davis, Jim -> White, Ann -> *\n",
+ "Bowen, Van -> Davis, Jim -> Morse, Sam -> White, Ann -> *\n",
+ "[ Bowen, Van ] -> [ Davis, Jim ] -> [ Morse, Sam ] -> [ White, Ann ] -> *\n"
+ ]
}
],
"prompt_number": 8
@@ -124,7 +349,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb
index 1618010d..6d15626d 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch14"
+ "name": "",
+ "signature": "sha256:81aae5e06fff42b82bf693bbb18c1546fd839925af34574cc2f25fff08c2a482"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,43 @@
{
"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",
+ "\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 +55,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 +98,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 +142,44 @@
{
"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",
+ "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,53 @@
{
"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",
+ "\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 +241,59 @@
{
"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",
+ "\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 +301,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 +354,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 +415,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 +480,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 +526,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb
index 54f50fe3..869f8b29 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": ""
+ "name": "",
+ "signature": "sha256:63b02b86d37296507c195a23c5c84302645481c443c7f5e7bd4fa711e72942f7"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -27,10 +28,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate the rate of heat transfer per m 2 of surface area of the wall, which is 220 mm thick.\n",
"\n",
- "'''\n",
"\n",
"# Variables\n",
"t1 = 60.; \t\t\t#0C\n",
@@ -69,10 +67,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "(i) Determine the thickness of fire brick and insulation \n",
- "(ii) Calculate the heat loss \n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"t1 = 1325.; \t\t\t#0C\n",
@@ -120,10 +115,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "What thickness of loosely packed rock wool insulation should be added to\n",
- "reduce the heat loss or (gain) \n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"L_A = 0.1; \t\t\t#m\n",
@@ -164,10 +156,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''Find-\n",
- "(i) To how many millimetres of insulation brick is the air layer equivalent ?\n",
- "(ii) What is the temperature of the outer surface of the steel plate ?\n",
- "'''\n",
"\n",
"# Variables\n",
"L_A = 0.2; \t\t\t#m\n",
@@ -218,9 +206,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find the heat flow rate\n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"k_A = 150.; \t\t\t#W/m 0C\n",
@@ -276,11 +262,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "calculate-\n",
- "(i) The rate of heat loss per m 2 of the tank surface area ;\n",
- "(ii) The temperature of the outside surface of the tank.\n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"L = 0.012; \t\t\t#m\n",
@@ -327,11 +309,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate\n",
- "(i) The rate at which heat must be removed from the interior \n",
- "(ii) The temperature on the outer surface of the metal sheet.\n",
- "'''\n",
+ "\n",
"# Variables\n",
"L_A = 0.003; \t\t\t#m\n",
"L_B = 0.05; \t\t\t#m\n",
@@ -379,12 +357,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine :\n",
- "(i) The unknown thermal conductivity \u2018k\u2019 ;\n",
- "(ii) The overall heat transfer coefficient ;\n",
- "(iii) All surface temperatures.\n",
- "'''\n",
"\n",
"#Varaible Declaration\n",
"L_A = 0.25; \t\t#m\n",
@@ -456,9 +428,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "calculate the heat loss per metre of length\n",
- "'''\n",
+ "\n",
"\n",
"import math\n",
"\n",
@@ -501,9 +471,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find the rate of heat loss from 60 m length of pipe.\n",
- "'''\n",
+ "\n",
"\n",
"import math\n",
"\n",
@@ -550,11 +518,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "How thick should the asbestos be provided in order to limit the heat losses to 2.1 kW/m 2 ?\n",
- "'''\n",
"\n",
- "# Variables\n",
"r1 = 0.06; \t\t\t#m\n",
"r2 = 0.08; \t\t\t#m\n",
"k_A = 42.; \t\t\t#W/m 0C\n",
@@ -596,9 +560,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find the rate of heat leakage\n",
- "'''\n",
"\n",
"# Variables\n",
"r2 = 0.7; \t\t\t#m\n",
@@ -637,10 +598,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine the critical thickness of insulation \n",
"\n",
- "'''\n",
"\n",
"import math \n",
"\n",
@@ -686,9 +644,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "calculate the rate of heat transfer\n",
- "'''\n",
"\n",
"# Variables\n",
"A = 1*1.5; \t\t\t#m**2\n",
@@ -727,9 +682,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "find how much electric power must be supplied to the wire to maintain the wire surface at 120\u00b0C ?\n",
- "'''\n",
+ "\n",
"\n",
"import math\n",
"\n",
@@ -772,9 +725,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine the heat transfer co-efficient and the rate of heat transfer \n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -825,9 +776,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine the length of the tube required for developed flow.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -882,9 +831,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "calculate the area of the heat exchanger.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -934,12 +881,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find-\n",
- "(i) The rate of heat transfer,\n",
- "(ii) The mass flow rate of water, and\n",
- "(iii) The surface area of the heat exchanger.\n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"t_c1 = 25.; \t\t\t#0C\n",
@@ -992,9 +934,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "determine the number of tubes required. \n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -1053,13 +993,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "calculate-\n",
- "(i) Mass of cooling water circulated in kg/min,\n",
- "(ii) Condenser surface area,\n",
- "(iii) Number of tubes required per pass, and\n",
- "(iv) Tube length.\n",
- "'''\n",
+ "\n",
"\n",
"\n",
"# Variables\n",
@@ -1125,9 +1059,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine the water exit temperature\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -1182,12 +1114,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate-\n",
- "(i) The total rate of energy emission.\n",
- "(ii) The intensity of normal radiation, and\n",
- "(iii) The wavelength of maximum monochromatic emissive power.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -1233,11 +1160,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate-\n",
- "(i) The surface temperature of the sun, and\n",
- "(ii) The heat flux at surface of the sun.\n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"wavelength = 0.49; \t\t\t#\u03bcm\n",
@@ -1276,14 +1199,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate-\n",
- "(i) Monochromatic emissive power \n",
- "(ii) Wavelength at which the emission is maximum,\n",
- "(iii) Maximum emissive power,\n",
- "(iv) Total emissive power, and\n",
- "(v) Total emissive power of the furnance \n",
- "'''\n",
"\n",
"import math\n",
"\n",
@@ -1345,10 +1260,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "(i) When the body is assumed to be grey with \u03b5 = 0.42.\n",
- "(ii) When the body is not grey.\n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"T1 = 1273.; \t\t\t#K\n",
@@ -1395,9 +1307,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "find the time required for the heating operation.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -1450,9 +1360,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate the heat transfer rate \n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"r1 = 0.05; \t\t\t#m\n",
@@ -1496,9 +1404,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "calculate the steady state temperature of cylinder surface \n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"r1 = 0.05; \t\t\t#m\n",
@@ -1545,9 +1451,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate the rate of evaporation of liquid air.\n",
- "'''\n",
"\n",
"# Variables\n",
"r1 = 0.105; \t\t\t#m\n",
@@ -1592,9 +1495,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Estimate the rate of heat flow by radiation to the oxygen in the container.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -1639,9 +1540,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find the percentage reduction when a polished aluminium shield\n",
- "'''\n",
"\n",
"# Variables\n",
"e1 = 0.3;\n",
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb
index 32a42e59..8e3b0a97 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": ""
+ "name": "",
+ "signature": "sha256:9c7b64e157d08f3c7a366b1cf2d1f4de4a886f7dc2475d29ec96bb2b10e8dc31"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -27,9 +28,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find the velocity of the gas \n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -74,9 +73,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "determine the pressure and the temperature \n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"p1 = 35.; \t\t\t#kN/m**2\n",
@@ -122,11 +119,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find the sonic velocity of -\n",
- "(i) Crude oil \n",
- "(ii) Mercury \n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -170,9 +163,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Find the speed of the plane\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -214,7 +205,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "# Find the velocity of a bullet fired \n",
+ " \n",
"\n",
"# Variables\n",
"a = 40*math.pi/180; \t\t\t#Mach angle in radians\n",
@@ -254,9 +245,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "find the velocity of the projectile. \n",
- "'''\n",
"\n",
"import math \n",
"\n",
@@ -299,9 +287,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine the speed of the aircraft \n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -345,12 +331,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate-\n",
- "(i) Stagnation pressure,\n",
- "(ii) Stagnation temperature, and\n",
- "(iii) Stagnation density.\n",
- "'''\n",
"\n",
"import math \n",
"\n",
@@ -401,10 +381,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Compute stagnation properties and the local Mach number. \n",
- "What would be the compressibility correction factor for a pitot-static tube \n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -465,11 +442,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate stagnation pressure if -\n",
- "(i) Compressibility is neglected ;\n",
- "(ii) Compressibility is accounted for.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -524,9 +497,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Calculate the speed of the aircraft. \n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -570,9 +541,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "What is the true speed of the aircraft ?\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -619,9 +588,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine the reservoir pressure, temperature and the throat area.\n",
- "'''\n",
"\n",
"import math \n",
"\n",
@@ -670,9 +636,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "find the maximum flow rate of air.\n",
- "'''\n",
"\n",
"import math \n",
"\n",
@@ -717,9 +680,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "find the velocity of air flowing at the outlet of the nozzle.\n",
- "'''\n",
+ "\n",
"\n",
"# Variables\n",
"R = 287.; \t\t\t#J/kg K\n",
@@ -760,11 +721,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "find the mass rate of flow of air through the nozzle to the atmosphere when the pressure in the tank is-\n",
- "(i) 140 kN/m 2 (abs.),\n",
- "(ii) 300 kN/m 2\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -825,13 +782,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine-\n",
- "(i) Stagnation temperature and stagnation pressure,\n",
- "(ii) Sonic velocity and Mach number at this section,\n",
- "(iii) Velocity, Mach number and flow area at outlet section where pressure is 110 kN/m 2 ,\n",
- "(iv) Pressure, temperature, velocity and flow area at throat of the nozzle.\n",
- "'''\n",
"\n",
"import math \n",
"\n",
@@ -927,9 +877,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "determine the flow conditions before and after the shock wave.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
@@ -995,11 +943,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "'''\n",
- "Determine :\n",
- "(i) Pressure, temperature and Mach number downstream of the shock, and\n",
- "(ii) Strength of shock.\n",
- "'''\n",
+ "\n",
"\n",
"import math \n",
"\n",
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb
index 606044d0..7167565e 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "CH2"
+ "name": "",
+ "signature": "sha256:6dd70c1f71cf7fe5898008fd909312cd4643245d1663bc204223830088a91083"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.1 Boolean Variables\n'''\n\n# prints the value of a boolean variable:\nflag=False\nprint \"flag = %r\" % flag\nflag = True\nprint \"flag = %r\" % flag",
+ "input": [
+ "\n",
+ "\n",
+ "# prints the value of a boolean variable:\n",
+ "flag=False\n",
+ "print \"flag = %r\" % flag\n",
+ "flag = True\n",
+ "print \"flag = %r\" % flag"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "flag = False\nflag = True\n"
+ "text": [
+ "flag = False\n",
+ "flag = True\n"
+ ]
}
],
"prompt_number": 1
@@ -25,14 +37,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.2 Character Variables\n'''\n\n# prints the character and its internally stored\nc='A'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='t'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='\\t' # the tab character\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='!'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "input": [
+ "\n",
+ "\n",
+ "# prints the character and its internally stored\n",
+ "c='A'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='t'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='\\t' # the tab character\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='!'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "c = A, int(c) = 65\nc = t, int(c) = 116\nc = \t, int(c) = 9\nc = !, int(c) = 33\n"
+ "text": [
+ "c = A, int(c) = 65\n",
+ "c = t, int(c) = 116\n",
+ "c = \t, int(c) = 9\n",
+ "c = !, int(c) = 33\n"
+ ]
}
],
"prompt_number": 2
@@ -40,14 +69,26 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.3 Integer Type Ranges\nThis program prints the numeric ranges of the Python\n'''\nimport sys\n# defines the constants SHRT_MIN, etc.\nprint 'maximum limit int : ',\nprint sys.maxint\nprint 'float info'\nprint sys.float_info",
+ "input": [
+ "\n",
+ "import sys\n",
+ "# defines the constants SHRT_MIN, etc.\n",
+ "print 'maximum limit int : ',\n",
+ "print sys.maxint\n",
+ "print 'float info'\n",
+ "print sys.float_info"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "maximum limit int : 2147483647\nfloat info\nsys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n"
+ "text": [
+ "maximum limit int : 2147483647\n",
+ "float info\n",
+ "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n"
+ ]
}
],
"prompt_number": 3
@@ -55,14 +96,33 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.4 Integer Arithmetic\nThis example illustrates how the arithmetic operators work.\n'''\n\n# tests operators +, -, *, /, and %:\nm=54\nn=20\nprint \"m = %d and n = %d\" %(m,n)\nprint \"m+n = %d\" % (m+n) # 54+20 = 74\nprint \"m-n = %d\" % (m-n) # 54-20 = 34\nprint \"m*n = %d\" % (m*n)# 54*20 = 1080\nprint \"m/n = %d\" % (m/n) # 54/20 = 2\nprint \"m modulo by n = %d\" % (m%n) # 54%20 = 14",
+ "input": [
+ "\n",
+ "\n",
+ "# tests operators +, -, *, /, and %:\n",
+ "m=54\n",
+ "n=20\n",
+ "print \"m = %d and n = %d\" %(m,n)\n",
+ "print \"m+n = %d\" % (m+n) # 54+20 = 74\n",
+ "print \"m-n = %d\" % (m-n) # 54-20 = 34\n",
+ "print \"m*n = %d\" % (m*n)# 54*20 = 1080\n",
+ "print \"m/n = %d\" % (m/n) # 54/20 = 2\n",
+ "print \"m modulo by n = %d\" % (m%n) # 54%20 = 14"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "m = 54 and n = 20\nm+n = 74\nm-n = 34\nm*n = 1080\nm/n = 2\nm modulo by n = 14\n"
+ "text": [
+ "m = 54 and n = 20\n",
+ "m+n = 74\n",
+ "m-n = 34\n",
+ "m*n = 1080\n",
+ "m/n = 2\n",
+ "m modulo by n = 14\n"
+ ]
}
],
"prompt_number": 4
@@ -70,14 +130,29 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.5 Applying the Pre-increment and Post-increment Operators\n'''\n\n# shows the difference between m++ and ++m:\nm = 44\nm += 1\nn = m\nprint \"m = %d , n = %d\" %(m,n)\nm = 44\nn = m # the post-increment operator is applied to m\nm += 1\nprint \"m = %d , n = %d\" %(m,n)\n",
+ "input": [
+ "\n",
+ "\n",
+ "# shows the difference between m++ and ++m:\n",
+ "m = 44\n",
+ "m += 1\n",
+ "n = m\n",
+ "print \"m = %d , n = %d\" %(m,n)\n",
+ "m = 44\n",
+ "n = m # the post-increment operator is applied to m\n",
+ "m += 1\n",
+ "print \"m = %d , n = %d\" %(m,n)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "m = 45 , n = 45\nm = 45 , n = 44\n"
+ "text": [
+ "m = 45 , n = 45\n",
+ "m = 45 , n = 44\n"
+ ]
}
],
"prompt_number": 5
@@ -85,14 +160,37 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.6 Applying Composite Arithmetic Assignment Operators\n'''\n\n# tests arithmetic assignment operators:\nn=22\nprint \"n = %d\" % n\nn += 9 # adds 9 to n\nprint \"After n += 9, n = %d\" % n\nn -= 5 # subtracts 5 from n\nprint \"After n -= 5, n = %d\" % n\nn *= 2 # multiplies n by 3\nprint \"After n *= 2, n = %d\" % n \nn /= 3 # divides n by 9\nprint \"After n /= 3, n = %d\" % n \nn %= 7 # reduces n to the remainder from dividing by 4\nprint 'After n modulo by 7 n = %d' %n",
+ "input": [
+ "\n",
+ "\n",
+ "# tests arithmetic assignment operators:\n",
+ "n=22\n",
+ "print \"n = %d\" % n\n",
+ "n += 9 # adds 9 to n\n",
+ "print \"After n += 9, n = %d\" % n\n",
+ "n -= 5 # subtracts 5 from n\n",
+ "print \"After n -= 5, n = %d\" % n\n",
+ "n *= 2 # multiplies n by 3\n",
+ "print \"After n *= 2, n = %d\" % n \n",
+ "n /= 3 # divides n by 9\n",
+ "print \"After n /= 3, n = %d\" % n \n",
+ "n %= 7 # reduces n to the remainder from dividing by 4\n",
+ "print 'After n modulo by 7 n = %d' %n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 22\nAfter n += 9, n = 31\nAfter n -= 5, n = 26\nAfter n *= 2, n = 52\nAfter n /= 3, n = 17\nAfter n modulo by 7 n = 3\n"
+ "text": [
+ "n = 22\n",
+ "After n += 9, n = 31\n",
+ "After n -= 5, n = 26\n",
+ "After n *= 2, n = 52\n",
+ "After n /= 3, n = 17\n",
+ "After n modulo by 7 n = 3\n"
+ ]
}
],
"prompt_number": 6
@@ -100,14 +198,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.7 Floating-Point Arithmetic\nThis program is nearly the same as the one in Example 2.4. The important difference is that these\nvariables are declared to have the floating-point type double instead of the integer type int.\n'''\n\n# tests the floating-point operators +, -, *, and /:\nx=54.0\ny=20.0\nprint \"x = %f and y = %f\" %(x,y)\nprint \"x+y = %f\" %(x+y) # 54.0+20.0 = 74.0\nprint \"x-y = %f\" % (x-y) # 54.0-20.0 = 34.0\nprint \"x*y = %f\" %( x*y) # 54.0*20.0 = 1080.0\nprint \"x/y = %f\" % (x/y) # 54.0/20.0 = 2.7\n",
+ "input": [
+ "\n",
+ "\n",
+ "# tests the floating-point operators +, -, *, and /:\n",
+ "x=54.0\n",
+ "y=20.0\n",
+ "print \"x = %f and y = %f\" %(x,y)\n",
+ "print \"x+y = %f\" %(x+y) # 54.0+20.0 = 74.0\n",
+ "print \"x-y = %f\" % (x-y) # 54.0-20.0 = 34.0\n",
+ "print \"x*y = %f\" %( x*y) # 54.0*20.0 = 1080.0\n",
+ "print \"x/y = %f\" % (x/y) # 54.0/20.0 = 2.7\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 54.000000 and y = 20.000000\nx+y = 74.000000\nx-y = 34.000000\nx*y = 1080.000000\nx/y = 2.700000\n"
+ "text": [
+ "x = 54.000000 and y = 20.000000\n",
+ "x+y = 74.000000\n",
+ "x-y = 34.000000\n",
+ "x*y = 1080.000000\n",
+ "x/y = 2.700000\n"
+ ]
}
],
"prompt_number": 7
@@ -115,14 +230,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.8 Using the sizeof Operator\nThis program tells you how much space each of the undamental types uses:\nNote : Python has few number of data types so output would be differ.\n'''\nimport sys\n# prints the storage sizes of the fundamental types:\nprint \"Number of bytes used:\\n\"\n\nprint \" char: %d \" % sys.getsizeof('a')\nprint \" int : %d \" % sys.getsizeof(int(1))\nprint \" string : %d \" % sys.getsizeof(str('hellololdei'))\nprint \"float : %d\" % sys.getsizeof(float(1.1))",
+ "input": [
+ "\n",
+ "import sys\n",
+ "# prints the storage sizes of the fundamental types:\n",
+ "print \"Number of bytes used:\\n\"\n",
+ "\n",
+ "print \" char: %d \" % sys.getsizeof('a')\n",
+ "print \" int : %d \" % sys.getsizeof(int(1))\n",
+ "print \" string : %d \" % sys.getsizeof(str('hellololdei'))\n",
+ "print \"float : %d\" % sys.getsizeof(float(1.1))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Number of bytes used:\n\n char: 22 \n int : 12 \n string : 32 \nfloat : 16\n"
+ "text": [
+ "Number of bytes used:\n",
+ "\n",
+ " char: 22 \n",
+ " int : 12 \n",
+ " string : 32 \n",
+ "float : 16\n"
+ ]
}
],
"prompt_number": 8
@@ -130,14 +262,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.9 Reading from the <cfloat> Header File\nThis program tells you the precision and magnitude range that the float type has on your system:\n'''\nimport sys\n# prints the storage sizes of the fundamental types:\nfbits = 8*sys.getsizeof(float(123))\n\n# each byte contains 8 bits\nprint \"float uses : %d bits:\\n\\t\" % fbits \n",
+ "input": [
+ "\n",
+ "import sys\n",
+ "# prints the storage sizes of the fundamental types:\n",
+ "fbits = 8*sys.getsizeof(float(123))\n",
+ "\n",
+ "# each byte contains 8 bits\n",
+ "print \"float uses : %d bits:\\n\\t\" % fbits \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "float uses : 128 bits:\n\t\n"
+ "text": [
+ "float uses : 128 bits:\n",
+ "\t\n"
+ ]
}
],
"prompt_number": 9
@@ -145,14 +288,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nThis program casts a double value into int value:\n'''\n\n# casts a double value as an int:\nv = 1234.56789\nn = int(v);\nprint \"v = %f, n = %d\" %(v,n)",
+ "input": [
+ "\n",
+ "\n",
+ "# casts a double value as an int:\n",
+ "v = 1234.56789\n",
+ "n = int(v);\n",
+ "print \"v = %f, n = %d\" %(v,n)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "v = 1234.567890, n = 1234\n"
+ "text": [
+ "v = 1234.567890, n = 1234\n"
+ ]
}
],
"prompt_number": 10
@@ -160,14 +312,36 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.11 Promotion of Types\nThis program promotes a char to a short to an int to a float to a double:\nNote : Python is type independent. So would give output differ.\n'''\n# prints promoted vales of 65 from char to double:\nc='A'\nprint \"char c = \" + c\nk=c;\nprint \"k = \" + k \nm=k;\nprint \"m = \" + m \nn=m\nprint \"n = \" + n\nx=m\nprint \"x = \" + x \ny=x\nprint \"y = \" + y",
+ "input": [
+ "\n",
+ "# prints promoted vales of 65 from char to double:\n",
+ "c='A'\n",
+ "print \"char c = \" + c\n",
+ "k=c;\n",
+ "print \"k = \" + k \n",
+ "m=k;\n",
+ "print \"m = \" + m \n",
+ "n=m\n",
+ "print \"n = \" + n\n",
+ "x=m\n",
+ "print \"x = \" + x \n",
+ "y=x\n",
+ "print \"y = \" + y"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "char c = A\nk = A\nm = A\nn = A\nx = A\ny = A\n"
+ "text": [
+ "char c = A\n",
+ "k = A\n",
+ "m = A\n",
+ "n = A\n",
+ "x = A\n",
+ "y = A\n"
+ ]
}
],
"prompt_number": 11
@@ -175,14 +349,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.12 Integer Overflow\nThis program repeatedly multiplies n by 1000 until it overflows.\n'''\n# prints n until it overflows:\nn=1000\nprint \"n = %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n = %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n = %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n = %d\" % n\n",
+ "input": [
+ "\n",
+ "# prints n until it overflows:\n",
+ "n=1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 1000\nn = 1000000\nn = 1000000000\nn = 1000000000000\n"
+ "text": [
+ "n = 1000\n",
+ "n = 1000000\n",
+ "n = 1000000000\n",
+ "n = 1000000000000\n"
+ ]
}
],
"prompt_number": 12
@@ -190,14 +380,33 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.13 Floating-point Overflow\nThis program is similar to the one in Example 2.12. It repeatedly squares x until it overflows.\n'''\n\n# prints x until it overflows:\nx=1000.0\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\n",
+ "input": [
+ "\n",
+ "# prints x until it overflows:\n",
+ "x=1000.0\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 1000.000000\nx = 1000000.000000\nx = 1000000000000.000000\nx = 999999999999999983222784.000000\nx = 1000000000000000043845843045076197354634047651840.000000\n"
+ "text": [
+ "x = 1000.000000\n",
+ "x = 1000000.000000\n",
+ "x = 1000000000000.000000\n",
+ "x = 999999999999999983222784.000000\n",
+ "x = 1000000000000000043845843045076197354634047651840.000000\n"
+ ]
}
],
"prompt_number": 13
@@ -205,14 +414,34 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.14 Round-off Error\nThis program does some simple arithmetic to illustrate roundoff error:\n'''\n\n# illustrates round-off error::\nx = 1000/3.0\nprint \"x = %f\" %x # x = 1000/3\ny = x - 333.0\nprint \"y = %f\" % y # y = 1/3\nz = 3*y - 1.0\nprint \"z = %f\" %z # z = 3(1/3) - 1\nif (z == 0):\n print \"z == 0.\\n\"\nelse:\n print \"z does not equal 0.\\n\" # z != 0\n",
+ "input": [
+ "\n",
+ "\n",
+ "# illustrates round-off error::\n",
+ "x = 1000/3.0\n",
+ "print \"x = %f\" %x # x = 1000/3\n",
+ "y = x - 333.0\n",
+ "print \"y = %f\" % y # y = 1/3\n",
+ "z = 3*y - 1.0\n",
+ "print \"z = %f\" %z # z = 3(1/3) - 1\n",
+ "if (z == 0):\n",
+ " print \"z == 0.\\n\"\n",
+ "else:\n",
+ " print \"z does not equal 0.\\n\" # z != 0\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 333.333333\ny = 0.333333\nz = -0.000000\nz does not equal 0.\n\n"
+ "text": [
+ "x = 333.333333\n",
+ "y = 0.333333\n",
+ "z = -0.000000\n",
+ "z does not equal 0.\n",
+ "\n"
+ ]
}
],
"prompt_number": 14
@@ -220,7 +449,34 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.15 Hidden Round-off Error\nThis program implements the quadratic formula to solve quadratic equations.\n'''\nimport math\n\n# implements the quadratic formula\na = float(raw_input(\"Enter the coefficients of a quadratic equation:\\n a : \"))\nb = float(raw_input('b : '))\nc = float(raw_input('c : '))\n\nprint \"The equation is: \",\nprint a,\nprint \"*x*x + \",\nprint b,\nprint \"*x + \" ,\nprint c,\nprint \" = 0\" \n\nd = b*b - 4*a*c # discriminant\nsqrtd = math.sqrt(d)\nx1 = (-b + sqrtd)/(2*a)\nx2 = (-b - sqrtd)/(2*a)\nprint \"The solutions are:\"\nprint \"\\tx1 = %f\" % x1\nprint \"\\tx2 = %f\" % x2\nprint \"Check:\" \nprint \"\\ta*x1*x1 + b*x1 + c = %f\" %( a*x1*x1 + b*x1 + c)\nprint \"\\ta*x2*x2 + b*x2 + c = %f\" %( a*x2*x2 + b*x2 + c)\n",
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "# implements the quadratic formula\n",
+ "a = float(raw_input(\"Enter the coefficients of a quadratic equation:\\n a : \"))\n",
+ "b = float(raw_input('b : '))\n",
+ "c = float(raw_input('c : '))\n",
+ "\n",
+ "print \"The equation is: \",\n",
+ "print a,\n",
+ "print \"*x*x + \",\n",
+ "print b,\n",
+ "print \"*x + \" ,\n",
+ "print c,\n",
+ "print \" = 0\" \n",
+ "\n",
+ "d = b*b - 4*a*c # discriminant\n",
+ "sqrtd = math.sqrt(d)\n",
+ "x1 = (-b + sqrtd)/(2*a)\n",
+ "x2 = (-b - sqrtd)/(2*a)\n",
+ "print \"The solutions are:\"\n",
+ "print \"\\tx1 = %f\" % x1\n",
+ "print \"\\tx2 = %f\" % x2\n",
+ "print \"Check:\" \n",
+ "print \"\\ta*x1*x1 + b*x1 + c = %f\" %( a*x1*x1 + b*x1 + c)\n",
+ "print \"\\ta*x2*x2 + b*x2 + c = %f\" %( a*x2*x2 + b*x2 + c)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -228,24 +484,39 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter the coefficients of a quadratic equation:\n a : 2\n"
+ "text": [
+ "Enter the coefficients of a quadratic equation:\n",
+ " a : 2\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "b : 1\n"
+ "text": [
+ "b : 1\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "c : -3\n"
+ "text": [
+ "c : -3\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The equation is: 2.0 *x*x + 1.0 *x + -3.0 = 0\nThe solutions are:\n\tx1 = 1.000000\n\tx2 = -1.500000\nCheck:\n\ta*x1*x1 + b*x1 + c = 0.000000\n\ta*x2*x2 + b*x2 + c = 0.000000\n"
+ "text": [
+ "The equation is: 2.0 *x*x + 1.0 *x + -3.0 = 0\n",
+ "The solutions are:\n",
+ "\tx1 = 1.000000\n",
+ "\tx2 = -1.500000\n",
+ "Check:\n",
+ "\ta*x1*x1 + b*x1 + c = 0.000000\n",
+ "\ta*x2*x2 + b*x2 + c = 0.000000\n"
+ ]
}
],
"prompt_number": 15
@@ -253,7 +524,14 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.17 Scientific Format\nThis program shows how floating-point values may be input in scientific format:\n'''\n\n# prints double values in scientific e-format:\nx = float(raw_input(\"Enter float: \"))\nprint \"Its reciprocal is: \",\nprint 1/x ",
+ "input": [
+ "\n",
+ "\n",
+ "# prints double values in scientific e-format:\n",
+ "x = float(raw_input(\"Enter float: \"))\n",
+ "print \"Its reciprocal is: \",\n",
+ "print 1/x "
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -261,12 +539,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter float: 234.567e89\n"
+ "text": [
+ "Enter float: 234.567e89\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Its reciprocal is: 4.2631742743e-92\n"
+ "text": [
+ "Its reciprocal is: 4.2631742743e-92\n"
+ ]
}
],
"prompt_number": 16
@@ -274,7 +556,19 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.18 Scope of Variables\nNOte : Python does not have variable scopes. Once variable is declared then it stays live until program gets exit.\nso it wont give any errors.\n\n'''\n# illustrates the scope of variables:\nx = 11\n# ERROR: this is not in the scope of x\nif True:\n x = 22 # OK: this is in the scope of x\n y = 33 # ERROR: this is not in the scope of y\n x = 44 # OK: this is in the scope of x\n y = 55 # OK: this is in the scope of y\nx = 66 # OK: this is in the scope of x\ny = 77 # ERROR: this is not in the scope of y\n",
+ "input": [
+ "\n",
+ "# illustrates the scope of variables:\n",
+ "x = 11\n",
+ "# ERROR: this is not in the scope of x\n",
+ "if True:\n",
+ " x = 22 # OK: this is in the scope of x\n",
+ " y = 33 # ERROR: this is not in the scope of y\n",
+ " x = 44 # OK: this is in the scope of x\n",
+ " y = 55 # OK: this is in the scope of y\n",
+ "x = 66 # OK: this is in the scope of x\n",
+ "y = 77 # ERROR: this is not in the scope of y\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -283,14 +577,33 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 2.19 Nested and Parallel Scopes\nNOte : Python does not have variable scopes. Once variable is declared then it stays live until program gets exit.\nso output would be differ.\n'''\n# this x is global\nx = 11\n\nif True:\n # illustrates the nested and parallel scopes:\n x = 22\n # begin scope of internal block\n if True:\n x = 33\n print \"In block inside main(): x = %d \" % x\n # end scope of internal block\nprint \"In main(): x = %d\" %x \nprint \"In main(): x = %d \"% x",
+ "input": [
+ "\n",
+ "# this x is global\n",
+ "x = 11\n",
+ "\n",
+ "if True:\n",
+ " # illustrates the nested and parallel scopes:\n",
+ " x = 22\n",
+ " # begin scope of internal block\n",
+ " if True:\n",
+ " x = 33\n",
+ " print \"In block inside main(): x = %d \" % x\n",
+ " # end scope of internal block\n",
+ "print \"In main(): x = %d\" %x \n",
+ "print \"In main(): x = %d \"% x"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "In block inside main(): x = 33 \nIn main(): x = 33\nIn main(): x = 33 \n"
+ "text": [
+ "In block inside main(): x = 33 \n",
+ "In main(): x = 33\n",
+ "In main(): x = 33 \n"
+ ]
}
],
"prompt_number": 18
@@ -298,7 +611,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb
index 8a41802e..577b8d76 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch3"
+ "name": "",
+ "signature": "sha256:0e3c85fb42769559f9f8805f3e4d4314b9161e1f9e7c2177b9fdd0a81efd83fb"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,31 +11,47 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.1 Testing for Divisibility\nThis program tests if one positive integer is not divisible by another:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter two positive integers: \";\n",
+ "n = int(raw_input())\n",
+ "d = int(raw_input())\n",
+ "if (n%d):\n",
+ " print \"%d is not divisible by %d\" %(n,d)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two positive integers: \n"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "66\n"
+ "text": [
+ "66\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "66 is not divisible by 7\n"
+ "text": [
+ "66 is not divisible by 7\n"
+ ]
}
],
"prompt_number": 1
@@ -42,31 +59,48 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.2 Testing for Divisibility Again\nThis program is the same as the program in Example 3.1 except that the if statement has been replaced\nby an if..else statement:\n'''\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)\nelse:\n print \"%d is divisible by %d\" %(n,d)\n",
+ "input": [
+ "\n",
+ "print \"Enter two positive integers: \";\n",
+ "n = int(raw_input())\n",
+ "d = int(raw_input())\n",
+ "if (n%d):\n",
+ " print \"%d is not divisible by %d\" %(n,d)\n",
+ "else:\n",
+ " print \"%d is divisible by %d\" %(n,d)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two positive integers: \n"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "56\n"
+ "text": [
+ "56\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "56 is divisible by 7\n"
+ "text": [
+ "56 is divisible by 7\n"
+ ]
}
],
"prompt_number": 2
@@ -74,31 +108,50 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.3 The Minimum of Two Integers\nThis program prints the minimum of the two integers entered:\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\n\nif (m < n):\n print \"%d is the minimum.\" %m\nelse:\n print \"%d is the minimum.\" %n\n\n",
+ "input": [
+ "\n",
+ "print \"Enter two integers: \"\n",
+ "m = int(raw_input())\n",
+ "n = int(raw_input())\n",
+ "\n",
+ "if (m < n):\n",
+ " print \"%d is the minimum.\" %m\n",
+ "else:\n",
+ " print \"%d is the minimum.\" %n\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two integers: \n"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "55 is the minimum.\n"
+ "text": [
+ "55 is the minimum.\n"
+ ]
}
],
"prompt_number": 3
@@ -106,7 +159,15 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.4 A Common Programming Error\nThis program is erroneous:\n'''\nprint \"Enter an integer: \"\nn = int(raw_input())\nif (n = 22):\n print \"%d = 22\" %n\nelse: \n print \"%d != 22\" %n",
+ "input": [
+ "\n",
+ "print \"Enter an integer: \"\n",
+ "n = int(raw_input())\n",
+ "if (n = 22):\n",
+ " print \"%d = 22\" %n\n",
+ "else: \n",
+ " print \"%d != 22\" %n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -124,37 +185,61 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.5 The Minimum of Three Integers\nThis program is similar to the one in Example 3.3 except that it applies to three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nm=n1\n# now min <= n1\nif (n2 < m):\n m = n2 # now min <= n1 and min <= n2\nif (n3 < m):\n m = n3 # now min <= n1, min <= n2, and min <= n3\nprint \"Their minimum is %d\" % m",
+ "input": [
+ "\n",
+ "print \"Enter three integers: \"\n",
+ "n1 = int(raw_input())\n",
+ "n2 = int(raw_input())\n",
+ "n3 = int(raw_input())\n",
+ "\n",
+ "m=n1\n",
+ "# now min <= n1\n",
+ "if (n2 < m):\n",
+ " m = n2 # now min <= n1 and min <= n2\n",
+ "if (n3 < m):\n",
+ " m = n3 # now min <= n1, min <= n2, and min <= n3\n",
+ "print \"Their minimum is %d\" % m"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter three integers: \n"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Their minimum is 33\n"
+ "text": [
+ "Their minimum is 33\n"
+ ]
}
],
"prompt_number": 5
@@ -162,31 +247,53 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.6 A Statement Block within an if Statement\nThis program inputs two integers and then outputs them in increasing order:\n'''\n\nprint \"Enter two integers: \"\nx = int(raw_input())\ny = int(raw_input())\n\nif (x > y):\n temp=x\n x = y\n y = temp\n \n\nprint \"%d <= %d\" %(x,y)",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter two integers: \"\n",
+ "x = int(raw_input())\n",
+ "y = int(raw_input())\n",
+ "\n",
+ "if (x > y):\n",
+ " temp=x\n",
+ " x = y\n",
+ " y = temp\n",
+ " \n",
+ "\n",
+ "print \"%d <= %d\" %(x,y)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two integers: \n"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "66\n"
+ "text": [
+ "66\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "44 <= 66\n"
+ "text": [
+ "44 <= 66\n"
+ ]
}
],
"prompt_number": 6
@@ -194,25 +301,52 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.7 Using Blocks to Limit Scope\nThis program uses the same name n for three different variables:\n'''\nn=44\nprint \"n = %d\" % n \nif True:\n # scope extends over 4 lines\n print \"Enter an integer: \"\n n = int(raw_input())\n print \"n = %d\" % n \n\nif True:\n print \"n = %d\" % n \n # the n that was declared first\nif True:\n print \"n = %d\" % n \n\nprint \"n = %d\" % n ",
+ "input": [
+ "\n",
+ "n=44\n",
+ "print \"n = %d\" % n \n",
+ "if True:\n",
+ " # scope extends over 4 lines\n",
+ " print \"Enter an integer: \"\n",
+ " n = int(raw_input())\n",
+ " print \"n = %d\" % n \n",
+ "\n",
+ "if True:\n",
+ " print \"n = %d\" % n \n",
+ " # the n that was declared first\n",
+ "if True:\n",
+ " print \"n = %d\" % n \n",
+ "\n",
+ "print \"n = %d\" % n "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 44\nEnter an integer: \n"
+ "text": [
+ "n = 44\n",
+ "Enter an integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 77\nn = 77\nn = 77\nn = 77\n"
+ "text": [
+ "n = 77\n",
+ "n = 77\n",
+ "n = 77\n",
+ "n = 77\n"
+ ]
}
],
"prompt_number": 7
@@ -220,37 +354,60 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.8 Using Compound Conditions\nThis program has the same effect as the one in Example 3.5 on page 39. This version uses compound\nconditions to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 <= n2 and n1 <= n3):\n print \"Their minimum is %d\" % n1\n \nif (n2 <= n1 and n2 <= n3):\n print \"Their minimum is %d \" % n2 \nif (n3 <= n1 and n3 <= n2):\n print \"Their minimum is %d\" % n3 \n",
+ "input": [
+ "\n",
+ "print \"Enter three integers: \"\n",
+ "n1 = int(raw_input())\n",
+ "n2 = int(raw_input())\n",
+ "n3 = int(raw_input())\n",
+ "if (n1 <= n2 and n1 <= n3):\n",
+ " print \"Their minimum is %d\" % n1\n",
+ " \n",
+ "if (n2 <= n1 and n2 <= n3):\n",
+ " print \"Their minimum is %d \" % n2 \n",
+ "if (n3 <= n1 and n3 <= n2):\n",
+ " print \"Their minimum is %d\" % n3 \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter three integers: \n"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Their minimum is 33 \n"
+ "text": [
+ "Their minimum is 33 \n"
+ ]
}
],
"prompt_number": 8
@@ -258,25 +415,40 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.9 User-Friendly Input\nThis program allows the user to input either a 'Y' or a 'y' for 'yes':\n'''\n\nprint \"Are you enrolled (y/n): \"\nans = raw_input()\nif (ans == 'Y' or ans == 'y'):\n print \"You are enrolled.\\n\"\nelse: \n print \"You are not enrolled.\\n\"\n",
+ "input": [
+ "\n",
+ "print \"Are you enrolled (y/n): \"\n",
+ "ans = raw_input()\n",
+ "if (ans == 'Y' or ans == 'y'):\n",
+ " print \"You are enrolled.\\n\"\n",
+ "else: \n",
+ " print \"You are not enrolled.\\n\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Are you enrolled (y/n): \n"
+ "text": [
+ "Are you enrolled (y/n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "y\n"
+ "text": [
+ "y\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "You are enrolled.\n\n"
+ "text": [
+ "You are enrolled.\n",
+ "\n"
+ ]
}
],
"prompt_number": 9
@@ -284,31 +456,50 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.10 Short-Circuiting\nThis program tests integer divisibility:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\n\nif (d != 0 and n%d == 0): \n print \"%d divides %d\" %(d,n)\nelse:\n print \"%d does not divide %d\"% (d,n)",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter two positive integers: \";\n",
+ "n = int(raw_input())\n",
+ "d = int(raw_input())\n",
+ "\n",
+ "if (d != 0 and n%d == 0): \n",
+ " print \"%d divides %d\" %(d,n)\n",
+ "else:\n",
+ " print \"%d does not divide %d\"% (d,n)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two positive integers: \n"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "6\n"
+ "text": [
+ "6\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "6 does not divide 33\n"
+ "text": [
+ "6 does not divide 33\n"
+ ]
}
],
"prompt_number": 10
@@ -316,32 +507,49 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.11 Another Logical Error\nThis program is erroneous:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nif (n1 >= n2 >= n3):\n print \"max = x\"",
+ "input": [
+ "\n",
+ "print \"Enter three integers: \"\n",
+ "n1 = int(raw_input())\n",
+ "n2 = int(raw_input())\n",
+ "n3 = int(raw_input())\n",
+ "\n",
+ "if (n1 >= n2 >= n3):\n",
+ " print \"max = x\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter three integers: \n"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1\n"
+ "text": [
+ "1\n"
+ ]
}
],
"prompt_number": 11
@@ -349,31 +557,53 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.12 Nesting Selection Statements\nThis program has the same effect as the one in Example 3.10 on page 42:\n'''\nprint \"Enter two positive integers: \"\nn = int(raw_input())\nd = int(raw_input())\n\nif (d != 0):\n if (n%d == 0):\n print d,\n print \" divides %d\" % n \n else:\n print \"%d does not divide %d\" %(d,n)\nelse:\n print '%d does not divide %d '%(d,n)",
+ "input": [
+ "\n",
+ "print \"Enter two positive integers: \"\n",
+ "n = int(raw_input())\n",
+ "d = int(raw_input())\n",
+ "\n",
+ "if (d != 0):\n",
+ " if (n%d == 0):\n",
+ " print d,\n",
+ " print \" divides %d\" % n \n",
+ " else:\n",
+ " print \"%d does not divide %d\" %(d,n)\n",
+ "else:\n",
+ " print '%d does not divide %d '%(d,n)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two positive integers: \n"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "44 does not divide 55\n"
+ "text": [
+ "44 does not divide 55\n"
+ ]
}
],
"prompt_number": 12
@@ -381,37 +611,63 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.13 Using Nested Selection Statements\nThis program has the same effect as those in Example 3.5 on page 39 and Example 3.8 on page 41.\nThis version uses nested if..else statements to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 < n2):\n if (n1 < n3):\n print \"Their minimum is : %d\" % n1\n else:\n print \"Their minimum is : %d\" % n3\nelse: # n1 >= n2\n if (n2 < n3):\n print \"Their minimum is : %d\" % n2\n else:\n print \"Their minimum is %d\" % n3",
+ "input": [
+ "\n",
+ "print \"Enter three integers: \"\n",
+ "n1 = int(raw_input())\n",
+ "n2 = int(raw_input())\n",
+ "n3 = int(raw_input())\n",
+ "if (n1 < n2):\n",
+ " if (n1 < n3):\n",
+ " print \"Their minimum is : %d\" % n1\n",
+ " else:\n",
+ " print \"Their minimum is : %d\" % n3\n",
+ "else: # n1 >= n2\n",
+ " if (n2 < n3):\n",
+ " print \"Their minimum is : %d\" % n2\n",
+ " else:\n",
+ " print \"Their minimum is %d\" % n3"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter three integers: \n"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Their minimum is : 33\n"
+ "text": [
+ "Their minimum is : 33\n"
+ ]
}
],
"prompt_number": 13
@@ -419,58 +675,116 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.14 A Guessing Game\nThis program finds a number that the user selects from 1 to 8:\n'''\n\nprint \"Pick a number from 1 to 8.\" \nanswer = int(raw_input())\nprint \"Is it less than 5? (y|n): \"\nanswer = raw_input()\nif (answer == 'y'): # 1 <= n <= 4\n print \"Is it less than 3? (y|n): \"\n answer = raw_input() \n if (answer == 'y'): # 1 <= n <= 2\n print \"Is it less than 2? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 1.\"\n else:\n print \"Your number is 2.\"\n else: # 3 <= n <= 4\n print \"Is it less than 4? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 3.\"\n else:\n print \"Your number is 4.\"\nelse: # 5 <= n <= 8\n print \"Is it less than 7? (y|n): \"\n answer = raw_input()\n if (answer == 'y'): # 5 <= n <= 6\n print \"Is it less than 6? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 5.\"\n else:\n print \"Your number is 6.\" \n else: # 7 <= n <= 8\n print \"Is it less than 8? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 7.\" \n else:\n print \"Your number is 8.\"",
+ "input": [
+ "\n",
+ "print \"Pick a number from 1 to 8.\" \n",
+ "answer = int(raw_input())\n",
+ "print \"Is it less than 5? (y|n): \"\n",
+ "answer = raw_input()\n",
+ "if (answer == 'y'): # 1 <= n <= 4\n",
+ " print \"Is it less than 3? (y|n): \"\n",
+ " answer = raw_input() \n",
+ " if (answer == 'y'): # 1 <= n <= 2\n",
+ " print \"Is it less than 2? (y|n): \"\n",
+ " answer = raw_input()\n",
+ " if (answer == 'y'):\n",
+ " print \"Your number is 1.\"\n",
+ " else:\n",
+ " print \"Your number is 2.\"\n",
+ " else: # 3 <= n <= 4\n",
+ " print \"Is it less than 4? (y|n): \"\n",
+ " answer = raw_input()\n",
+ " if (answer == 'y'):\n",
+ " print \"Your number is 3.\"\n",
+ " else:\n",
+ " print \"Your number is 4.\"\n",
+ "else: # 5 <= n <= 8\n",
+ " print \"Is it less than 7? (y|n): \"\n",
+ " answer = raw_input()\n",
+ " if (answer == 'y'): # 5 <= n <= 6\n",
+ " print \"Is it less than 6? (y|n): \"\n",
+ " answer = raw_input()\n",
+ " if (answer == 'y'):\n",
+ " print \"Your number is 5.\"\n",
+ " else:\n",
+ " print \"Your number is 6.\" \n",
+ " else: # 7 <= n <= 8\n",
+ " print \"Is it less than 8? (y|n): \"\n",
+ " answer = raw_input()\n",
+ " if (answer == 'y'):\n",
+ " print \"Your number is 7.\" \n",
+ " else:\n",
+ " print \"Your number is 8.\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Pick a number from 1 to 8.\n"
+ "text": [
+ "Pick a number from 1 to 8.\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "6\n"
+ "text": [
+ "6\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Is it less than 5? (y|n): \n"
+ "text": [
+ "Is it less than 5? (y|n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "n\n"
+ "text": [
+ "n\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Is it less than 7? (y|n): \n"
+ "text": [
+ "Is it less than 7? (y|n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "y\n"
+ "text": [
+ "y\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Is it less than 6? (y|n): \n"
+ "text": [
+ "Is it less than 6? (y|n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "n\n"
+ "text": [
+ "n\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your number is 6.\n"
+ "text": [
+ "Your number is 6.\n"
+ ]
}
],
"prompt_number": 14
@@ -478,7 +792,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.15 Using the else if Construct for Parallel Alternatives\nThis program requests the users language and then prints a greeting in that language:\n'''\nlanguage = raw_input(\"Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): \")\n\nif (language == 'e'): \n print \"Welcome to ProjectEuclid.\"\nelif (language == 'f'):\n print \"Bon jour, ProjectEuclid.\"\nelif (language == 'g'):\n print \"Guten tag, ProjectEuclid.\"\nelif (language == 'i'):\n print \"Bon giorno, ProjectEuclid.\"\nelif (language == 'r'):\n print \"Dobre utre, ProjectEuclid.\"\nelse:\n print \"Sorry; we don't speak your language.\"\n",
+ "input": [
+ "\n",
+ "language = raw_input(\"Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): \")\n",
+ "\n",
+ "if (language == 'e'): \n",
+ " print \"Welcome to ProjectEuclid.\"\n",
+ "elif (language == 'f'):\n",
+ " print \"Bon jour, ProjectEuclid.\"\n",
+ "elif (language == 'g'):\n",
+ " print \"Guten tag, ProjectEuclid.\"\n",
+ "elif (language == 'i'):\n",
+ " print \"Bon giorno, ProjectEuclid.\"\n",
+ "elif (language == 'r'):\n",
+ " print \"Dobre utre, ProjectEuclid.\"\n",
+ "else:\n",
+ " print \"Sorry; we don't speak your language.\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -486,12 +816,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n"
+ "text": [
+ "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Bon giorno, ProjectEuclid.\n"
+ "text": [
+ "Bon giorno, ProjectEuclid.\n"
+ ]
}
],
"prompt_number": 15
@@ -499,7 +833,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.16 Using the else if Construct to Select a Range of Scores\nThis program converts a test score into its equivalent letter grade:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n",
+ "input": [
+ "\n",
+ "score = int(raw_input(\"Enter your test score: \"))\n",
+ "a = int(score/10)\n",
+ "if a == 10 or a == 9:\n",
+ " print \"Your grade is an A.\"\n",
+ "elif a == 8:\n",
+ " print \"Your grade is a B.\" \n",
+ "elif a == 7:\n",
+ " print \"Your grade is a C.\" \n",
+ "elif a == 6:\n",
+ " print \"Your grade is a D.\"\n",
+ "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n",
+ " print \"Your grade is an F.\" \n",
+ "else:\n",
+ " print \"Error: score is out of range.\\n\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -507,12 +857,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter your test score: 83\n"
+ "text": [
+ "Enter your test score: 83\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your grade is a B.\n"
+ "text": [
+ "Your grade is a B.\n"
+ ]
}
],
"prompt_number": 16
@@ -520,7 +874,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.17 Using a switch Statement to Select a Range of Scores\nThis program has the same effect as the one in Example 3.16:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"",
+ "input": [
+ "\n",
+ "score = int(raw_input(\"Enter your test score: \"))\n",
+ "a = int(score/10)\n",
+ "if a == 10 or a == 9:\n",
+ " print \"Your grade is an A.\"\n",
+ "elif a == 8:\n",
+ " print \"Your grade is a B.\" \n",
+ "elif a == 7:\n",
+ " print \"Your grade is a C.\" \n",
+ "elif a == 6:\n",
+ " print \"Your grade is a D.\"\n",
+ "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n",
+ " print \"Your grade is an F.\" \n",
+ "else:\n",
+ " print \"Error: score is out of range.\\n\"\n",
+ "\n",
+ "print \"Goodbye.\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -528,12 +900,17 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter your test score: 83\n"
+ "text": [
+ "Enter your test score: 83\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your grade is a B.\nGoodbye.\n"
+ "text": [
+ "Your grade is a B.\n",
+ "Goodbye.\n"
+ ]
}
],
"prompt_number": 17
@@ -541,7 +918,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.18 An Erroneous Fall-through in a switch Statement\nThis program was intended to have the same effect as the one in Example 3.17. But with\nstatements, the program execution falls through all the case statements it encounters:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"",
+ "input": [
+ "\n",
+ "score = int(raw_input(\"Enter your test score: \"))\n",
+ "a = int(score/10)\n",
+ "if a == 10 or a == 9:\n",
+ " print \"Your grade is an A.\"\n",
+ "elif a == 8:\n",
+ " print \"Your grade is a B.\" \n",
+ "elif a == 7:\n",
+ " print \"Your grade is a C.\" \n",
+ "elif a == 6:\n",
+ " print \"Your grade is a D.\"\n",
+ "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n",
+ " print \"Your grade is an F.\" \n",
+ "else:\n",
+ " print \"Error: score is out of range.\\n\"\n",
+ "\n",
+ "print \"Goodbye.\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -549,12 +944,17 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter your test score: 83\n"
+ "text": [
+ "Enter your test score: 83\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your grade is a B.\nGoodbye.\n"
+ "text": [
+ "Your grade is a B.\n",
+ "Goodbye.\n"
+ ]
}
],
"prompt_number": 18
@@ -562,31 +962,46 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.19 Finding the Minimum Again\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\nprint min(m,n),\nprint 'is the minimum'",
+ "input": [
+ "\n",
+ "print \"Enter two integers: \"\n",
+ "m = int(raw_input())\n",
+ "n = int(raw_input())\n",
+ "print min(m,n),\n",
+ "print 'is the minimum'"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two integers: \n"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "33 is the minimum\n"
+ "text": [
+ "33 is the minimum\n"
+ ]
}
],
"prompt_number": 19
@@ -594,7 +1009,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb
index d137f53e..80faf7b0 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch4"
+ "name": "",
+ "signature": "sha256:151156e7bea70740a97337b774930eddac369834b8935c55926007a1955d4c91"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,25 +11,41 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.1 Using a while Loop to Compute a Sum of Consecutive Integers\nThis program computes the sum 1 + 2 + 3 + + n for an input integer n:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (i <= n):\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(i,s)",
+ "input": [
+ "\n",
+ "i=1\n",
+ "print \"Enter a positive integer: \"\n",
+ "n = int(raw_input())\n",
+ "s=0\n",
+ "while (i <= n):\n",
+ " s += i\n",
+ " i += 1\n",
+ "print \"The sum of the first %d integers is %d\" %(i,s)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The sum of the first 6 integers is 15\n"
+ "text": [
+ "The sum of the first 6 integers is 15\n"
+ ]
}
],
"prompt_number": 1
@@ -36,25 +53,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.2 Using a while Loop to Compute a Sum of Reciprocals\nThis program computes the sum of reciprocals s = 1 + 1/2 + 1/3 + 1/n where n is\ninteger for which n is greater than s\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\ns=0.0\ni=0\nwhile (s < bound):\n i += 1\n s += 1.0/i\n\nprint \"The sum of the first %d reciprocals is %f\" %(i,s)",
+ "input": [
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "bound = int(raw_input())\n",
+ "s=0.0\n",
+ "i=0\n",
+ "while (s < bound):\n",
+ " i += 1\n",
+ " s += 1.0/i\n",
+ "\n",
+ "print \"The sum of the first %d reciprocals is %f\" %(i,s)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The sum of the first 83 reciprocals is 5.002068\n"
+ "text": [
+ "The sum of the first 83 reciprocals is 5.002068\n"
+ ]
}
],
"prompt_number": 2
@@ -62,42 +96,65 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.3 Using a while Loop to Repeat a Computation\nThis program prints the square root of each number input by the user. It uses a while loop to allow any\nnumber of computations in a single run of the program:\n'''\nimport math\nprint \"Enter a positive number: \"\nx = float(raw_input())\nwhile (x > 0):\n print \"sqrt(%d) = %f \"%(x,math.sqrt(x))\n print \"Enter another positive number (or 0 to quit): \"\n x = float(raw_input())\n",
+ "input": [
+ "\n",
+ "import math\n",
+ "print \"Enter a positive number: \"\n",
+ "x = float(raw_input())\n",
+ "while (x > 0):\n",
+ " print \"sqrt(%d) = %f \"%(x,math.sqrt(x))\n",
+ " print \"Enter another positive number (or 0 to quit): \"\n",
+ " x = float(raw_input())\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive number: \n"
+ "text": [
+ "Enter a positive number: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "sqrt(5) = 2.236068 \nEnter another positive number (or 0 to quit): \n"
+ "text": [
+ "sqrt(5) = 2.236068 \n",
+ "Enter another positive number (or 0 to quit): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "3\n"
+ "text": [
+ "3\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "sqrt(3) = 1.732051 \nEnter another positive number (or 0 to quit): \n"
+ "text": [
+ "sqrt(3) = 1.732051 \n",
+ "Enter another positive number (or 0 to quit): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
}
],
"prompt_number": 3
@@ -105,25 +162,43 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.4 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=1\nprint \"Enter a positive integer: \";\nn = int(raw_input())\ns=0\nwhile(True):\n if (i > n):\n break # terminates the loop immediately\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)",
+ "input": [
+ "\n",
+ "i=1\n",
+ "print \"Enter a positive integer: \";\n",
+ "n = int(raw_input())\n",
+ "s=0\n",
+ "while(True):\n",
+ " if (i > n):\n",
+ " break # terminates the loop immediately\n",
+ " s += i\n",
+ " i += 1\n",
+ "print \"The sum of the first %d integers is %d\" %(n,s)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The sum of the first 5 integers is 15\n"
+ "text": [
+ "The sum of the first 5 integers is 15\n"
+ ]
}
],
"prompt_number": 5
@@ -131,25 +206,48 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.5 The Fibonacci Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "bound = int(raw_input())\n",
+ "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n",
+ "f0=0\n",
+ "f1=1\n",
+ "while (True):\n",
+ " f2 = f0 + f1\n",
+ " if (f2 > bound):\n",
+ " break\n",
+ " print \", %d\" % f2,\n",
+ " f0 = f1\n",
+ " f1 = f2\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ "text": [
+ "Fibonacci numbers < 10:\n",
+ "0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ ]
}
],
"prompt_number": 6
@@ -157,20 +255,40 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.6 Using the exit(0) Function\nThe exit() function provides another way to terminate a loop. When it executes, it terminates the\nprogram itself:\n'''\nimport sys\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n sys.exit(0)\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ",
+ "input": [
+ "\n",
+ "import sys\n",
+ "print \"Enter a positive integer: \"\n",
+ "bound = int(raw_input())\n",
+ "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n",
+ "f0=0\n",
+ "f1=1\n",
+ "while (True):\n",
+ " f2 = f0 + f1\n",
+ " if (f2 > bound):\n",
+ " sys.exit(0)\n",
+ " print \", %d\" % f2,\n",
+ " f0 = f1\n",
+ " f1 = f2\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"ename": "SystemExit",
@@ -184,12 +302,17 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8"
+ "text": [
+ "Fibonacci numbers < 10:\n",
+ "0, 1 , 1 , 2 , 3 , 5 , 8"
+ ]
},
{
"output_type": "stream",
"stream": "stderr",
- "text": "To exit: use 'exit', 'quit', or Ctrl-D.\n"
+ "text": [
+ "To exit: use 'exit', 'quit', or Ctrl-D.\n"
+ ]
}
],
"prompt_number": 7
@@ -197,25 +320,48 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2",
+ "input": [
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "bound = int(raw_input())\n",
+ "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n",
+ "f0=0\n",
+ "f1=1\n",
+ "# Error : infinite loop !\n",
+ "while (True):\n",
+ " f2 = f0 + f1\n",
+ " # By commenting the below if statement, it goes to infinite.\n",
+ " if (f2 > bound):\n",
+ " break\n",
+ " print \", %d\" % f2,\n",
+ " f0 = f1\n",
+ " f1 = f2"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ "text": [
+ "Fibonacci numbers < 10:\n",
+ "0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ ]
}
],
"prompt_number": 2
@@ -223,14 +369,35 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "bound = int(raw_input())\n",
+ "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n",
+ "f0=0\n",
+ "f1=1\n",
+ "# Error : infinite loop !\n",
+ "while (True):\n",
+ " f2 = f0 + f1\n",
+ " # By commenting the below if statement, it goes to infinite.\n",
+ " if (f2 > bound):\n",
+ " break\n",
+ " print \", %d\" % f2,\n",
+ " f0 = f1\n",
+ " f1 = f2"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \nFibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ "text": [
+ "Enter a positive integer: \n",
+ "Fibonacci numbers < 10:\n",
+ "0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ ]
}
],
"prompt_number": 1
@@ -238,25 +405,41 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.8 Using a do..while Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=0\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile i<=n:\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)\n",
+ "input": [
+ "\n",
+ "i=0\n",
+ "print \"Enter a positive integer: \"\n",
+ "n = int(raw_input())\n",
+ "s=0\n",
+ "while i<=n:\n",
+ " s += i\n",
+ " i += 1\n",
+ "print \"The sum of the first %d integers is %d\" %(n,s)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The sum of the first 10 integers is 55\n"
+ "text": [
+ "The sum of the first 10 integers is 55\n"
+ ]
}
],
"prompt_number": 3
@@ -264,25 +447,44 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.9 The Factorial Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Factorial numbers < %d:\\n1, 1\" %bound,\nf=1\ni=1\nwhile f < bound:\n i += 1\n f *= i\n print \", %d\" %f,\n",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "bound = int(raw_input())\n",
+ "print \"Factorial numbers < %d:\\n1, 1\" %bound,\n",
+ "f=1\n",
+ "i=1\n",
+ "while f < bound:\n",
+ " i += 1\n",
+ " f *= i\n",
+ " print \", %d\" %f,\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Factorial numbers < 10:\n1, 1 , 2 , 6 , 24\n"
+ "text": [
+ "Factorial numbers < 10:\n",
+ "1, 1 , 2 , 6 , 24\n"
+ ]
}
],
"prompt_number": 4
@@ -290,25 +492,40 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.10 Using a for Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0;\nfor i in range(0,n+1):\n s += i\nprint \"The sum of the first %d integers is %d\" %(n,s)\n",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "n = int(raw_input())\n",
+ "s=0;\n",
+ "for i in range(0,n+1):\n",
+ " s += i\n",
+ "print \"The sum of the first %d integers is %d\" %(n,s)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The sum of the first 10 integers is 55\n"
+ "text": [
+ "The sum of the first 10 integers is 55\n"
+ ]
}
],
"prompt_number": 5
@@ -316,25 +533,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.11 Reusing for Loop Control Variable Names\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nfor i in range(1,n/2): # the scope of this i is this loop\n s += i\n\nfor i in range(n/2,n+1): # the scope of this i is this loop\n s += i\nprint \"The sum of the first %d integers is %d\" % (n,s)\n",
+ "input": [
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "n = int(raw_input())\n",
+ "s=0\n",
+ "for i in range(1,n/2): # the scope of this i is this loop\n",
+ " s += i\n",
+ "\n",
+ "for i in range(n/2,n+1): # the scope of this i is this loop\n",
+ " s += i\n",
+ "print \"The sum of the first %d integers is %d\" % (n,s)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The sum of the first 10 integers is 55\n"
+ "text": [
+ "The sum of the first 10 integers is 55\n"
+ ]
}
],
"prompt_number": 6
@@ -342,25 +576,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.12 The Factorial Numbers Again\nThis program has the same effect as the one in Example 4.9 on page 65:\n'''\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\n\nprint \"Factorial numbers that are <= %d:\\n1, 1\" %bound,\nf=1\nfor i in range(2,bound+1):\n f *= i\n print \", %d\" % f,\n",
+ "input": [
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "bound = int(raw_input())\n",
+ "\n",
+ "print \"Factorial numbers that are <= %d:\\n1, 1\" %bound,\n",
+ "f=1\n",
+ "for i in range(2,bound+1):\n",
+ " f *= i\n",
+ " print \", %d\" % f,\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Factorial numbers that are <= 10:\n1, 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 , 3628800\n"
+ "text": [
+ "Factorial numbers that are <= 10:\n",
+ "1, 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 , 3628800\n"
+ ]
}
],
"prompt_number": 7
@@ -368,14 +619,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.13 Using a Descending for Loop\nThis program prints the first ten positive integers in reverse order:\n'''\n\nfor i in range(10,0,-1):\n print i,\n",
+ "input": [
+ "\n",
+ "\n",
+ "for i in range(10,0,-1):\n",
+ " print i,\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10 9 8 7 6 5 4 3 2 1\n"
+ "text": [
+ "10 9 8 7 6 5 4 3 2 1\n"
+ ]
}
],
"prompt_number": 8
@@ -383,25 +641,52 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.14 Using a for Loop with a Step Greater than One\nThis program determines whether an input number is prime:\n'''\nprime = True\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nif (n < 2):\n print \"%d is not prime.\" %n\n prime = False\nelif (n < 4):\n print \"%d is prime.\" %n\n prime = False\nelif (n%2 == 0):\n print \"%d = 2* %d\" %(n,n/2)\n prime = False\nelse:\n for d in range(3,n/2+1):\n if (n%d == 0):\n print \"%d = %d * %d\" %(n,d,n/d)\n prime = False\nif prime: \n print \"%d is prime.\"%n\n",
+ "input": [
+ "\n",
+ "prime = True\n",
+ "print \"Enter a positive integer: \"\n",
+ "n = int(raw_input())\n",
+ "if (n < 2):\n",
+ " print \"%d is not prime.\" %n\n",
+ " prime = False\n",
+ "elif (n < 4):\n",
+ " print \"%d is prime.\" %n\n",
+ " prime = False\n",
+ "elif (n%2 == 0):\n",
+ " print \"%d = 2* %d\" %(n,n/2)\n",
+ " prime = False\n",
+ "else:\n",
+ " for d in range(3,n/2+1):\n",
+ " if (n%d == 0):\n",
+ " print \"%d = %d * %d\" %(n,d,n/d)\n",
+ " prime = False\n",
+ "if prime: \n",
+ " print \"%d is prime.\"%n\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "11\n"
+ "text": [
+ "11\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "11 is prime.\n"
+ "text": [
+ "11 is prime.\n"
+ ]
}
],
"prompt_number": 9
@@ -409,49 +694,74 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.15 Using a Sentinel to Control a for Loop\nThis program finds the maximum of a sequence of input numbers:\n'''\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0:\n n = int(raw_input())\n if n > m :\n m = n\n\nprint \"max = %d\" % m",
+ "input": [
+ "\n",
+ "print \"Enter positive integers (0 to quit): \";\n",
+ "n = int(raw_input())\n",
+ "m = n\n",
+ "while n > 0:\n",
+ " n = int(raw_input())\n",
+ " if n > m :\n",
+ " m = n\n",
+ "\n",
+ "print \"max = %d\" % m"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter positive integers (0 to quit): \n"
+ "text": [
+ "Enter positive integers (0 to quit): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "19\n"
+ "text": [
+ "19\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "42\n"
+ "text": [
+ "42\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1\n"
+ "text": [
+ "1\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "max = 42\n"
+ "text": [
+ "max = 42\n"
+ ]
}
],
"prompt_number": 10
@@ -459,49 +769,76 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.16 Using a Loop Invariant to Prove that a for Loop is Correct\nThis program finds the minimum of a sequence of input numbers. It is similar to the program in\nExample 4.15:\n'''\n\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0: \n if n < m :\n m = n\n n = int(raw_input())\n\nprint \"min = %d\" % m\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "print \"Enter positive integers (0 to quit): \";\n",
+ "n = int(raw_input())\n",
+ "m = n\n",
+ "while n > 0: \n",
+ " if n < m :\n",
+ " m = n\n",
+ " n = int(raw_input())\n",
+ "\n",
+ "print \"min = %d\" % m\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter positive integers (0 to quit): \n"
+ "text": [
+ "Enter positive integers (0 to quit): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "19\n"
+ "text": [
+ "19\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "42\n"
+ "text": [
+ "42\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1\n"
+ "text": [
+ "1\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "min = 1\n"
+ "text": [
+ "min = 1\n"
+ ]
}
],
"prompt_number": 11
@@ -509,14 +846,28 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.17\n'''\n\nm = 95\nn = 11\nwhile m%n > 0:\n print \"%d modulo %d = %d\" %(m,n,m%n)\n m -= 3\n n += 1\n",
+ "input": [
+ "\n",
+ "m = 95\n",
+ "n = 11\n",
+ "while m%n > 0:\n",
+ " print \"%d modulo %d = %d\" %(m,n,m%n)\n",
+ " m -= 3\n",
+ " n += 1\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "95 modulo 11 = 7\n92 modulo 12 = 8\n89 modulo 13 = 11\n86 modulo 14 = 2\n83 modulo 15 = 8\n"
+ "text": [
+ "95 modulo 11 = 7\n",
+ "92 modulo 12 = 8\n",
+ "89 modulo 13 = 11\n",
+ "86 modulo 14 = 2\n",
+ "83 modulo 15 = 8\n"
+ ]
}
],
"prompt_number": 12
@@ -524,14 +875,34 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.18 Nesting for Loops\nThis program prints a multiplication table:\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n print \"%4d\" % (x*y),\n print \"\"\n",
+ "input": [
+ "\n",
+ "\n",
+ "for x in range(1,13):\n",
+ " for y in range(1,13):\n",
+ " print \"%4d\" % (x*y),\n",
+ " print \"\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": " 1 2 3 4 5 6 7 8 9 10 11 12 \n 2 4 6 8 10 12 14 16 18 20 22 24 \n 3 6 9 12 15 18 21 24 27 30 33 36 \n 4 8 12 16 20 24 28 32 36 40 44 48 \n 5 10 15 20 25 30 35 40 45 50 55 60 \n 6 12 18 24 30 36 42 48 54 60 66 72 \n 7 14 21 28 35 42 49 56 63 70 77 84 \n 8 16 24 32 40 48 56 64 72 80 88 96 \n 9 18 27 36 45 54 63 72 81 90 99 108 \n 10 20 30 40 50 60 70 80 90 100 110 120 \n 11 22 33 44 55 66 77 88 99 110 121 132 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n"
+ "text": [
+ " 1 2 3 4 5 6 7 8 9 10 11 12 \n",
+ " 2 4 6 8 10 12 14 16 18 20 22 24 \n",
+ " 3 6 9 12 15 18 21 24 27 30 33 36 \n",
+ " 4 8 12 16 20 24 28 32 36 40 44 48 \n",
+ " 5 10 15 20 25 30 35 40 45 50 55 60 \n",
+ " 6 12 18 24 30 36 42 48 54 60 66 72 \n",
+ " 7 14 21 28 35 42 49 56 63 70 77 84 \n",
+ " 8 16 24 32 40 48 56 64 72 80 88 96 \n",
+ " 9 18 27 36 45 54 63 72 81 90 99 108 \n",
+ " 10 20 30 40 50 60 70 80 90 100 110 120 \n",
+ " 11 22 33 44 55 66 77 88 99 110 121 132 \n",
+ " 12 24 36 48 60 72 84 96 108 120 132 144 \n"
+ ]
}
],
"prompt_number": 13
@@ -539,25 +910,59 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.19 Testing a Loop Invariant\n'''\nimport math\n# defines pow() and log()\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nd=0 # the discrete binary logarithm of n\np2d=1 # = 2^d\ni = n\nwhile i > 1:\n # INVARIANT: 2^d <= n/i < 2*2^d\n p2d=math.pow(2,d) # = 2^d\n print \"%2d <= %2d\" %(p2d,2*p2d)\n i /= 2\n d += 1\n\np2d=math.pow(2,d) # = 2^d\nprint \"%2d <= %2d < %2d\" %(p2d,n,2*p2d)\nprint \" The discrete binary logarithm of is %d\" % d \nlgn = math.log(n)/math.log(2) # base 2 logarithm\nprint \"The continuous binary logarithm of is %f\" % lgn",
+ "input": [
+ "\n",
+ "import math\n",
+ "# defines pow() and log()\n",
+ "\n",
+ "print \"Enter a positive integer: \"\n",
+ "n = int(raw_input())\n",
+ "d=0 # the discrete binary logarithm of n\n",
+ "p2d=1 # = 2^d\n",
+ "i = n\n",
+ "while i > 1:\n",
+ " # INVARIANT: 2^d <= n/i < 2*2^d\n",
+ " p2d=math.pow(2,d) # = 2^d\n",
+ " print \"%2d <= %2d\" %(p2d,2*p2d)\n",
+ " i /= 2\n",
+ " d += 1\n",
+ "\n",
+ "p2d=math.pow(2,d) # = 2^d\n",
+ "print \"%2d <= %2d < %2d\" %(p2d,n,2*p2d)\n",
+ "print \" The discrete binary logarithm of is %d\" % d \n",
+ "lgn = math.log(n)/math.log(2) # base 2 logarithm\n",
+ "print \"The continuous binary logarithm of is %f\" % lgn"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "17\n"
+ "text": [
+ "17\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " 1 <= 2\n 2 <= 4\n 4 <= 8\n 8 <= 16\n16 <= 17 < 32\n The discrete binary logarithm of is 4\nThe continuous binary logarithm of is 4.087463\n"
+ "text": [
+ " 1 <= 2\n",
+ " 2 <= 4\n",
+ " 4 <= 8\n",
+ " 8 <= 16\n",
+ "16 <= 17 < 32\n",
+ " The discrete binary logarithm of is 4\n",
+ "The continuous binary logarithm of is 4.087463\n"
+ ]
}
],
"prompt_number": 14
@@ -565,25 +970,44 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.20 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60. It uses a break statement to\ncontrol the loop:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (True):\n if (i > n):\n break\n s += i\n i += 1\n\nprint \"The sum of the first %d integers is %d\" %(i,s)\n",
+ "input": [
+ "\n",
+ "i=1\n",
+ "print \"Enter a positive integer: \"\n",
+ "n = int(raw_input())\n",
+ "s=0\n",
+ "while (True):\n",
+ " if (i > n):\n",
+ " break\n",
+ " s += i\n",
+ " i += 1\n",
+ "\n",
+ "print \"The sum of the first %d integers is %d\" %(i,s)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter a positive integer: \n"
+ "text": [
+ "Enter a positive integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The sum of the first 11 integers is 55\n"
+ "text": [
+ "The sum of the first 11 integers is 55\n"
+ ]
}
],
"prompt_number": 15
@@ -591,58 +1015,92 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.21 Controlling Input with a Sentinel\nThis program reads a sequence of positive integers, terminated by 0, and prints their average:\n'''\ncount=0\ns=0\nprint \"Enter positive integers (0 to quit):\" \nwhile True: # \"forever\"\n print \"\\t %d :\" %(count + 1),\n n = int(raw_input())\n if (n <= 0):\n break\n count += 1\n s += n\n\nprint \"The average of those %d positive numbers is \" %count,\nprint float(s)/count\n",
+ "input": [
+ "\n",
+ "count=0\n",
+ "s=0\n",
+ "print \"Enter positive integers (0 to quit):\" \n",
+ "while True: # \"forever\"\n",
+ " print \"\\t %d :\" %(count + 1),\n",
+ " n = int(raw_input())\n",
+ " if (n <= 0):\n",
+ " break\n",
+ " count += 1\n",
+ " s += n\n",
+ "\n",
+ "print \"The average of those %d positive numbers is \" %count,\n",
+ "print float(s)/count\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter positive integers (0 to quit):\n\t 1 :"
+ "text": [
+ "Enter positive integers (0 to quit):\n",
+ "\t 1 :"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "12\n"
+ "text": [
+ "12\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " \t 2 :"
+ "text": [
+ " \t 2 :"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "32\n"
+ "text": [
+ "32\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " \t 3 :"
+ "text": [
+ " \t 3 :"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "11\n"
+ "text": [
+ "11\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " \t 4 :"
+ "text": [
+ " \t 4 :"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " The average of those 3 positive numbers is 18.3333333333\n"
+ "text": [
+ " The average of those 3 positive numbers is 18.3333333333\n"
+ ]
}
],
"prompt_number": 16
@@ -650,14 +1108,37 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.22 Using a break Statement with Nested Loops\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n if y>x:\n break\n else:\n print '%4d' %(x*y),\n print ''\n",
+ "input": [
+ "\n",
+ "\n",
+ "for x in range(1,13):\n",
+ " for y in range(1,13):\n",
+ " if y>x:\n",
+ " break\n",
+ " else:\n",
+ " print '%4d' %(x*y),\n",
+ " print ''\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": " 1 \n 2 4 \n 3 6 9 \n 4 8 12 16 \n 5 10 15 20 25 \n 6 12 18 24 30 36 \n 7 14 21 28 35 42 49 \n 8 16 24 32 40 48 56 64 \n 9 18 27 36 45 54 63 72 81 \n 10 20 30 40 50 60 70 80 90 100 \n 11 22 33 44 55 66 77 88 99 110 121 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n"
+ "text": [
+ " 1 \n",
+ " 2 4 \n",
+ " 3 6 9 \n",
+ " 4 8 12 16 \n",
+ " 5 10 15 20 25 \n",
+ " 6 12 18 24 30 36 \n",
+ " 7 14 21 28 35 42 49 \n",
+ " 8 16 24 32 40 48 56 64 \n",
+ " 9 18 27 36 45 54 63 72 81 \n",
+ " 10 20 30 40 50 60 70 80 90 100 \n",
+ " 11 22 33 44 55 66 77 88 99 110 121 \n",
+ " 12 24 36 48 60 72 84 96 108 120 132 144 \n"
+ ]
}
],
"prompt_number": 17
@@ -665,7 +1146,17 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.23 Using continue and break Statements\nThis little program illustrates the continue and break statements:\n'''\nwhile True:\n n = int(raw_input('Enter int : '))\n if (n%2 == 0):\n continue\n if (n%3 == 0):\n break\n print \"\\tBottom of loop.\\n\"\nprint \"\\tOutside of loop.\\n\"\n",
+ "input": [
+ "\n",
+ "while True:\n",
+ " n = int(raw_input('Enter int : '))\n",
+ " if (n%2 == 0):\n",
+ " continue\n",
+ " if (n%3 == 0):\n",
+ " break\n",
+ " print \"\\tBottom of loop.\\n\"\n",
+ "print \"\\tOutside of loop.\\n\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -673,35 +1164,49 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter int : 5\n"
+ "text": [
+ "Enter int : 5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tBottom of loop.\n\n"
+ "text": [
+ "\tBottom of loop.\n",
+ "\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter int : 4\n"
+ "text": [
+ "Enter int : 4\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter int : 6\n"
+ "text": [
+ "Enter int : 6\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter int : 9\n"
+ "text": [
+ "Enter int : 9\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tOutside of loop.\n\n"
+ "text": [
+ "\tOutside of loop.\n",
+ "\n"
+ ]
}
],
"prompt_number": 18
@@ -709,14 +1214,47 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.24 Using a goto Statement to Break Out of a Nest of Loops\nNote : Python has no goto facility.\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n",
+ "input": [
+ "\n",
+ "N=5\n",
+ "done=False\n",
+ "for i in range(N):\n",
+ " for j in range(N):\n",
+ " if done:\n",
+ " break\n",
+ " for k in range(N):\n",
+ " if done:\n",
+ " break\n",
+ " if (i+j+k>N):\n",
+ " done = True\n",
+ " else:\n",
+ " print i+j+k,\n",
+ " print \" \",\n",
+ " print \"* \"\n",
+ " print \".\" \n",
+ " done = False\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n"
+ "text": [
+ "0 1 2 3 4 * \n",
+ "1 2 3 4 5 * \n",
+ "2 3 4 5 * \n",
+ ".\n",
+ "1 2 3 4 5 * \n",
+ "2 3 4 5 * \n",
+ ".\n",
+ "2 3 4 5 * \n",
+ ".\n",
+ "3 4 5 * \n",
+ ".\n",
+ "4 5 * \n",
+ ".\n"
+ ]
}
],
"prompt_number": 19
@@ -724,14 +1262,47 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.25 Using a Flag to Break Out of a Nest of Loops\nThis program has the same output as that in Example 4.24:\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n",
+ "input": [
+ "\n",
+ "N=5\n",
+ "done=False\n",
+ "for i in range(N):\n",
+ " for j in range(N):\n",
+ " if done:\n",
+ " break\n",
+ " for k in range(N):\n",
+ " if done:\n",
+ " break\n",
+ " if (i+j+k>N):\n",
+ " done = True\n",
+ " else:\n",
+ " print i+j+k,\n",
+ " print \" \",\n",
+ " print \"* \"\n",
+ " print \".\" \n",
+ " done = False\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n"
+ "text": [
+ "0 1 2 3 4 * \n",
+ "1 2 3 4 5 * \n",
+ "2 3 4 5 * \n",
+ ".\n",
+ "1 2 3 4 5 * \n",
+ "2 3 4 5 * \n",
+ ".\n",
+ "2 3 4 5 * \n",
+ ".\n",
+ "3 4 5 * \n",
+ ".\n",
+ "4 5 * \n",
+ ".\n"
+ ]
}
],
"prompt_number": 20
@@ -739,14 +1310,33 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.26 Generating Pseudo-Random Numbers\nThis program uses the rand() function to generate pseudo-random numbers:\n'''\nimport random\n\n# prints pseudo-random numbers:\n\nfor i in range(0,8):\n print random.random()\n\n ",
+ "input": [
+ "\n",
+ "import random\n",
+ "\n",
+ "# prints pseudo-random numbers:\n",
+ "\n",
+ "for i in range(0,8):\n",
+ " print random.random()\n",
+ "\n",
+ " "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0.702115758628\n0.969460447904\n0.409934401112\n0.700339443791\n0.093528851602\n0.132172955687\n0.0162887279366\n0.943010713478\n"
+ "text": [
+ "0.702115758628\n",
+ "0.969460447904\n",
+ "0.409934401112\n",
+ "0.700339443791\n",
+ "0.093528851602\n",
+ "0.132172955687\n",
+ "0.0162887279366\n",
+ "0.943010713478\n"
+ ]
}
],
"prompt_number": 21
@@ -754,25 +1344,47 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.27 Setting the Seed Interactively\nThis program is the same as the one in Example 4.26 except that it allows the pseudo-random number\ngenerator's seed to be set interactively:\n'''\nimport random\n# prints pseudo-random numbers:\nprint \"Enter seed: \"\nseed = int(raw_input())\nrandom.seed(seed);\nfor i in range(0,8):\n print random.random()\n",
+ "input": [
+ "\n",
+ "import random\n",
+ "# prints pseudo-random numbers:\n",
+ "print \"Enter seed: \"\n",
+ "seed = int(raw_input())\n",
+ "random.seed(seed);\n",
+ "for i in range(0,8):\n",
+ " print random.random()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter seed: \n"
+ "text": [
+ "Enter seed: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "0.62290169489\n0.741786989261\n0.795193565566\n0.942450283777\n0.73989857474\n0.922324996665\n0.0290052282836\n0.465622654378\n"
+ "text": [
+ "0.62290169489\n",
+ "0.741786989261\n",
+ "0.795193565566\n",
+ "0.942450283777\n",
+ "0.73989857474\n",
+ "0.922324996665\n",
+ "0.0290052282836\n",
+ "0.465622654378\n"
+ ]
}
],
"prompt_number": 22
@@ -780,14 +1392,28 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.28 Setting the Seed from the System Clock\nThis program is the same as the one in Example 4.27 except that it sets the pseudo-random number\ngenerator's seed from the system clock.\n'''\nimport random\nfor i in range(0,8):\n print random.random()\n",
+ "input": [
+ "\n",
+ "import random\n",
+ "for i in range(0,8):\n",
+ " print random.random()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0.943356716998\n0.648974553137\n0.900900491751\n0.113205964653\n0.469069047782\n0.24657283262\n0.543760859236\n0.573941187928\n"
+ "text": [
+ "0.943356716998\n",
+ "0.648974553137\n",
+ "0.900900491751\n",
+ "0.113205964653\n",
+ "0.469069047782\n",
+ "0.24657283262\n",
+ "0.543760859236\n",
+ "0.573941187928\n"
+ ]
}
],
"prompt_number": 23
@@ -795,31 +1421,52 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 4.29 Generating Pseudo-Random Numbers in Given Range\nThis program is the same as the one in Example 4.28 except that the pseudo-random numbers that it\ngenerates are restricted to given range:\n'''\nimport random\nprint \"Enter minimum and maximum: \"\nm = int(raw_input())\nn = int(raw_input())\n# lowest and highest numbers\nr = n - m + 1\n# number of numbers in range\nfor i in range(0,20):\n j = int(random.random()*100 % r + m)\n print j,\n print \" \",\n",
+ "input": [
+ "\n",
+ "import random\n",
+ "print \"Enter minimum and maximum: \"\n",
+ "m = int(raw_input())\n",
+ "n = int(raw_input())\n",
+ "# lowest and highest numbers\n",
+ "r = n - m + 1\n",
+ "# number of numbers in range\n",
+ "for i in range(0,20):\n",
+ " j = int(random.random()*100 % r + m)\n",
+ " print j,\n",
+ " print \" \",\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter minimum and maximum: \n"
+ "text": [
+ "Enter minimum and maximum: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "15\n"
+ "text": [
+ "15\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "6 15 10 8 15 9 7 7 11 6 5 15 14 15 15 15 11 13 14 6 \n"
+ "text": [
+ "6 15 10 8 15 9 7 7 11 6 5 15 14 15 15 15 11 13 14 6 \n"
+ ]
}
],
"prompt_number": 24
@@ -827,7 +1474,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb
index 6ef37c88..a10c8e6e 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch5"
+ "name": "",
+ "signature": "sha256:507ed1502286b7e07eb64a51dd0502249f2763079a23aefef815ad5e65c327ad"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,28 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.1 The Square Root Function sqrt()\n'''\nimport math\n\n# tests the sqrt() function:\nfor i in range(0,6):\n print \"\\t %d \\t %f\" %(i,math.sqrt(i))",
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "# tests the sqrt() function:\n",
+ "for i in range(0,6):\n",
+ " print \"\\t %d \\t %f\" %(i,math.sqrt(i))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "\t 0 \t 0.000000\n\t 1 \t 1.000000\n\t 2 \t 1.414214\n\t 3 \t 1.732051\n\t 4 \t 2.000000\n\t 5 \t 2.236068\n"
+ "text": [
+ "\t 0 \t 0.000000\n",
+ "\t 1 \t 1.000000\n",
+ "\t 2 \t 1.414214\n",
+ "\t 3 \t 1.732051\n",
+ "\t 4 \t 2.000000\n",
+ "\t 5 \t 2.236068\n"
+ ]
}
],
"prompt_number": 1
@@ -25,14 +40,35 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.2 Testing a Trigonometry Identity\n'''\nimport math\n# tests the identity sin 2x = 2 sin x cos x:\nx = 0\nwhile x < 2:\n print \"%f \\t\\t %f \\t %f\" %(x,math.sin(2*x),2*math.sin(x)*math.cos(x))\n x += 0.2\n\n",
+ "input": [
+ "\n",
+ "import math\n",
+ "# tests the identity sin 2x = 2 sin x cos x:\n",
+ "x = 0\n",
+ "while x < 2:\n",
+ " print \"%f \\t\\t %f \\t %f\" %(x,math.sin(2*x),2*math.sin(x)*math.cos(x))\n",
+ " x += 0.2\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0.000000 \t\t 0.000000 \t 0.000000\n0.200000 \t\t 0.389418 \t 0.389418\n0.400000 \t\t 0.717356 \t 0.717356\n0.600000 \t\t 0.932039 \t 0.932039\n0.800000 \t\t 0.999574 \t 0.999574\n1.000000 \t\t 0.909297 \t 0.909297\n1.200000 \t\t 0.675463 \t 0.675463\n1.400000 \t\t 0.334988 \t 0.334988\n1.600000 \t\t -0.058374 \t -0.058374\n1.800000 \t\t -0.442520 \t -0.442520\n2.000000 \t\t -0.756802 \t -0.756802\n"
+ "text": [
+ "0.000000 \t\t 0.000000 \t 0.000000\n",
+ "0.200000 \t\t 0.389418 \t 0.389418\n",
+ "0.400000 \t\t 0.717356 \t 0.717356\n",
+ "0.600000 \t\t 0.932039 \t 0.932039\n",
+ "0.800000 \t\t 0.999574 \t 0.999574\n",
+ "1.000000 \t\t 0.909297 \t 0.909297\n",
+ "1.200000 \t\t 0.675463 \t 0.675463\n",
+ "1.400000 \t\t 0.334988 \t 0.334988\n",
+ "1.600000 \t\t -0.058374 \t -0.058374\n",
+ "1.800000 \t\t -0.442520 \t -0.442520\n",
+ "2.000000 \t\t -0.756802 \t -0.756802\n"
+ ]
}
],
"prompt_number": 2
@@ -40,7 +76,14 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.3 A cube() Function\nHere is a simple example of a user-defined function:\n'''\n\ndef cube(x):\n # returns cube of x:\n return x*x*x\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "def cube(x):\n",
+ " # returns cube of x:\n",
+ " return x*x*x\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -49,7 +92,19 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.4 A Test Driver for the cube() Function\nHere is a complete program that includes the definition of the cube() function from Example 5.4\ntogether with a test driver for it:\n'''\n\ndef cube(x):\n # returns cube of x:\n return x*x*x\n\n# tests the cube() function:\nn=1\nwhile (n != 0):\n n = int(raw_input())\n print \"\\tcube( %d ) = %d\" %(n,cube(n))",
+ "input": [
+ "\n",
+ "\n",
+ "def cube(x):\n",
+ " # returns cube of x:\n",
+ " return x*x*x\n",
+ "\n",
+ "# tests the cube() function:\n",
+ "n=1\n",
+ "while (n != 0):\n",
+ " n = int(raw_input())\n",
+ " print \"\\tcube( %d ) = %d\" %(n,cube(n))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -57,45 +112,61 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4\n"
+ "text": [
+ "4\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tcube( 4 ) = 64\n"
+ "text": [
+ "\tcube( 4 ) = 64\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2\n"
+ "text": [
+ "2\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tcube( 2 ) = 8\n"
+ "text": [
+ "\tcube( 2 ) = 8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tcube( 9 ) = 729\n"
+ "text": [
+ "\tcube( 9 ) = 729\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tcube( 0 ) = 0\n"
+ "text": [
+ "\tcube( 0 ) = 0\n"
+ ]
}
],
"prompt_number": 1
@@ -103,7 +174,24 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.5 A Test Driver for the max() Function\nHere is a function with two parameters. It returns the larger of the two values passed to it.\n'''\n\ndef maximum(x,y):\n # returns larger of the two given integers:\n if (x < y):\n return y\n else:\n return x\n\n# tests the max() function:\nm = 1\nn = 1\nwhile m != 0: \n m = int(raw_input())\n n = int(raw_input())\n print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n\n",
+ "input": [
+ "\n",
+ "def maximum(x,y):\n",
+ " # returns larger of the two given integers:\n",
+ " if (x < y):\n",
+ " return y\n",
+ " else:\n",
+ " return x\n",
+ "\n",
+ "# tests the max() function:\n",
+ "m = 1\n",
+ "n = 1\n",
+ "while m != 0: \n",
+ " m = int(raw_input())\n",
+ " n = int(raw_input())\n",
+ " print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -111,35 +199,47 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2\n"
+ "text": [
+ "2\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax( 5 , 2 ) = 5\n"
+ "text": [
+ "\tmax( 5 , 2 ) = 5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "3\n"
+ "text": [
+ "3\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax( 0 , 3 ) = 3\n"
+ "text": [
+ "\tmax( 0 , 3 ) = 3\n"
+ ]
}
],
"prompt_number": 2
@@ -147,7 +247,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.6 The max() Function with Declaration Separate from Definition\n'''\n\ndef maximum(x,y):\n # returns larger of the two given integers:\n if (x < y):\n return y\n else:\n return x\n\n# tests the max() function:\nm = 1\nn = 1\nwhile m != 0: \n m = int(raw_input())\n n = int(raw_input())\n print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "def maximum(x,y):\n",
+ " # returns larger of the two given integers:\n",
+ " if (x < y):\n",
+ " return y\n",
+ " else:\n",
+ " return x\n",
+ "\n",
+ "# tests the max() function:\n",
+ "m = 1\n",
+ "n = 1\n",
+ "while m != 0: \n",
+ " m = int(raw_input())\n",
+ " n = int(raw_input())\n",
+ " print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -155,35 +273,47 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2\n"
+ "text": [
+ "2\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax( 5 , 2 ) = 5\n"
+ "text": [
+ "\tmax( 5 , 2 ) = 5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "3\n"
+ "text": [
+ "3\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax( 0 , 3 ) = 3\n"
+ "text": [
+ "\tmax( 0 , 3 ) = 3\n"
+ ]
}
],
"prompt_number": 3
@@ -191,7 +321,18 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.8 The max() Function Compiled Separately\n'''\n\n\n# returns larger of the two given integers:\n\nm = 1\nn = 1\nwhile m!=0:\n m = int(raw_input())\n n = int(raw_input())\n print \"\\tmax(%d,%d) = %d\" %(m,n, max(m,n))\n",
+ "input": [
+ "\n",
+ "\n",
+ "# returns larger of the two given integers:\n",
+ "\n",
+ "m = 1\n",
+ "n = 1\n",
+ "while m!=0:\n",
+ " m = int(raw_input())\n",
+ " n = int(raw_input())\n",
+ " print \"\\tmax(%d,%d) = %d\" %(m,n, max(m,n))\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -199,69 +340,93 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4\n"
+ "text": [
+ "4\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax(5,4) = 5\n"
+ "text": [
+ "\tmax(5,4) = 5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4\n"
+ "text": [
+ "4\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "3\n"
+ "text": [
+ "3\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax(4,3) = 4\n"
+ "text": [
+ "\tmax(4,3) = 4\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax(8,0) = 8\n"
+ "text": [
+ "\tmax(8,0) = 8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\tmax(0,5) = 5\n"
+ "text": [
+ "\tmax(0,5) = 5\n"
+ ]
}
],
"prompt_number": 4
@@ -269,14 +434,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.9 The Factorial Function\n'''\n\ndef fact(n):\n if (n < 0):\n return 0\n f = 1\n while (n > 1):\n f *= n\n n -= 1\n return f\n\nfor i in range(-1,6):\n print fact(i),\n",
+ "input": [
+ "\n",
+ "\n",
+ "def fact(n):\n",
+ " if (n < 0):\n",
+ " return 0\n",
+ " f = 1\n",
+ " while (n > 1):\n",
+ " f *= n\n",
+ " n -= 1\n",
+ " return f\n",
+ "\n",
+ "for i in range(-1,6):\n",
+ " print fact(i),\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0 1 1 2 6 24 120\n"
+ "text": [
+ "0 1 1 2 6 24 120\n"
+ ]
}
],
"prompt_number": 5
@@ -284,14 +465,46 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.10 The Permutation Function\n'''\ndef fact(n):\n if (n < 0):\n return 0\n f = 1\n while (n > 1):\n f *= n\n n -= 1\n return f\n\n\ndef perm(n,k):\n # returns P(n,k), the number of permutations of k from n:\n if (n < 0 or k < 0 or k > n):\n return 0\n return fact(n)/fact(n-k)\n\nfor i in range(-1,8):\n for j in range(-1,i+2):\n print perm(i,j),\n print ''\n",
+ "input": [
+ "\n",
+ "def fact(n):\n",
+ " if (n < 0):\n",
+ " return 0\n",
+ " f = 1\n",
+ " while (n > 1):\n",
+ " f *= n\n",
+ " n -= 1\n",
+ " return f\n",
+ "\n",
+ "\n",
+ "def perm(n,k):\n",
+ " # returns P(n,k), the number of permutations of k from n:\n",
+ " if (n < 0 or k < 0 or k > n):\n",
+ " return 0\n",
+ " return fact(n)/fact(n-k)\n",
+ "\n",
+ "for i in range(-1,8):\n",
+ " for j in range(-1,i+2):\n",
+ " print perm(i,j),\n",
+ " print ''\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "0 0 \n0 1 0 \n0 1 1 0 \n0 1 2 2 0 \n0 1 3 6 6 0 \n0 1 4 12 24 24 0 \n0 1 5 20 60 120 120 0 \n0 1 6 30 120 360 720 720 0 \n0 1 7 42 210 840 2520 5040 5040 0 \n"
+ "text": [
+ "0 0 \n",
+ "0 1 0 \n",
+ "0 1 1 0 \n",
+ "0 1 2 2 0 \n",
+ "0 1 3 6 6 0 \n",
+ "0 1 4 12 24 24 0 \n",
+ "0 1 5 20 60 120 120 0 \n",
+ "0 1 6 30 120 360 720 720 0 \n",
+ "0 1 7 42 210 840 2520 5040 5040 0 \n"
+ ]
}
],
"prompt_number": 6
@@ -299,7 +512,47 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.11 A Function that Prints Dates\n'''\n\ndef printDate(m,d,y):\n # prints the given date in literal form:\n if (m < 1 or m > 12 or d < 1 or d > 31 or y < 0):\n print \"Error: parameter out of range.\\n\"\n return\n if m == 1:\n print \"January \",\n elif m ==2:\n print \"February \",\n elif m==3 :\n print \"March \",\n elif m==4:\n print \"April \",\n elif m==5:\n print \"May \",\n elif m==6:\n print \"June \",\n elif m==7:\n print \"July \",\n elif m==8:\n print \"August \",\n elif m==9:\n print \"September \",\n elif m==10:\n print \"October \",\n elif m==1:\n print \"November \",\n else:\n print \"December \",\n print d , \", \", y \n\n# tests the printDate() function:\nmonth = 1\nwhile month > 0:\n month = int(raw_input())\n day = int(raw_input())\n year = int(raw_input())\n printDate(month,day,year)\n",
+ "input": [
+ "\n",
+ "def printDate(m,d,y):\n",
+ " # prints the given date in literal form:\n",
+ " if (m < 1 or m > 12 or d < 1 or d > 31 or y < 0):\n",
+ " print \"Error: parameter out of range.\\n\"\n",
+ " return\n",
+ " if m == 1:\n",
+ " print \"January \",\n",
+ " elif m ==2:\n",
+ " print \"February \",\n",
+ " elif m==3 :\n",
+ " print \"March \",\n",
+ " elif m==4:\n",
+ " print \"April \",\n",
+ " elif m==5:\n",
+ " print \"May \",\n",
+ " elif m==6:\n",
+ " print \"June \",\n",
+ " elif m==7:\n",
+ " print \"July \",\n",
+ " elif m==8:\n",
+ " print \"August \",\n",
+ " elif m==9:\n",
+ " print \"September \",\n",
+ " elif m==10:\n",
+ " print \"October \",\n",
+ " elif m==1:\n",
+ " print \"November \",\n",
+ " else:\n",
+ " print \"December \",\n",
+ " print d , \", \", y \n",
+ "\n",
+ "# tests the printDate() function:\n",
+ "month = 1\n",
+ "while month > 0:\n",
+ " month = int(raw_input())\n",
+ " day = int(raw_input())\n",
+ " year = int(raw_input())\n",
+ " printDate(month,day,year)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -307,47 +560,64 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "12\n"
+ "text": [
+ "12\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1989\n"
+ "text": [
+ "1989\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "September 12 , 1989\n"
+ "text": [
+ "September 12 , 1989\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2001\n"
+ "text": [
+ "2001\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Error: parameter out of range.\n\n"
+ "text": [
+ "Error: parameter out of range.\n",
+ "\n"
+ ]
}
],
"prompt_number": 8
@@ -355,14 +625,305 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.12 Classifying Characters\n'''\nimport string\ndef ispunct(s):\n return all(c in string.punctuation for c in s)\ndef printCharCategory(c):\n # prints the category to which the given character belongs:\n print \"The character [\" + c + \"] is a \",\n if(c.isdigit()):\n print \"digit.\\n\"\n elif (c.islower()):\n print \"lower-case letter.\\n\"\n elif (c.isupper()): \n print \"capital letter.\\n\"\n elif (c.isspace()):\n print \"white space character.\\n\"\n elif (ord(c) >= 10 and ord(c) <= 15 or ord(c) == 0):\n print \"control character.\\n\"\n elif (ispunct(c)):\n print \"punctuation mark.\\n\"\n else:\n print \"Error.\\n\"\n\n# prints the category to which the given character belongs;\n# tests the printCharCategory() function:\nfor c in range(128):\n printCharCategory(chr(c))\n",
+ "input": [
+ "\n",
+ "import string\n",
+ "def ispunct(s):\n",
+ " return all(c in string.punctuation for c in s)\n",
+ "def printCharCategory(c):\n",
+ " # prints the category to which the given character belongs:\n",
+ " print \"The character [\" + c + \"] is a \",\n",
+ " if(c.isdigit()):\n",
+ " print \"digit.\\n\"\n",
+ " elif (c.islower()):\n",
+ " print \"lower-case letter.\\n\"\n",
+ " elif (c.isupper()): \n",
+ " print \"capital letter.\\n\"\n",
+ " elif (c.isspace()):\n",
+ " print \"white space character.\\n\"\n",
+ " elif (ord(c) >= 10 and ord(c) <= 15 or ord(c) == 0):\n",
+ " print \"control character.\\n\"\n",
+ " elif (ispunct(c)):\n",
+ " print \"punctuation mark.\\n\"\n",
+ " else:\n",
+ " print \"Error.\\n\"\n",
+ "\n",
+ "# prints the category to which the given character belongs;\n",
+ "# tests the printCharCategory() function:\n",
+ "for c in range(128):\n",
+ " printCharCategory(chr(c))\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": " The character [\u0000] is a control character.\n\nThe character [\u0001] is a Error.\n\nThe character [\u0002] is a Error.\n\nThe character [\u0003] is a Error.\n\nThe character [\u0004] is a Error.\n\nThe character [\u0005] is a Error.\n\nThe character [\u0006] is a Error.\n\nThe character [\u0007] is a Error.\n\nThe character [\b] is a Error.\n\nThe character [\t] is a white space character.\n\nThe character [\n] is a white space character.\n\nThe character [\u000b] is a white space character.\n\nThe character [\f] is a white space character.\n\nThe character [\r] is a white space character.\n\nThe character [\u000e] is a control character.\n\nThe character [\u000f] is a control character.\n\nThe character [\u0010] is a Error.\n\nThe character [\u0011] is a Error.\n\nThe character [\u0012] is a Error.\n\nThe character [\u0013] is a Error.\n\nThe character [\u0014] is a Error.\n\nThe character [\u0015] is a Error.\n\nThe character [\u0016] is a Error.\n\nThe character [\u0017] is a Error.\n\nThe character [\u0018] is a Error.\n\nThe character [\u0019] is a Error.\n\nThe character [\u001a] is a Error.\n\nThe character [\u001b] is a Error.\n\nThe character [\u001c] is a Error.\n\nThe character [\u001d] is a Error.\n\nThe character [\u001e] is a Error.\n\nThe character [\u001f] is a Error.\n\nThe character [ ] is a white space character.\n\nThe character [!] is a punctuation mark.\n\nThe character [\"] is a punctuation mark.\n\nThe character [#] is a punctuation mark.\n\nThe character [$] is a punctuation mark.\n\nThe character [%] is a punctuation mark.\n\nThe character [&] is a punctuation mark.\n\nThe character ['] is a punctuation mark.\n\nThe character [(] is a punctuation mark.\n\nThe character [)] is a punctuation mark.\n\nThe character [*] is a punctuation mark.\n\nThe character [+] is a punctuation mark.\n\nThe character [,] is a punctuation mark.\n\nThe character [-] is a punctuation mark.\n\nThe character [.] is a punctuation mark.\n\nThe character [/] is a punctuation mark.\n\nThe character [0] is a digit.\n\nThe character [1] is a digit.\n\nThe character [2] is a digit.\n\nThe character [3] is a digit.\n\nThe character [4] is a digit.\n\nThe character [5] is a digit.\n\nThe character [6] is a digit.\n\nThe character [7] is a digit.\n\nThe character [8] is a digit.\n\nThe character [9] is a digit.\n\nThe character [:] is a punctuation mark.\n\nThe character [;] is a punctuation mark.\n\nThe character [<] is a punctuation mark.\n\nThe character [=] is a punctuation mark.\n\nThe character [>] is a punctuation mark.\n\nThe character [?] is a punctuation mark.\n\nThe character [@] is a punctuation mark.\n\nThe character [A] is a capital letter.\n\nThe character [B] is a capital letter.\n\nThe character [C] is a capital letter.\n\nThe character [D] is a capital letter.\n\nThe character [E] is a capital letter.\n\nThe character [F] is a capital letter.\n\nThe character [G] is a capital letter.\n\nThe character [H] is a capital letter.\n\nThe character [I] is a capital letter.\n\nThe character [J] is a capital letter.\n\nThe character [K] is a capital letter.\n\nThe character [L] is a capital letter.\n\nThe character [M] is a capital letter.\n\nThe character [N] is a capital letter.\n\nThe character [O] is a capital letter.\n\nThe character [P] is a capital letter.\n\nThe character [Q] is a capital letter.\n\nThe character [R] is a capital letter.\n\nThe character [S] is a capital letter.\n\nThe character [T] is a capital letter.\n\nThe character [U] is a capital letter.\n\nThe character [V] is a capital letter.\n\nThe character [W] is a capital letter.\n\nThe character [X] is a capital letter.\n\nThe character [Y] is a capital letter.\n\nThe character [Z] is a capital letter.\n\nThe character [[] is a punctuation mark.\n\nThe character [\\] is a punctuation mark.\n\nThe character []] is a punctuation mark.\n\nThe character [^] is a punctuation mark.\n\nThe character [_] is a punctuation mark.\n\nThe character [`] is a punctuation mark.\n\nThe character [a] is a lower-case letter.\n\nThe character [b] is a lower-case letter.\n\nThe character [c] is a lower-case letter.\n\nThe character [d] is a lower-case letter.\n\nThe character [e] is a lower-case letter.\n\nThe character [f] is a lower-case letter.\n\nThe character [g] is a lower-case letter.\n\nThe character [h] is a lower-case letter.\n\nThe character [i] is a lower-case letter.\n\nThe character [j] is a lower-case letter.\n\nThe character [k] is a lower-case letter.\n\nThe character [l] is a lower-case letter.\n\nThe character [m] is a lower-case letter.\n\nThe character [n] is a lower-case letter.\n\nThe character [o] is a lower-case letter.\n\nThe character [p] is a lower-case letter.\n\nThe character [q] is a lower-case letter.\n\nThe character [r] is a lower-case letter.\n\nThe character [s] is a lower-case letter.\n\nThe character [t] is a lower-case letter.\n\nThe character [u] is a lower-case letter.\n\nThe character [v] is a lower-case letter.\n\nThe character [w] is a lower-case letter.\n\nThe character [x] is a lower-case letter.\n\nThe character [y] is a lower-case letter.\n\nThe character [z] is a lower-case letter.\n\nThe character [{] is a punctuation mark.\n\nThe character [|] is a punctuation mark.\n\nThe character [}] is a punctuation mark.\n\nThe character [~] is a punctuation mark.\n\nThe character [\u007f] is a Error.\n\n"
+ "text": [
+ " The character [\u0000] is a control character.\n",
+ "\n",
+ "The character [\u0001] is a Error.\n",
+ "\n",
+ "The character [\u0002] is a Error.\n",
+ "\n",
+ "The character [\u0003] is a Error.\n",
+ "\n",
+ "The character [\u0004] is a Error.\n",
+ "\n",
+ "The character [\u0005] is a Error.\n",
+ "\n",
+ "The character [\u0006] is a Error.\n",
+ "\n",
+ "The character [\u0007] is a Error.\n",
+ "\n",
+ "The character [\b] is a Error.\n",
+ "\n",
+ "The character [\t] is a white space character.\n",
+ "\n",
+ "The character [\n",
+ "] is a white space character.\n",
+ "\n",
+ "The character [\u000b",
+ "] is a white space character.\n",
+ "\n",
+ "The character [\f",
+ "] is a white space character.\n",
+ "\n",
+ "The character [\r",
+ "] is a white space character.\n",
+ "\n",
+ "The character [\u000e] is a control character.\n",
+ "\n",
+ "The character [\u000f] is a control character.\n",
+ "\n",
+ "The character [\u0010] is a Error.\n",
+ "\n",
+ "The character [\u0011] is a Error.\n",
+ "\n",
+ "The character [\u0012] is a Error.\n",
+ "\n",
+ "The character [\u0013] is a Error.\n",
+ "\n",
+ "The character [\u0014] is a Error.\n",
+ "\n",
+ "The character [\u0015] is a Error.\n",
+ "\n",
+ "The character [\u0016] is a Error.\n",
+ "\n",
+ "The character [\u0017] is a Error.\n",
+ "\n",
+ "The character [\u0018] is a Error.\n",
+ "\n",
+ "The character [\u0019] is a Error.\n",
+ "\n",
+ "The character [\u001a] is a Error.\n",
+ "\n",
+ "The character [\u001b] is a Error.\n",
+ "\n",
+ "The character [\u001c",
+ "] is a Error.\n",
+ "\n",
+ "The character [\u001d",
+ "] is a Error.\n",
+ "\n",
+ "The character [\u001e",
+ "] is a Error.\n",
+ "\n",
+ "The character [\u001f] is a Error.\n",
+ "\n",
+ "The character [ ] is a white space character.\n",
+ "\n",
+ "The character [!] is a punctuation mark.\n",
+ "\n",
+ "The character [\"] is a punctuation mark.\n",
+ "\n",
+ "The character [#] is a punctuation mark.\n",
+ "\n",
+ "The character [$] is a punctuation mark.\n",
+ "\n",
+ "The character [%] is a punctuation mark.\n",
+ "\n",
+ "The character [&] is a punctuation mark.\n",
+ "\n",
+ "The character ['] is a punctuation mark.\n",
+ "\n",
+ "The character [(] is a punctuation mark.\n",
+ "\n",
+ "The character [)] is a punctuation mark.\n",
+ "\n",
+ "The character [*] is a punctuation mark.\n",
+ "\n",
+ "The character [+] is a punctuation mark.\n",
+ "\n",
+ "The character [,] is a punctuation mark.\n",
+ "\n",
+ "The character [-] is a punctuation mark.\n",
+ "\n",
+ "The character [.] is a punctuation mark.\n",
+ "\n",
+ "The character [/] is a punctuation mark.\n",
+ "\n",
+ "The character [0] is a digit.\n",
+ "\n",
+ "The character [1] is a digit.\n",
+ "\n",
+ "The character [2] is a digit.\n",
+ "\n",
+ "The character [3] is a digit.\n",
+ "\n",
+ "The character [4] is a digit.\n",
+ "\n",
+ "The character [5] is a digit.\n",
+ "\n",
+ "The character [6] is a digit.\n",
+ "\n",
+ "The character [7] is a digit.\n",
+ "\n",
+ "The character [8] is a digit.\n",
+ "\n",
+ "The character [9] is a digit.\n",
+ "\n",
+ "The character [:] is a punctuation mark.\n",
+ "\n",
+ "The character [;] is a punctuation mark.\n",
+ "\n",
+ "The character [<] is a punctuation mark.\n",
+ "\n",
+ "The character [=] is a punctuation mark.\n",
+ "\n",
+ "The character [>] is a punctuation mark.\n",
+ "\n",
+ "The character [?] is a punctuation mark.\n",
+ "\n",
+ "The character [@] is a punctuation mark.\n",
+ "\n",
+ "The character [A] is a capital letter.\n",
+ "\n",
+ "The character [B] is a capital letter.\n",
+ "\n",
+ "The character [C] is a capital letter.\n",
+ "\n",
+ "The character [D] is a capital letter.\n",
+ "\n",
+ "The character [E] is a capital letter.\n",
+ "\n",
+ "The character [F] is a capital letter.\n",
+ "\n",
+ "The character [G] is a capital letter.\n",
+ "\n",
+ "The character [H] is a capital letter.\n",
+ "\n",
+ "The character [I] is a capital letter.\n",
+ "\n",
+ "The character [J] is a capital letter.\n",
+ "\n",
+ "The character [K] is a capital letter.\n",
+ "\n",
+ "The character [L] is a capital letter.\n",
+ "\n",
+ "The character [M] is a capital letter.\n",
+ "\n",
+ "The character [N] is a capital letter.\n",
+ "\n",
+ "The character [O] is a capital letter.\n",
+ "\n",
+ "The character [P] is a capital letter.\n",
+ "\n",
+ "The character [Q] is a capital letter.\n",
+ "\n",
+ "The character [R] is a capital letter.\n",
+ "\n",
+ "The character [S] is a capital letter.\n",
+ "\n",
+ "The character [T] is a capital letter.\n",
+ "\n",
+ "The character [U] is a capital letter.\n",
+ "\n",
+ "The character [V] is a capital letter.\n",
+ "\n",
+ "The character [W] is a capital letter.\n",
+ "\n",
+ "The character [X] is a capital letter.\n",
+ "\n",
+ "The character [Y] is a capital letter.\n",
+ "\n",
+ "The character [Z] is a capital letter.\n",
+ "\n",
+ "The character [[] is a punctuation mark.\n",
+ "\n",
+ "The character [\\] is a punctuation mark.\n",
+ "\n",
+ "The character []] is a punctuation mark.\n",
+ "\n",
+ "The character [^] is a punctuation mark.\n",
+ "\n",
+ "The character [_] is a punctuation mark.\n",
+ "\n",
+ "The character [`] is a punctuation mark.\n",
+ "\n",
+ "The character [a] is a lower-case letter.\n",
+ "\n",
+ "The character [b] is a lower-case letter.\n",
+ "\n",
+ "The character [c] is a lower-case letter.\n",
+ "\n",
+ "The character [d] is a lower-case letter.\n",
+ "\n",
+ "The character [e] is a lower-case letter.\n",
+ "\n",
+ "The character [f] is a lower-case letter.\n",
+ "\n",
+ "The character [g] is a lower-case letter.\n",
+ "\n",
+ "The character [h] is a lower-case letter.\n",
+ "\n",
+ "The character [i] is a lower-case letter.\n",
+ "\n",
+ "The character [j] is a lower-case letter.\n",
+ "\n",
+ "The character [k] is a lower-case letter.\n",
+ "\n",
+ "The character [l] is a lower-case letter.\n",
+ "\n",
+ "The character [m] is a lower-case letter.\n",
+ "\n",
+ "The character [n] is a lower-case letter.\n",
+ "\n",
+ "The character [o] is a lower-case letter.\n",
+ "\n",
+ "The character [p] is a lower-case letter.\n",
+ "\n",
+ "The character [q] is a lower-case letter.\n",
+ "\n",
+ "The character [r] is a lower-case letter.\n",
+ "\n",
+ "The character [s] is a lower-case letter.\n",
+ "\n",
+ "The character [t] is a lower-case letter.\n",
+ "\n",
+ "The character [u] is a lower-case letter.\n",
+ "\n",
+ "The character [v] is a lower-case letter.\n",
+ "\n",
+ "The character [w] is a lower-case letter.\n",
+ "\n",
+ "The character [x] is a lower-case letter.\n",
+ "\n",
+ "The character [y] is a lower-case letter.\n",
+ "\n",
+ "The character [z] is a lower-case letter.\n",
+ "\n",
+ "The character [{] is a punctuation mark.\n",
+ "\n",
+ "The character [|] is a punctuation mark.\n",
+ "\n",
+ "The character [}] is a punctuation mark.\n",
+ "\n",
+ "The character [~] is a punctuation mark.\n",
+ "\n",
+ "The character [\u007f] is a Error.\n",
+ "\n"
+ ]
}
],
"prompt_number": 4
@@ -370,14 +931,40 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.13 A Function that Tests Primality\n'''\nimport math\ndef isPrime(n):\n # returns True if n is prime, False otherwise:\n sqrtn = math.sqrt(n)\n if (n < 2):\n return False\n # 0 and 1 are not primes\n if (n < 4):\n return True\n # 2 and 3 are the first primes\n if (n%2 == 0):\n return False\n # 2 is the only even prime\n for d in range(3,int(sqrtn+1),2):\n if (n%d == 0):\n return False\n # n has a nontrivial divisor\n return True;\n\nfor n in range(0,80):\n if (isPrime(n)):\n print n,\n",
+ "input": [
+ "\n",
+ "import math\n",
+ "def isPrime(n):\n",
+ " # returns True if n is prime, False otherwise:\n",
+ " sqrtn = math.sqrt(n)\n",
+ " if (n < 2):\n",
+ " return False\n",
+ " # 0 and 1 are not primes\n",
+ " if (n < 4):\n",
+ " return True\n",
+ " # 2 and 3 are the first primes\n",
+ " if (n%2 == 0):\n",
+ " return False\n",
+ " # 2 is the only even prime\n",
+ " for d in range(3,int(sqrtn+1),2):\n",
+ " if (n%d == 0):\n",
+ " return False\n",
+ " # n has a nontrivial divisor\n",
+ " return True;\n",
+ "\n",
+ "for n in range(0,80):\n",
+ " if (isPrime(n)):\n",
+ " print n,\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79\n"
+ "text": [
+ "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79\n"
+ ]
}
],
"prompt_number": 10
@@ -385,7 +972,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.14 A Leap Year Function\n'''\ndef isLeapYear(y):\n # returns true iff y is a leap year:\n return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)\n\n# tests the isLeapYear() function:\nn = 2\nwhile n > 1:\n n = int(raw_input())\n if (isLeapYear(n)):\n print \"%d is a leap year.\" % n\n else:\n print \"%d is not a leap year.\" %n\n",
+ "input": [
+ "\n",
+ "def isLeapYear(y):\n",
+ " # returns true iff y is a leap year:\n",
+ " return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)\n",
+ "\n",
+ "# tests the isLeapYear() function:\n",
+ "n = 2\n",
+ "while n > 1:\n",
+ " n = int(raw_input())\n",
+ " if (isLeapYear(n)):\n",
+ " print \"%d is a leap year.\" % n\n",
+ " else:\n",
+ " print \"%d is not a leap year.\" %n\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -393,45 +994,61 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2004\n"
+ "text": [
+ "2004\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "2004 is a leap year.\n"
+ "text": [
+ "2004 is a leap year.\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2006\n"
+ "text": [
+ "2006\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "2006 is not a leap year.\n"
+ "text": [
+ "2006 is not a leap year.\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2013\n"
+ "text": [
+ "2013\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "2013 is not a leap year.\n"
+ "text": [
+ "2013 is not a leap year.\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "0 is a leap year.\n"
+ "text": [
+ "0 is a leap year.\n"
+ ]
}
],
"prompt_number": 11
@@ -439,47 +1056,88 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.15 A Function for Reading the User's Age\n'''\n\ndef age():\n # prompts the user to input his/her age, and returns that value:\n while (True):\n print \"How old are you: \"\n n = int(raw_input())\n if (n < 0):\n print \"\\a\\tYour age could not be negative.\"\n elif (n > 120):\n print \"\\a\\tYou could not be over 120.\"\n else:\n return n\n print \"\\n\\tTry again.\\n\"\n\na = age();\nprint \"\\nYou are %d years old.\" %a\n",
+ "input": [
+ "\n",
+ "\n",
+ "def age():\n",
+ " # prompts the user to input his/her age, and returns that value:\n",
+ " while (True):\n",
+ " print \"How old are you: \"\n",
+ " n = int(raw_input())\n",
+ " if (n < 0):\n",
+ " print \"\\a\\tYour age could not be negative.\"\n",
+ " elif (n > 120):\n",
+ " print \"\\a\\tYou could not be over 120.\"\n",
+ " else:\n",
+ " return n\n",
+ " print \"\\n\\tTry again.\\n\"\n",
+ "\n",
+ "a = age();\n",
+ "print \"\\nYou are %d years old.\" %a\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "How old are you: \n"
+ "text": [
+ "How old are you: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "-12\n"
+ "text": [
+ "-12\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\u0007\tYour age could not be negative.\n\n\tTry again.\n\nHow old are you: \n"
+ "text": [
+ "\u0007\tYour age could not be negative.\n",
+ "\n",
+ "\tTry again.\n",
+ "\n",
+ "How old are you: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "125\n"
+ "text": [
+ "125\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\u0007\tYou could not be over 120.\n\n\tTry again.\n\nHow old are you: \n"
+ "text": [
+ "\u0007\tYou could not be over 120.\n",
+ "\n",
+ "\tTry again.\n",
+ "\n",
+ "How old are you: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "24\n"
+ "text": [
+ "24\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\nYou are 24 years old.\n"
+ "text": [
+ "\n",
+ "You are 24 years old.\n"
+ ]
}
],
"prompt_number": 14
@@ -487,14 +1145,29 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.16 The swap() Function\n'''\n\ndef swap(x,y):\n # exchanges the values of x and y:\n x[0],y[0] = y[0],x[0]\n\na = [22.2]\nb = [44.4]\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\nswap(a,b)\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\n",
+ "input": [
+ "\n",
+ "\n",
+ "def swap(x,y):\n",
+ " # exchanges the values of x and y:\n",
+ " x[0],y[0] = y[0],x[0]\n",
+ "\n",
+ "a = [22.2]\n",
+ "b = [44.4]\n",
+ "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n",
+ "swap(a,b)\n",
+ "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "a = 22.20 , b = 44.40 \na = 44.40 , b = 22.20 \n"
+ "text": [
+ "a = 22.20 , b = 44.40 \n",
+ "a = 44.40 , b = 22.20 \n"
+ ]
}
],
"prompt_number": 15
@@ -502,14 +1175,33 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.17 \nNote : Python doesn't support pass value by reference. but can be done by passing list.\n'''\n\ndef f(x,y):\n x[0]= 88\n y[0] = 99\n\n# tests the f() function:\na = [22]\nb = [44]\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\nf(a,b)\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\nf(2*a,b)\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\n",
+ "input": [
+ "\n",
+ "\n",
+ "def f(x,y):\n",
+ " x[0]= 88\n",
+ " y[0] = 99\n",
+ "\n",
+ "# tests the f() function:\n",
+ "a = [22]\n",
+ "b = [44]\n",
+ "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n",
+ "f(a,b)\n",
+ "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n",
+ "f(2*a,b)\n",
+ "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "a = 22.00 , b = 44.00 \na = 88.00 , b = 99.00 \na = 88.00 , b = 99.00 \n"
+ "text": [
+ "a = 22.00 , b = 44.00 \n",
+ "a = 88.00 , b = 99.00 \n",
+ "a = 88.00 , b = 99.00 \n"
+ ]
}
],
"prompt_number": 16
@@ -517,25 +1209,46 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.18 Returning More than One Value\n'''\n\ndef computeCircle(r):\n # returns the area and circumference of a circle with radius r:\n PI = 3.141592653589793\n area = PI*r*r\n circumference = 2*PI*r\n return area,circumference\n\n# tests the computeCircle() function:\nprint \"Enter radius: \"\nr = int(raw_input())\na,c = computeCircle(r)\nprint \"area = %.2f , circumference = %.2f\" %(a,c)",
+ "input": [
+ "\n",
+ "\n",
+ "def computeCircle(r):\n",
+ " # returns the area and circumference of a circle with radius r:\n",
+ " PI = 3.141592653589793\n",
+ " area = PI*r*r\n",
+ " circumference = 2*PI*r\n",
+ " return area,circumference\n",
+ "\n",
+ "# tests the computeCircle() function:\n",
+ "print \"Enter radius: \"\n",
+ "r = int(raw_input())\n",
+ "a,c = computeCircle(r)\n",
+ "print \"area = %.2f , circumference = %.2f\" %(a,c)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter radius: \n"
+ "text": [
+ "Enter radius: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "area = 78.54 , circumference = 31.42\n"
+ "text": [
+ "area = 78.54 , circumference = 31.42\n"
+ ]
}
],
"prompt_number": 17
@@ -543,14 +1256,38 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.19\nNote : Python passes variable by value and not by reference. So output would be differ.\n'''\n\n\ndef f(x,y,z):\n x[0] += z[0]\n y[0] += z[0]\n print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n\nx = [22]\ny = [33]\nz = [44]\n\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\nf(x,y,z)\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\nx[0] = 2*x[0] - 3\nf(x,y,z)\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n",
+ "input": [
+ "\n",
+ "\n",
+ "def f(x,y,z):\n",
+ " x[0] += z[0]\n",
+ " y[0] += z[0]\n",
+ " print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n",
+ "\n",
+ "x = [22]\n",
+ "y = [33]\n",
+ "z = [44]\n",
+ "\n",
+ "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n",
+ "f(x,y,z)\n",
+ "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n",
+ "x[0] = 2*x[0] - 3\n",
+ "f(x,y,z)\n",
+ "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 22 , y = 33 , z = 44\nx = 66 , y = 77 , z = 44\nx = 66 , y = 77 , z = 44\nx = 173 , y = 121 , z = 44\nx = 173 , y = 121 , z = 44\n"
+ "text": [
+ "x = 22 , y = 33 , z = 44\n",
+ "x = 66 , y = 77 , z = 44\n",
+ "x = 66 , y = 77 , z = 44\n",
+ "x = 173 , y = 121 , z = 44\n",
+ "x = 173 , y = 121 , z = 44\n"
+ ]
}
],
"prompt_number": 5
@@ -558,25 +1295,42 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.20\n'''\ndef cube(x):\n # returns cube of x:\n return x*x*x\n\n# tests the cube() function:\nprint cube(4)\nx = int(raw_input())\ny = cube(2*x-3)\nprint y\n",
+ "input": [
+ "\n",
+ "def cube(x):\n",
+ " # returns cube of x:\n",
+ " return x*x*x\n",
+ "\n",
+ "# tests the cube() function:\n",
+ "print cube(4)\n",
+ "x = int(raw_input())\n",
+ "y = cube(2*x-3)\n",
+ "print y\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "64\n"
+ "text": [
+ "64\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "343\n"
+ "text": [
+ "343\n"
+ ]
}
],
"prompt_number": 19
@@ -584,14 +1338,40 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.21 Nested and Parallel Scopes\nPython has it's own scope so output would be differ.\n'''\nx = 11\n\ndef f():\n x = 44\n print \"In f(): x = %d\" % x \n\ndef g():\n print \"In g(): x = %d\" % x \n\nx = 22\nx = 33\nprint \"In block inside main(): x = %d\" % x\n\n\nprint \"In main(): x = %d\" % x \nprint \"In main(): ::x = %d\" % x \nf()\ng()\n",
+ "input": [
+ "\n",
+ "x = 11\n",
+ "\n",
+ "def f():\n",
+ " x = 44\n",
+ " print \"In f(): x = %d\" % x \n",
+ "\n",
+ "def g():\n",
+ " print \"In g(): x = %d\" % x \n",
+ "\n",
+ "x = 22\n",
+ "x = 33\n",
+ "print \"In block inside main(): x = %d\" % x\n",
+ "\n",
+ "\n",
+ "print \"In main(): x = %d\" % x \n",
+ "print \"In main(): ::x = %d\" % x \n",
+ "f()\n",
+ "g()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "In block inside main(): x = 33\nIn main(): x = 33\nIn main(): ::x = 33\nIn f(): x = 44\nIn g(): x = 33\n"
+ "text": [
+ "In block inside main(): x = 33\n",
+ "In main(): x = 33\n",
+ "In main(): ::x = 33\n",
+ "In f(): x = 44\n",
+ "In g(): x = 33\n"
+ ]
}
],
"prompt_number": 20
@@ -599,14 +1379,29 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.22 \n'''\n\ndef max_(x, y,z=0):\n if x > y and x > y:\n return x\n elif y > x and y > z:\n return y\n else:\n return z\n \n \nprint max(99,77), \" \" , max(55,66,33)\n",
+ "input": [
+ "\n",
+ "\n",
+ "def max_(x, y,z=0):\n",
+ " if x > y and x > y:\n",
+ " return x\n",
+ " elif y > x and y > z:\n",
+ " return y\n",
+ " else:\n",
+ " return z\n",
+ " \n",
+ " \n",
+ "print max(99,77), \" \" , max(55,66,33)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "99 66\n"
+ "text": [
+ "99 66\n"
+ ]
}
],
"prompt_number": 21
@@ -614,31 +1409,51 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.23 Using the return Statement to Terminate a Program\n'''\n\n# prints the quotient of two input integers:\nprint \"Enter two integers: \"\nn = int(raw_input())\nd = int(raw_input())\nif (d == 0):\n import sys\n sys.exit(0)\nprint n , \"/\" , d , \" = \" , n/d \n\n",
+ "input": [
+ "\n",
+ "\n",
+ "# prints the quotient of two input integers:\n",
+ "print \"Enter two integers: \"\n",
+ "n = int(raw_input())\n",
+ "d = int(raw_input())\n",
+ "if (d == 0):\n",
+ " import sys\n",
+ " sys.exit(0)\n",
+ "print n , \"/\" , d , \" = \" , n/d \n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter two integers: \n"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2\n"
+ "text": [
+ "2\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "8 / 2 = 4\n"
+ "text": [
+ "8 / 2 = 4\n"
+ ]
}
],
"prompt_number": 22
@@ -646,7 +1461,18 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.24 Using the exit() Function to Terminate a Program\n'''\n\ndef reciprocal(x):\n #returns the reciprocal of x:\n if (x == 0):\n import sys\n sys.exit(1); # terminate the program\n return 1.0/x\n\nx = float(raw_input())\nprint reciprocal(x)\n",
+ "input": [
+ "\n",
+ "def reciprocal(x):\n",
+ " #returns the reciprocal of x:\n",
+ " if (x == 0):\n",
+ " import sys\n",
+ " sys.exit(1); # terminate the program\n",
+ " return 1.0/x\n",
+ "\n",
+ "x = float(raw_input())\n",
+ "print reciprocal(x)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -654,12 +1480,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "25\n"
+ "text": [
+ "25\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "0.04\n"
+ "text": [
+ "0.04\n"
+ ]
}
],
"prompt_number": 23
@@ -667,14 +1497,32 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 5.25 Default Parameters\nThis function evaluates the third degree polynomial a0 + a1x + a2x2 + a3x3. \n'''\ndef p(x,a0,a1=0,a2=0,a3=0):\n # returns a0 + a1*x + a2*x^2 + a3*x^3:\n return (a0 + (a1 + (a2 + a3*x)*x)*x)\n\n\n# tests the p() function:\nx = 2.0003\nprint \"p(x,7) = %f\" % p(x,7)\nprint \"p(x,7,6) = %f\" % p(x,7,6)\nprint \"p(x,7,6,5) = %f\" % p(x,7,6,5)\nprint \"p(x,7,6,5,4) = %f\" % p(x,7,6,5,4)\n",
+ "input": [
+ "\n",
+ "def p(x,a0,a1=0,a2=0,a3=0):\n",
+ " # returns a0 + a1*x + a2*x^2 + a3*x^3:\n",
+ " return (a0 + (a1 + (a2 + a3*x)*x)*x)\n",
+ "\n",
+ "\n",
+ "# tests the p() function:\n",
+ "x = 2.0003\n",
+ "print \"p(x,7) = %f\" % p(x,7)\n",
+ "print \"p(x,7,6) = %f\" % p(x,7,6)\n",
+ "print \"p(x,7,6,5) = %f\" % p(x,7,6,5)\n",
+ "print \"p(x,7,6,5,4) = %f\" % p(x,7,6,5,4)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "p(x,7) = 7.000000\np(x,7,6) = 19.001800\np(x,7,6,5) = 39.007800\np(x,7,6,5,4) = 71.022203\n"
+ "text": [
+ "p(x,7) = 7.000000\n",
+ "p(x,7,6) = 19.001800\n",
+ "p(x,7,6,5) = 39.007800\n",
+ "p(x,7,6,5,4) = 71.022203\n"
+ ]
}
],
"prompt_number": 24
@@ -682,7 +1530,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb
index a355a8d2..e1a26f6e 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch6"
+ "name": "",
+ "signature": "sha256:a7aedcfda01e5e2719af0a5e76da43c6e7b01797bb61e0483e48a2e68eb0941d"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,28 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.1 Using Direct Access on Arrays\n'''\n\na = [0, 0, 0]\na[2] = 55.55\na[0] = 11.11\na[1] = 33.33\nprint \"a[0] = \" , a[0] \nprint \"a[1] = \" , a[1] \nprint \"a[2] = \" , a[2] \n",
+ "input": [
+ "\n",
+ "\n",
+ "a = [0, 0, 0]\n",
+ "a[2] = 55.55\n",
+ "a[0] = 11.11\n",
+ "a[1] = 33.33\n",
+ "print \"a[0] = \" , a[0] \n",
+ "print \"a[1] = \" , a[1] \n",
+ "print \"a[2] = \" , a[2] \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "a[0] = 11.11\na[1] = 33.33\na[2] = 55.55\n"
+ "text": [
+ "a[0] = 11.11\n",
+ "a[1] = 33.33\n",
+ "a[2] = 55.55\n"
+ ]
}
],
"prompt_number": 1
@@ -25,49 +40,82 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.2 Printing a Sequence in Order\n'''\n\nSIZE=5 # defines the size N for 5 elements\na = []\n# declares the array's elements as type double\nprint \"Enter \" , SIZE , \" numbers:\\t\"\nfor i in range(SIZE):\n a.append(float(raw_input()))\n \nprint \"In reverse order: \"\nfor i in range(SIZE-1,-1,-1):\n print \"\\t\" , a[i]\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "SIZE=5 # defines the size N for 5 elements\n",
+ "a = []\n",
+ "# declares the array's elements as type double\n",
+ "print \"Enter \" , SIZE , \" numbers:\\t\"\n",
+ "for i in range(SIZE):\n",
+ " a.append(float(raw_input()))\n",
+ " \n",
+ "print \"In reverse order: \"\n",
+ "for i in range(SIZE-1,-1,-1):\n",
+ " print \"\\t\" , a[i]\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter 5 numbers:\t\n"
+ "text": [
+ "Enter 5 numbers:\t\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1\n"
+ "text": [
+ "1\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2\n"
+ "text": [
+ "2\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "3.3\n"
+ "text": [
+ "3.3\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4.4\n"
+ "text": [
+ "4.4\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5.5\n"
+ "text": [
+ "5.5\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "In reverse order: \n\t5.5\n\t4.4\n\t3.3\n\t2.0\n\t1.0\n"
+ "text": [
+ "In reverse order: \n",
+ "\t5.5\n",
+ "\t4.4\n",
+ "\t3.3\n",
+ "\t2.0\n",
+ "\t1.0\n"
+ ]
}
],
"prompt_number": 2
@@ -75,14 +123,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.3 Initializing an Array\nThis program initializes the array a and then prints its values:\n'''\n\na = [ 22.2, 44.4, 66.6 ]\n\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i]\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "a = [ 22.2, 44.4, 66.6 ]\n",
+ "\n",
+ "size = len(a)\n",
+ "for i in range(size):\n",
+ " print \"\\ta[\" , i , \"] = \" , a[i]\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "\ta[ 0 ] = 22.2\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 66.6\n"
+ "text": [
+ "\ta[ 0 ] = 22.2\n",
+ "\ta[ 1 ] = 44.4\n",
+ "\ta[ 2 ] = 66.6\n"
+ ]
}
],
"prompt_number": 3
@@ -90,14 +151,29 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.4 Initializing an Array with Trailing Zeros\n'''\n\na = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i] \n",
+ "input": [
+ "\n",
+ "\n",
+ "a = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]\n",
+ "size = len(a)\n",
+ "for i in range(size):\n",
+ " print \"\\ta[\" , i , \"] = \" , a[i] \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "\ta[ 0 ] = 22.2\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 66.6\n\ta[ 3 ] = 0\n\ta[ 4 ] = 0\n\ta[ 5 ] = 0\n\ta[ 6 ] = 0\n"
+ "text": [
+ "\ta[ 0 ] = 22.2\n",
+ "\ta[ 1 ] = 44.4\n",
+ "\ta[ 2 ] = 66.6\n",
+ "\ta[ 3 ] = 0\n",
+ "\ta[ 4 ] = 0\n",
+ "\ta[ 5 ] = 0\n",
+ "\ta[ 6 ] = 0\n"
+ ]
}
],
"prompt_number": 4
@@ -105,14 +181,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.5 An Uninitialized Array\nNOte : In PYthon , we have to initialize array. Without initializing it, it doesn't work.\nArray size is automatically incremented when we push data into it.\n'''\nimport numpy\nSIZE = 4\na = numpy.zeros(4)\n# declares the array's elements as type float\nfor i in range(SIZE):\n print \"\\ta[\" , i , \"] = \" , a[i]\n",
+ "input": [
+ "\n",
+ "import numpy\n",
+ "SIZE = 4\n",
+ "a = numpy.zeros(4)\n",
+ "# declares the array's elements as type float\n",
+ "for i in range(SIZE):\n",
+ " print \"\\ta[\" , i , \"] = \" , a[i]\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "\ta[ 0 ] = 0.0\n\ta[ 1 ] = 0.0\n\ta[ 2 ] = 0.0\n\ta[ 3 ] = 0.0\n"
+ "text": [
+ "\ta[ 0 ] = 0.0\n",
+ "\ta[ 1 ] = 0.0\n",
+ "\ta[ 2 ] = 0.0\n",
+ "\ta[ 3 ] = 0.0\n"
+ ]
}
],
"prompt_number": 5
@@ -120,7 +209,14 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.6 Allowing an Array Index to Exceed its Bounds\nNOte : Python gives error when this kind of error occurs. so It wont print garbage values after bound exceeds.\n'''\nSIZE=4\na = [ 33.3, 44.4, 55.5, 66.6 ]\nfor i in range(7): # ERROR: index is out of bounds!\n print \"\\ta[\" , i , \"] = \" , a[i] \n\n",
+ "input": [
+ "\n",
+ "SIZE=4\n",
+ "a = [ 33.3, 44.4, 55.5, 66.6 ]\n",
+ "for i in range(7): # ERROR: index is out of bounds!\n",
+ " print \"\\ta[\" , i , \"] = \" , a[i] \n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -137,7 +233,13 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "\ta[ 0 ] = 33.3\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 55.5\n\ta[ 3 ] = 66.6\n\ta[ 4 ] = "
+ "text": [
+ "\ta[ 0 ] = 33.3\n",
+ "\ta[ 1 ] = 44.4\n",
+ "\ta[ 2 ] = 55.5\n",
+ "\ta[ 3 ] = 66.6\n",
+ "\ta[ 4 ] = "
+ ]
}
],
"prompt_number": 6
@@ -145,14 +247,24 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.7 Causing Side Effects\nThis program inadvertently changes the value of a variable when it accesses a nonexistent element of\nan array:\nNote : Python automatically increase size of array/list. so it wont give error and output would be differ than c gives.\n'''\n\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na.append(88.8) # ERROR: index is out of bounds!\nprint \"x = \" , x \n",
+ "input": [
+ "\n",
+ "a = [ 22.2, 44.4, 66.6 ]\n",
+ "x=11.1\n",
+ "print \"x = \" , x \n",
+ "a.append(88.8) # ERROR: index is out of bounds!\n",
+ "print \"x = \" , x \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": " x = 11.1\nx = 11.1\n"
+ "text": [
+ " x = 11.1\n",
+ "x = 11.1\n"
+ ]
}
],
"prompt_number": 7
@@ -160,7 +272,14 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.8 Causing Unhandled Exceptions\nThis program crashes because the array index gets too big:\n'''\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na[3333] = 88.8 # ERROR: index is out of bounds!\nprint \"x = \" , x \n",
+ "input": [
+ "\n",
+ "a = [ 22.2, 44.4, 66.6 ]\n",
+ "x=11.1\n",
+ "print \"x = \" , x \n",
+ "a[3333] = 88.8 # ERROR: index is out of bounds!\n",
+ "print \"x = \" , x \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -177,7 +296,9 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 11.1\n"
+ "text": [
+ "x = 11.1\n"
+ ]
}
],
"prompt_number": 8
@@ -185,14 +306,26 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.9 Passing an Array to a Function that Returns its Sum\n'''\n\ndef sum_(a):\n s = 0\n for i in a:\n s += i\n return s\n \na = [ 11, 33, 55, 77 ]\nprint \"sum(a) = \" , sum_(a) \n",
+ "input": [
+ "\n",
+ "def sum_(a):\n",
+ " s = 0\n",
+ " for i in a:\n",
+ " s += i\n",
+ " return s\n",
+ " \n",
+ "a = [ 11, 33, 55, 77 ]\n",
+ "print \"sum(a) = \" , sum_(a) \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "sum(a) = 176\n"
+ "text": [
+ "sum(a) = 176\n"
+ ]
}
],
"prompt_number": 10
@@ -200,49 +333,86 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.10 Input and Output Functions for an Array\nThis program uses a read() function to input values into the array a interactively. Then it uses a\nprint() function to print the array:\n'''\ndef read(a):\n print \"Enter integers. Terminate with 0:\\n\"\n n = 1\n while True:\n n = int(raw_input(\"a[\" + str(len(a)) + \"]: \"))\n if n == 0:\n break\n a.append(n)\n \n\ndef print_(a):\n for i in a:\n print i ,\n\n\na = []\nread(a)\nprint \"The array has \" , len(a) , \" elements: \"\nprint_(a)\n",
+ "input": [
+ "\n",
+ "def read(a):\n",
+ " print \"Enter integers. Terminate with 0:\\n\"\n",
+ " n = 1\n",
+ " while True:\n",
+ " n = int(raw_input(\"a[\" + str(len(a)) + \"]: \"))\n",
+ " if n == 0:\n",
+ " break\n",
+ " a.append(n)\n",
+ " \n",
+ "\n",
+ "def print_(a):\n",
+ " for i in a:\n",
+ " print i ,\n",
+ "\n",
+ "\n",
+ "a = []\n",
+ "read(a)\n",
+ "print \"The array has \" , len(a) , \" elements: \"\n",
+ "print_(a)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter integers. Terminate with 0:\n\n"
+ "text": [
+ "Enter integers. Terminate with 0:\n",
+ "\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[0]: 11\n"
+ "text": [
+ "a[0]: 11\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[1]: 22\n"
+ "text": [
+ "a[1]: 22\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[2]: 33\n"
+ "text": [
+ "a[2]: 33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[3]: 44\n"
+ "text": [
+ "a[3]: 44\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "a[4]: 0\n"
+ "text": [
+ "a[4]: 0\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "The array has 4 elements: \n11 22 33 44\n"
+ "text": [
+ "The array has 4 elements: \n",
+ "11 22 33 44\n"
+ ]
}
],
"prompt_number": 12
@@ -250,14 +420,22 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.11 Printing the Memory Location of an Array\nThis program prints the value of the address stored in an array name.\n'''\nimport sys\na = [ 22, 44, 66, 88 ]\nprint \"a = \" , id(a) # the address of a[0]\n\n",
+ "input": [
+ "\n",
+ "import sys\n",
+ "a = [ 22, 44, 66, 88 ]\n",
+ "print \"a = \" , id(a) # the address of a[0]\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "a = 169156908\n"
+ "text": [
+ "a = 169156908\n"
+ ]
}
],
"prompt_number": 13
@@ -265,14 +443,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.12 The Linear Search\nThis program tests a function that implements the Linear Search algorithm:\n'''\ndef index(x,a,n):\n for i in range(len(a)):\n if (a[i] == x):\n return i\n return n # x not found\n\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(50,a,7) = \" , index(50,a,7) \n\n\n",
+ "input": [
+ "\n",
+ "def index(x,a,n):\n",
+ " for i in range(len(a)):\n",
+ " if (a[i] == x):\n",
+ " return i\n",
+ " return n # x not found\n",
+ "\n",
+ "a = [ 22, 44, 66, 88, 44, 66, 55 ]\n",
+ "print \"index(44,a,7) = \" , index(44,a,7)\n",
+ "print \"index(50,a,7) = \" , index(50,a,7) \n",
+ "\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "index(44,a,7) = 1\nindex(50,a,7) = 7\n"
+ "text": [
+ "index(44,a,7) = 1\n",
+ "index(50,a,7) = 7\n"
+ ]
}
],
"prompt_number": 14
@@ -280,14 +474,38 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.13 The Bubble Sort\nThis program tests a function that implements the Bubble Sort algorithm.\n'''\ndef sort(a,n):\n # bubble sort:\n n = len(a)\n for i in range(n):\n # bubble up max{a[0..n-i]}:\n for j in range(n-i-1):\n if (a[j] > a[j+1]):\n a[j],a[j+1] = a[j+1],a[j]\n\ndef print_(a):\n for i in range(len(a)):\n print a[i],\n print ''\n \na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n\nprint_(a)\nsort(a,8)\nprint_(a)\n",
+ "input": [
+ "\n",
+ "def sort(a,n):\n",
+ " # bubble sort:\n",
+ " n = len(a)\n",
+ " for i in range(n):\n",
+ " # bubble up max{a[0..n-i]}:\n",
+ " for j in range(n-i-1):\n",
+ " if (a[j] > a[j+1]):\n",
+ " a[j],a[j+1] = a[j+1],a[j]\n",
+ "\n",
+ "def print_(a):\n",
+ " for i in range(len(a)):\n",
+ " print a[i],\n",
+ " print ''\n",
+ " \n",
+ "a = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n",
+ "\n",
+ "print_(a)\n",
+ "sort(a,8)\n",
+ "print_(a)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n"
+ "text": [
+ "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n",
+ "22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n"
+ ]
}
],
"prompt_number": 15
@@ -295,14 +513,38 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.14 The Binary Search Algorithm\n'''\n\ndef index(x,a,n):\n # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n # binary search:\n lo=0\n hi=n-1\n while (lo <= hi):\n i = (lo + hi)/2 # the average of lo and hi\n if (a[i] == x):\n return i\n if (a[i] < x):\n lo = i+1 # continue search in a[i+1..hi]\n else:\n hi = i-1 # continue search in a[lo..i-1]\n return n # x was not found in a[0..n-1]\n\na = [ 22, 33, 44, 55, 66, 77, 88 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(60,a,7) = \" , index(60,a,7) \n\n",
+ "input": [
+ "\n",
+ "def index(x,a,n):\n",
+ " # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n",
+ " # binary search:\n",
+ " lo=0\n",
+ " hi=n-1\n",
+ " while (lo <= hi):\n",
+ " i = (lo + hi)/2 # the average of lo and hi\n",
+ " if (a[i] == x):\n",
+ " return i\n",
+ " if (a[i] < x):\n",
+ " lo = i+1 # continue search in a[i+1..hi]\n",
+ " else:\n",
+ " hi = i-1 # continue search in a[lo..i-1]\n",
+ " return n # x was not found in a[0..n-1]\n",
+ "\n",
+ "a = [ 22, 33, 44, 55, 66, 77, 88 ]\n",
+ "print \"index(44,a,7) = \" , index(44,a,7)\n",
+ "print \"index(60,a,7) = \" , index(60,a,7) \n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "index(44,a,7) = 2\nindex(60,a,7) = 7\n"
+ "text": [
+ "index(44,a,7) = 2\n",
+ "index(60,a,7) = 7\n"
+ ]
}
],
"prompt_number": 16
@@ -310,14 +552,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.15 Determining whether an Array is Sorted\nThis program tests a boolean function that determines whether a given array is nondecreasing.\n'''\n\ndef isNondecreasing(a,n):\n # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n for i in range(1,n):\n if (a[i]<a[i-1]):\n return False\n return True\n\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"isNondecreasing(a,4) = \" , isNondecreasing(a,4)\nprint \"isNondecreasing(a,7) = \" , isNondecreasing(a,7)\n",
+ "input": [
+ "\n",
+ "\n",
+ "def isNondecreasing(a,n):\n",
+ " # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n",
+ " for i in range(1,n):\n",
+ " if (a[i]<a[i-1]):\n",
+ " return False\n",
+ " return True\n",
+ "\n",
+ "a = [ 22, 44, 66, 88, 44, 66, 55 ]\n",
+ "print \"isNondecreasing(a,4) = \" , isNondecreasing(a,4)\n",
+ "print \"isNondecreasing(a,7) = \" , isNondecreasing(a,7)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "isNondecreasing(a,4) = True\nisNondecreasing(a,7) = False\n"
+ "text": [
+ "isNondecreasing(a,4) = True\n",
+ "isNondecreasing(a,7) = False\n"
+ ]
}
],
"prompt_number": 17
@@ -325,7 +583,39 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.16 Using the assert() Function to Enforce a Precondition\nThis program tests an improved version of the search() function from Example 6.14. This version\nuses the isNondecreasing() function from Example 6.15 to determine whether the array is sorted. It\npasses the resulting boolean return value to the assert() function so that the search will not be carried\nout if the array is not sorted:\n'''\n\ndef isNondecreasing(a,n):\n # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n for i in range(1,n):\n if (a[i]<a[i-1]):\n return False\n return True\n\n\ndef index(x,a,n):\n # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n # binary search:\n assert(isNondecreasing(a,n)) \n \n lo=0\n hi=n-1\n while (lo <= hi):\n i = (lo + hi)/2\n if (a[i] == x):\n return i\n if (a[i] < x):\n lo = i+1 # continue search in a[i+1..hi]\n else:\n hi = i-1 # continue search in a[lo..i-1]\n return n # x was not found in a[0..n-1]\n\na = [ 22, 33, 44, 55, 66, 77, 88, 60 ]\nprint \"index(44,a,7) = \" , index(44,a,7) \nprint \"index(44,a,8.py) = \" , index(44,a,8) \nprint \"index(60,a,7) = \" , index(60,a,8)\n",
+ "input": [
+ "\n",
+ "\n",
+ "def isNondecreasing(a,n):\n",
+ " # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n",
+ " for i in range(1,n):\n",
+ " if (a[i]<a[i-1]):\n",
+ " return False\n",
+ " return True\n",
+ "\n",
+ "\n",
+ "def index(x,a,n):\n",
+ " # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n",
+ " # binary search:\n",
+ " assert(isNondecreasing(a,n)) \n",
+ " \n",
+ " lo=0\n",
+ " hi=n-1\n",
+ " while (lo <= hi):\n",
+ " i = (lo + hi)/2\n",
+ " if (a[i] == x):\n",
+ " return i\n",
+ " if (a[i] < x):\n",
+ " lo = i+1 # continue search in a[i+1..hi]\n",
+ " else:\n",
+ " hi = i-1 # continue search in a[lo..i-1]\n",
+ " return n # x was not found in a[0..n-1]\n",
+ "\n",
+ "a = [ 22, 33, 44, 55, 66, 77, 88, 60 ]\n",
+ "print \"index(44,a,7) = \" , index(44,a,7) \n",
+ "print \"index(44,a,8.py) = \" , index(44,a,8) \n",
+ "print \"index(60,a,7) = \" , index(60,a,8)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -343,7 +633,10 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "index(44,a,7) = 2\nindex(44,a,7) = "
+ "text": [
+ "index(44,a,7) = 2\n",
+ "index(44,a,7) = "
+ ]
}
],
"prompt_number": 18
@@ -351,14 +644,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.17 Enumerating the Days of the Week\nThis program defines an array high[] of seven floats, representing the high temperatures for\nthe seven days of a week: 138\n'''\nDay = [ 0, 1, 2, 3, 4, 5, 6 ]\nhigh = [ 88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7]\n\nfor i in Day:\n print \"The high temperature for day \" , i , \" was \" , high[i] \n\n",
+ "input": [
+ "\n",
+ "Day = [ 0, 1, 2, 3, 4, 5, 6 ]\n",
+ "high = [ 88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7]\n",
+ "\n",
+ "for i in Day:\n",
+ " print \"The high temperature for day \" , i , \" was \" , high[i] \n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": " The high temperature for day 0 was 88.3\nThe high temperature for day 1 was 95.0\nThe high temperature for day 2 was 91.2\nThe high temperature for day 3 was 89.9\nThe high temperature for day 4 was 91.4\nThe high temperature for day 5 was 92.5\nThe high temperature for day 6 was 86.7\n"
+ "text": [
+ " The high temperature for day 0 was 88.3\n",
+ "The high temperature for day 1 was 95.0\n",
+ "The high temperature for day 2 was 91.2\n",
+ "The high temperature for day 3 was 89.9\n",
+ "The high temperature for day 4 was 91.4\n",
+ "The high temperature for day 5 was 92.5\n",
+ "The high temperature for day 6 was 86.7\n"
+ ]
}
],
"prompt_number": 19
@@ -366,14 +675,31 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.18 The Bubble Sort Again\n'''\n\ndef sort(a,n):\n a.sort()\n\ndef print_(a,n):\n for i in a:\n print i,\n print ''\na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\nprint_(a,8);\nsort(a,8)\nprint_(a,8)\n",
+ "input": [
+ "\n",
+ "\n",
+ "def sort(a,n):\n",
+ " a.sort()\n",
+ "\n",
+ "def print_(a,n):\n",
+ " for i in a:\n",
+ " print i,\n",
+ " print ''\n",
+ "a = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n",
+ "print_(a,8);\n",
+ "sort(a,8)\n",
+ "print_(a,8)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n"
+ "text": [
+ "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n",
+ "22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n"
+ ]
}
],
"prompt_number": 20
@@ -381,119 +707,181 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.19 Reading and Printing a Two-Dimensional Array\nThis program shows how a two-dimensional array can be processed:\n'''\n\ndef read(a):\n print \"Enter 15 integers, 5 per row:\\n\"\n for i in range(3):\n ar = []\n print \"Row \" , i , \": \",\n for j in range(5):\n ar.append(int(raw_input()))\n a.append(ar)\n\ndef print_(a):\n for i in range(3):\n for j in range(5):\n print a[i][j],\n print ''\na = []\nread(a)\nprint_(a)\n",
+ "input": [
+ "\n",
+ "\n",
+ "def read(a):\n",
+ " print \"Enter 15 integers, 5 per row:\\n\"\n",
+ " for i in range(3):\n",
+ " ar = []\n",
+ " print \"Row \" , i , \": \",\n",
+ " for j in range(5):\n",
+ " ar.append(int(raw_input()))\n",
+ " a.append(ar)\n",
+ "\n",
+ "def print_(a):\n",
+ " for i in range(3):\n",
+ " for j in range(5):\n",
+ " print a[i][j],\n",
+ " print ''\n",
+ "a = []\n",
+ "read(a)\n",
+ "print_(a)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter 15 integers, 5 per row:\n\nRow 0 : "
+ "text": [
+ "Enter 15 integers, 5 per row:\n",
+ "\n",
+ "Row 0 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "11\n"
+ "text": [
+ "11\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Row 1 : "
+ "text": [
+ " Row 1 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "60\n"
+ "text": [
+ "60\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "50\n"
+ "text": [
+ "50\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "30\n"
+ "text": [
+ "30\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "90\n"
+ "text": [
+ "90\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "70\n"
+ "text": [
+ "70\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Row 2 : "
+ "text": [
+ " Row 2 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "85\n"
+ "text": [
+ "85\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "25\n"
+ "text": [
+ "25\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "45\n"
+ "text": [
+ "45\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "45\n"
+ "text": [
+ "45\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " 44 77 33 11 44 \n60 50 30 90 70 \n85 25 45 45 55 \n"
+ "text": [
+ " 44 77 33 11 44 \n",
+ "60 50 30 90 70 \n",
+ "85 25 45 45 55 \n"
+ ]
}
],
"prompt_number": 21
@@ -501,119 +889,204 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.20 Processing a Two-Dimensional Array of Quiz Scores\n'''\ndef read(score):\n for s in range(3):\n print \"Student \" , s , \": \",\n st = []\n for q in range(5):\n st.append(int(raw_input()))\n score.append(st)\n\ndef printQuizAverages(score):\n for s in range(3):\n sm = 0\n for q in range(5):\n sm += score[s][q]\n print \"\\tStudent \" , s , \": \" , sm/5.0\n\ndef printClassAverages(score):\n for q in range(5):\n sm = 0\n for s in range(3):\n sm += score[s][q]\n print \"\\tQuiz \" , q , \": \" , sm/3.0\n\n\n\nNUM_STUDENTS = 3\nNUM_QUIZZES = 5\n\n\nscore = []\nprint \"Enter \" , NUM_QUIZZES , \" scores for each student: \"\nread(score)\nprint \"The quiz averages are:\"\nprintQuizAverages(score)\nprint \"The class averages are: \"\nprintClassAverages(score)\n",
+ "input": [
+ "\n",
+ "def read(score):\n",
+ " for s in range(3):\n",
+ " print \"Student \" , s , \": \",\n",
+ " st = []\n",
+ " for q in range(5):\n",
+ " st.append(int(raw_input()))\n",
+ " score.append(st)\n",
+ "\n",
+ "def printQuizAverages(score):\n",
+ " for s in range(3):\n",
+ " sm = 0\n",
+ " for q in range(5):\n",
+ " sm += score[s][q]\n",
+ " print \"\\tStudent \" , s , \": \" , sm/5.0\n",
+ "\n",
+ "def printClassAverages(score):\n",
+ " for q in range(5):\n",
+ " sm = 0\n",
+ " for s in range(3):\n",
+ " sm += score[s][q]\n",
+ " print \"\\tQuiz \" , q , \": \" , sm/3.0\n",
+ "\n",
+ "\n",
+ "\n",
+ "NUM_STUDENTS = 3\n",
+ "NUM_QUIZZES = 5\n",
+ "\n",
+ "\n",
+ "score = []\n",
+ "print \"Enter \" , NUM_QUIZZES , \" scores for each student: \"\n",
+ "read(score)\n",
+ "print \"The quiz averages are:\"\n",
+ "printQuizAverages(score)\n",
+ "print \"The class averages are: \"\n",
+ "printClassAverages(score)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter 5 scores for each student: \nStudent 0 : "
+ "text": [
+ "Enter 5 scores for each student: \n",
+ "Student 0 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Student 1 : "
+ "text": [
+ " Student 1 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " Student 2 : "
+ "text": [
+ " Student 2 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "5\n"
+ "text": [
+ "5\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "6\n"
+ "text": [
+ "6\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "8\n"
+ "text": [
+ "8\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9\n"
+ "text": [
+ "9\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " The quiz averages are:\n\tStudent 0 : 8.2\n\tStudent 1 : 8.8\n\tStudent 2 : 7.0\nThe class averages are: \n\tQuiz 0 : 7.33333333333\n\tQuiz 1 : 7.33333333333\n\tQuiz 2 : 8.33333333333\n\tQuiz 3 : 8.33333333333\n\tQuiz 4 : 8.66666666667\n"
+ "text": [
+ " The quiz averages are:\n",
+ "\tStudent 0 : 8.2\n",
+ "\tStudent 1 : 8.8\n",
+ "\tStudent 2 : 7.0\n",
+ "The class averages are: \n",
+ "\tQuiz 0 : 7.33333333333\n",
+ "\tQuiz 1 : 7.33333333333\n",
+ "\tQuiz 2 : 8.33333333333\n",
+ "\tQuiz 3 : 8.33333333333\n",
+ "\tQuiz 4 : 8.66666666667\n"
+ ]
}
],
"prompt_number": 24
@@ -621,14 +1094,32 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 6.21 Processing a Three-Dimensional Array\nThis program simply counts the number of zeros in a three-dimensional array:\n'''\n\ndef numZeros(a,n1,n2,n3):\n count = 0\n for i in range(n1):\n for j in range(n2):\n for k in range(n3):\n if (a[i][j][k] == 0):\n count += 1\n return count\n\n\na = [ [ [5,0,2], [0,0,9], [4,1,0], [7,7,7] ],[ [3,0,0], [8,5,0], [0,0,0], [2,0,9] ]]\nprint \"This array has \" , numZeros(a,2,4,3) , \" zeros\"\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "def numZeros(a,n1,n2,n3):\n",
+ " count = 0\n",
+ " for i in range(n1):\n",
+ " for j in range(n2):\n",
+ " for k in range(n3):\n",
+ " if (a[i][j][k] == 0):\n",
+ " count += 1\n",
+ " return count\n",
+ "\n",
+ "\n",
+ "a = [ [ [5,0,2], [0,0,9], [4,1,0], [7,7,7] ],[ [3,0,0], [8,5,0], [0,0,0], [2,0,9] ]]\n",
+ "print \"This array has \" , numZeros(a,2,4,3) , \" zeros\"\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "This array has 11 zeros\n"
+ "text": [
+ "This array has 11 zeros\n"
+ ]
}
],
"prompt_number": 25
@@ -636,7 +1127,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb
index f480fc86..ad8cdac0 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch7"
+ "name": "",
+ "signature": "sha256:653231542b6fdffa18c8d3af4f8fe1f2545223a4300162071901adcb77895ba7"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,24 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.1 Printing Pointer Values\n'''\n\nn=44\nprint \"n = \" , n \n# prints the value of n\nprint \"&n = \" , hex(id(n)) # prints the address of n\n",
+ "input": [
+ "\n",
+ "\n",
+ "n=44\n",
+ "print \"n = \" , n \n",
+ "# prints the value of n\n",
+ "print \"&n = \" , hex(id(n)) # prints the address of n\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 44\n&n = 0x8fc0eec\n"
+ "text": [
+ "n = 44\n",
+ "&n = 0x8fc0eec\n"
+ ]
}
],
"prompt_number": 1
@@ -25,14 +36,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.2\nNote : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"n = \" , n , \", rn = \" , rn \nn[0] -= 1\nprint \"n = \" , n , \", rn = \" , rn \nrn[0] *= 2\nprint \"n = \" , n , \", rn = \" , rn \n\n",
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "rn=n # r is a synonym for n\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "n[0] -= 1\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "rn[0] *= 2\n",
+ "print \"n = \" , n , \", rn = \" , rn \n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = [44] , rn = [44]\nn = [43] , rn = [43]\nn = [86] , rn = [86]\n"
+ "text": [
+ "n = [44] , rn = [44]\n",
+ "n = [43] , rn = [43]\n",
+ "n = [86] , rn = [86]\n"
+ ]
}
],
"prompt_number": 2
@@ -40,14 +67,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.3 References Are Not Separate Variables\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"&n = \" , hex(id(n)) , \", rn = \" , hex(id(rn ))\nrn2 = n\nrn3 = rn\nprint \"&rn2 = \" , hex(id(rn2)) , \", rn = \" , hex(id(rn ))\n",
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "n = [44]\n",
+ "rn=n # r is a synonym for n\n",
+ "print \"&n = \" , hex(id(n)) , \", rn = \" , hex(id(rn ))\n",
+ "rn2 = n\n",
+ "rn3 = rn\n",
+ "print \"&rn2 = \" , hex(id(rn2)) , \", rn = \" , hex(id(rn ))\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "&n = 0x9c6228c , rn = 0x9c6228c\n&rn2 = 0x9c6228c , rn = 0x9c6228c\n"
+ "text": [
+ "&n = 0x9c6228c , rn = 0x9c6228c\n",
+ "&rn2 = 0x9c6228c , rn = 0x9c6228c\n"
+ ]
}
],
"prompt_number": 3
@@ -55,14 +95,26 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.4 Using Pointer Variables\n\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"pn = \" , hex(id(pn)) , \", &pn = \" , hex(id(hex(id(pn))))\n\n\n",
+ "input": [
+ " \n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"pn = \" , hex(id(pn)) , \", &pn = \" , hex(id(hex(id(pn))))\n",
+ "\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = [44] , &n = 0x9c624ec\npn = 0x9c624ec , &pn = 0x9c6aa60\n"
+ "text": [
+ "n = [44] , &n = 0x9c624ec\n",
+ "pn = 0x9c624ec , &pn = 0x9c6aa60\n"
+ ]
}
],
"prompt_number": 4
@@ -70,14 +122,28 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.5\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\n\n",
+ "input": [
+ " \n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = [44] , &n = 0x9c58d6c\n\tpn = 0x9c58d6c ,\n &pn = 0x9c6ab20\n*pn = [44]\n"
+ "text": [
+ "n = [44] , &n = 0x9c58d6c\n",
+ "\tpn = 0x9c58d6c ,\n",
+ " &pn = 0x9c6ab20\n",
+ "*pn = [44]\n"
+ ]
}
],
"prompt_number": 5
@@ -85,14 +151,36 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.6 \n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nppn = pn\n\nprint \" ppn = \" , hex(id(hex(id(ppn)))) \nprint \" &ppn = \" , hex(id(hex(id(hex(id(ppn))))))\nprint \" *ppn = \" , hex(id(ppn)) \nprint \"**ppn = \" , ppn \n",
+ "input": [
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \", &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "ppn = pn\n",
+ "\n",
+ "print \" ppn = \" , hex(id(hex(id(ppn)))) \n",
+ "print \" &ppn = \" , hex(id(hex(id(hex(id(ppn))))))\n",
+ "print \" *ppn = \" , hex(id(ppn)) \n",
+ "print \"**ppn = \" , ppn \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = [44] , &n = 0x9bf05ac\n\tpn = 0x9bf05ac ,\n &pn = 0x9c58160\n*pn = [44]\n ppn = 0x9c58680\n &ppn = 0x9c58160\n *ppn = 0x9bf05ac\n**ppn = [44]\n"
+ "text": [
+ "n = [44] , &n = 0x9bf05ac\n",
+ "\tpn = 0x9bf05ac ,\n",
+ " &pn = 0x9c58160\n",
+ "*pn = [44]\n",
+ " ppn = 0x9c58680\n",
+ " &ppn = 0x9c58160\n",
+ " *ppn = 0x9bf05ac\n",
+ "**ppn = [44]\n"
+ ]
}
],
"prompt_number": 6
@@ -100,14 +188,40 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.7\n'''\n\nn = [44]\nprint \"n = \" , n , \"\\n &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nnn = pn\nprint \" ppn = \" , hex(id(nn))\nprint \" &ppn = \" , hex(id(hex(id(nn))))\nrpn = pn\nprint \" ppn = \" , hex(id(rpn))\nprint \" &ppn = \" , hex(id(hex(id(rpn))))\n\n\n",
+ "input": [
+ " \n",
+ "\n",
+ "n = [44]\n",
+ "print \"n = \" , n , \"\\n &n = \" , hex(id(n))\n",
+ "pn = n\n",
+ "print \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"*pn = \" , pn\n",
+ "nn = pn\n",
+ "print \" ppn = \" , hex(id(nn))\n",
+ "print \" &ppn = \" , hex(id(hex(id(nn))))\n",
+ "rpn = pn\n",
+ "print \" ppn = \" , hex(id(rpn))\n",
+ "print \" &ppn = \" , hex(id(hex(id(rpn))))\n",
+ "\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = [44] \n &n = 0x9bf60ec\n\tpn = 0x9bf60ec ,\n &pn = 0x9bf0e40\n*pn = [44]\n ppn = 0x9bf60ec\n &ppn = 0x9bf0e40\n ppn = 0x9bf60ec\n &ppn = 0x9bf0f20\n"
+ "text": [
+ "n = [44] \n",
+ " &n = 0x9bf60ec\n",
+ "\tpn = 0x9bf60ec ,\n",
+ " &pn = 0x9bf0e40\n",
+ "*pn = [44]\n",
+ " ppn = 0x9bf60ec\n",
+ " &ppn = 0x9bf0e40\n",
+ " ppn = 0x9bf60ec\n",
+ " &ppn = 0x9bf0f20\n"
+ ]
}
],
"prompt_number": 7
@@ -115,14 +229,32 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.8\n'''\n\ndef max_(m,n):\n if m>n:\n return m\n else:\n return n\n\nm = 44\nn = 22\nprint m , \", \" , n , \", \" , max_(m,n)\nm = max_(m,n) \nm = 55\n# changes the value of m from 44 to 55\nprint m , \", \" , n , \", \" , max_(m,n) \n",
+ "input": [
+ "\n",
+ "def max_(m,n):\n",
+ " if m>n:\n",
+ " return m\n",
+ " else:\n",
+ " return n\n",
+ "\n",
+ "m = 44\n",
+ "n = 22\n",
+ "print m , \", \" , n , \", \" , max_(m,n)\n",
+ "m = max_(m,n) \n",
+ "m = 55\n",
+ "# changes the value of m from 44 to 55\n",
+ "print m , \", \" , n , \", \" , max_(m,n) \n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "44 , 22 , 44\n55 , 22 , 55\n"
+ "text": [
+ "44 , 22 , 44\n",
+ "55 , 22 , 55\n"
+ ]
}
],
"prompt_number": 8
@@ -130,14 +262,27 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.9 \n'''\n\nv = []\nfor k in range(1,5):\n v.append(1.0/k)\n\nfor i in range(4):\n print \"v[\" , i , \"] = \" , v[i]\n",
+ "input": [
+ " \n",
+ "v = []\n",
+ "for k in range(1,5):\n",
+ " v.append(1.0/k)\n",
+ "\n",
+ "for i in range(4):\n",
+ " print \"v[\" , i , \"] = \" , v[i]\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "v[ 0 ] = 1.0\nv[ 1 ] = 0.5\nv[ 2 ] = 0.333333333333\nv[ 3 ] = 0.25\n"
+ "text": [
+ "v[ 0 ] = 1.0\n",
+ "v[ 1 ] = 0.5\n",
+ "v[ 2 ] = 0.333333333333\n",
+ "v[ 3 ] = 0.25\n"
+ ]
}
],
"prompt_number": 9
@@ -145,14 +290,34 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.10 \n'''\nimport sys\na = [22, 33, 44]\n\nprint \"a = \" , hex(id(a))\nprint \"sizeof(int) = \" , sys.getsizeof(1) \ns = 0\nfor i in a:\n s += i\n print \"\\t i = \" , hex(id(i)),\n print \"\\t *i = \" , i,\n print \"\\t sum = \" , s\n\n",
+ "input": [
+ "\n",
+ "import sys\n",
+ "a = [22, 33, 44]\n",
+ "\n",
+ "print \"a = \" , hex(id(a))\n",
+ "print \"sizeof(int) = \" , sys.getsizeof(1) \n",
+ "s = 0\n",
+ "for i in a:\n",
+ " s += i\n",
+ " print \"\\t i = \" , hex(id(i)),\n",
+ " print \"\\t *i = \" , i,\n",
+ " print \"\\t sum = \" , s\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "a = 0x9bf688c\nsizeof(int) = 12\n\t i = 0x8fc0ff4 \t *i = 22 \t sum = 22\n\t i = 0x8fc0f70 \t *i = 33 \t sum = 55\n\t i = 0x8fc0eec \t *i = 44 \t sum = 99\n"
+ "text": [
+ "a = 0x9bf688c\n",
+ "sizeof(int) = 12\n",
+ "\t i = 0x8fc0ff4 \t *i = 22 \t sum = 22\n",
+ "\t i = 0x8fc0f70 \t *i = 33 \t sum = 55\n",
+ "\t i = 0x8fc0eec \t *i = 44 \t sum = 99\n"
+ ]
}
],
"prompt_number": 10
@@ -160,14 +325,29 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.11 Examining the Addresses of Array Elements\n'''\n\na = [22, 33, 44, 55, 66]\nprint \"a = \" , hex(id(a)) , \", *a = \" , a[0] \nfor p in a:\n print \"p = \" , hex(id(p)) , \", *p = \" , p \n\n",
+ "input": [
+ " \n",
+ "\n",
+ "a = [22, 33, 44, 55, 66]\n",
+ "print \"a = \" , hex(id(a)) , \", *a = \" , a[0] \n",
+ "for p in a:\n",
+ " print \"p = \" , hex(id(p)) , \", *p = \" , p \n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "a = 0x9c6526c , *a = 22\np = 0x8fc0ff4 , *p = 22\np = 0x8fc0f70 , *p = 33\np = 0x8fc0eec , *p = 44\np = 0x8fc0e68 , *p = 55\np = 0x8fc0de4 , *p = 66\n"
+ "text": [
+ "a = 0x9c6526c , *a = 22\n",
+ "p = 0x8fc0ff4 , *p = 22\n",
+ "p = 0x8fc0f70 , *p = 33\n",
+ "p = 0x8fc0eec , *p = 44\n",
+ "p = 0x8fc0e68 , *p = 55\n",
+ "p = 0x8fc0de4 , *p = 66\n"
+ ]
}
],
"prompt_number": 11
@@ -175,14 +355,44 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.12 Pattern Matching\n'''\n\ndef loc(a1,a2,n1,n2):\n p = []\n for element in a2:\n if element in a1:\n p.append(element)\n return p\n\na1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]\na2 = [11, 11, 11, 22, 33]\nprint \"Array a1 begins at location\\t\" , hex(id(a1 ))\nprint \"Array a2 begins at location\\t\" , hex(id(a2)) \np = loc(a1, a2, 9, 5)\nif (p):\n print \"Array a2 found at location\\t\" , hex(id(p))\n for i in range(len(p)):\n print \"\\t\" , hex(id(p[i])) , \": \" , p[i], \"\\t\" , hex(id(a2[i])) , \": \" , a2[i] \nelse:\n print \"Not found.\"\n",
+ "input": [
+ " \n",
+ "\n",
+ "def loc(a1,a2,n1,n2):\n",
+ " p = []\n",
+ " for element in a2:\n",
+ " if element in a1:\n",
+ " p.append(element)\n",
+ " return p\n",
+ "\n",
+ "a1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]\n",
+ "a2 = [11, 11, 11, 22, 33]\n",
+ "print \"Array a1 begins at location\\t\" , hex(id(a1 ))\n",
+ "print \"Array a2 begins at location\\t\" , hex(id(a2)) \n",
+ "p = loc(a1, a2, 9, 5)\n",
+ "if (p):\n",
+ " print \"Array a2 found at location\\t\" , hex(id(p))\n",
+ " for i in range(len(p)):\n",
+ " print \"\\t\" , hex(id(p[i])) , \": \" , p[i], \"\\t\" , hex(id(a2[i])) , \": \" , a2[i] \n",
+ "else:\n",
+ " print \"Not found.\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Array a1 begins at location\t0x9bea56c\nArray a2 begins at location\t0x9bea62c\nArray a2 found at location\t0x9bea6cc\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc0ff4 : 22 \t0x8fc0ff4 : 22\n\t0x8fc0f70 : 33 \t0x8fc0f70 : 33\n"
+ "text": [
+ "Array a1 begins at location\t0x9bea56c\n",
+ "Array a2 begins at location\t0x9bea62c\n",
+ "Array a2 found at location\t0x9bea6cc\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc1078 : 11 \t0x8fc1078 : 11\n",
+ "\t0x8fc0ff4 : 22 \t0x8fc0ff4 : 22\n",
+ "\t0x8fc0f70 : 33 \t0x8fc0f70 : 33\n"
+ ]
}
],
"prompt_number": 12
@@ -190,102 +400,161 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.15 Using Dynamic Arrays\n'''\n\ndef get(a):\n print \"Enter number of items: \"\n n = int(raw_input())\n print \"Enter \" , n , \" items, one per line:\"\n for i in range(n):\n print \"\\t\" , i+1 , \": \",\n a.append(float(raw_input()))\n\ndef print_(a):\n for i in range(len(a)):\n print a[i] ,\n print ''\n\na = []\nget(a)\nprint_(a)\na = []\nget(a)\nprint_(a)\n",
+ "input": [
+ " \n",
+ "\n",
+ "def get(a):\n",
+ " print \"Enter number of items: \"\n",
+ " n = int(raw_input())\n",
+ " print \"Enter \" , n , \" items, one per line:\"\n",
+ " for i in range(n):\n",
+ " print \"\\t\" , i+1 , \": \",\n",
+ " a.append(float(raw_input()))\n",
+ "\n",
+ "def print_(a):\n",
+ " for i in range(len(a)):\n",
+ " print a[i] ,\n",
+ " print ''\n",
+ "\n",
+ "a = []\n",
+ "get(a)\n",
+ "print_(a)\n",
+ "a = []\n",
+ "get(a)\n",
+ "print_(a)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter number of items: \n"
+ "text": [
+ "Enter number of items: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "4\n"
+ "text": [
+ "4\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter 4 items, one per line:\n\t1 : "
+ "text": [
+ "Enter 4 items, one per line:\n",
+ "\t1 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44.4\n"
+ "text": [
+ "44.4\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " \t2 : "
+ "text": [
+ " \t2 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77.7\n"
+ "text": [
+ "77.7\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " \t3 : "
+ "text": [
+ " \t3 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "22.2\n"
+ "text": [
+ "22.2\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " \t4 : "
+ "text": [
+ " \t4 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "88.8\n"
+ "text": [
+ "88.8\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " 44.4 77.7 22.2 88.8 \nEnter number of items: \n"
+ "text": [
+ " 44.4 77.7 22.2 88.8 \n",
+ "Enter number of items: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "2\n"
+ "text": [
+ "2\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Enter 2 items, one per line:\n\t1 : "
+ "text": [
+ "Enter 2 items, one per line:\n",
+ "\t1 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "3.33\n"
+ "text": [
+ "3.33\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " \t2 : "
+ "text": [
+ " \t2 : "
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "9.99\n"
+ "text": [
+ "9.99\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": " 3.33 9.99 \n"
+ "text": [
+ " 3.33 9.99 \n"
+ ]
}
],
"prompt_number": 13
@@ -293,7 +562,15 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.17 Indirect Bubble Sort\n'''\n\ndef sort(p, n):\n for i in range(1,n):\n for j in range(n-i):\n if (p[j] > p[j+1]):\n p[j],p[j+1] = p[j+1],p[j]\n",
+ "input": [
+ " \n",
+ "\n",
+ "def sort(p, n):\n",
+ " for i in range(1,n):\n",
+ " for j in range(n-i):\n",
+ " if (p[j] > p[j+1]):\n",
+ " p[j],p[j+1] = p[j+1],p[j]\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -302,14 +579,37 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 7.18 The Sum of a Function\n'''\n\ndef sum_(k,n):\n # returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):\n s = 0\n for i in range(1,n+1):\n s += k(i)\n return s\n\ndef square(k):\n return k*k\n\ndef cube(k):\n return k*k*k\n\n\nprint sum_(square,4) # 1 + 4 + 9 + 16\nprint sum_(cube,4) # 1 + 8 + 27 + 64\n\n",
+ "input": [
+ " \n",
+ "\n",
+ "def sum_(k,n):\n",
+ " # returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):\n",
+ " s = 0\n",
+ " for i in range(1,n+1):\n",
+ " s += k(i)\n",
+ " return s\n",
+ "\n",
+ "def square(k):\n",
+ " return k*k\n",
+ "\n",
+ "def cube(k):\n",
+ " return k*k*k\n",
+ "\n",
+ "\n",
+ "print sum_(square,4) # 1 + 4 + 9 + 16\n",
+ "print sum_(cube,4) # 1 + 8 + 27 + 64\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "30\n100\n"
+ "text": [
+ "30\n",
+ "100\n"
+ ]
}
],
"prompt_number": 15
@@ -317,7 +617,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb
index 033f6e3f..a4d4d774 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch8"
+ "name": "",
+ "signature": "sha256:79346296b970ba1771cf99da135ed51f1d98b24ac99e0b8758fa3cab63c95d45"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,77 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "n= [44] # n holds the int 44\n",
+ "print \"int n=44; // n holds the int 44:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "pn = n \n",
+ "print \"int* pn=&n; // pn holds the address of n:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "\n",
+ "pn[0] = 77 # changes the value of n to 77\n",
+ "print \"*pn = 77; // changes the value of n to 77:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "\n",
+ "q = n \n",
+ "print \"int* q=&n; // q also holds the address of n:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "print \"\\t\\t q = \" , hex(id(q))\n",
+ "print \"\\t\\t &q = \" , hex(id(hex(id(hex(id(pn))))))\n",
+ "print \"\\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"
+ "text": [
+ "int n=44; // n holds the int 44:\n",
+ "\n",
+ "\t\t n = [44]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "int* 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]\n",
+ "int* 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
@@ -25,14 +89,29 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "s = \"ABCD\"\n",
+ "for 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"
+ "text": [
+ "s[ 0 ] = ' A '\n",
+ "\n",
+ "s[ 1 ] = ' B '\n",
+ "\n",
+ "s[ 2 ] = ' C '\n",
+ "\n",
+ "s[ 3 ] = ' D '\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -40,7 +119,16 @@
{
"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",
+ "input": [
+ " \n",
+ "while 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": [
@@ -48,29 +136,46 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Today's date is March 12, 2000.\n"
+ "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"
+ "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"
+ "text": [
+ "Tomorrow is Monday.\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\t\" Tomorrow \"\n\t\" is \"\n\t\" Monday. \"\n"
+ "text": [
+ "\t\" Tomorrow \"\n",
+ "\t\" is \"\n",
+ "\t\" Monday. \"\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "text": [
+ "\n"
+ ]
}
],
"prompt_number": 6
@@ -78,7 +183,15 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "while True:\n",
+ " line = raw_input()\n",
+ " if len(line) < 2:\n",
+ " break\n",
+ " print \"\\t[\" , line , \"]\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -86,29 +199,39 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ "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"
+ "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"
+ "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"
+ "text": [
+ "\t[ Over a many quaint and curious volume of forgotten lore, ]\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "text": [
+ "\n"
+ ]
}
],
"prompt_number": 7
@@ -116,7 +239,19 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "\n",
+ "while 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": [
@@ -124,29 +259,41 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ "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"
+ "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"
+ "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"
+ "text": [
+ "\t[ Over a many quaint and curious volume of forgotten lore ]\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "text": [
+ "\n"
+ ]
}
],
"prompt_number": 8
@@ -154,7 +301,19 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "count = 0\n",
+ "while True:\n",
+ " a = raw_input()\n",
+ " if len(a) < 1:\n",
+ " break\n",
+ " for ch in a:\n",
+ " if (ch == 'e'): count+=1\n",
+ " \n",
+ "print count , \" e's were counted.\\n\"\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -162,24 +321,33 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ "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"
+ "text": [
+ "Over many a quaint and curious volume of forgotten lore,\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "text": [
+ "\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "11 e's were counted.\n\n"
+ "text": [
+ "11 e's were counted.\n",
+ "\n"
+ ]
}
],
"prompt_number": 9
@@ -187,7 +355,14 @@
{
"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",
+ "input": [
+ " \n",
+ "while True:\n",
+ " a = raw_input()\n",
+ " if len(a) < 1:\n",
+ " break\n",
+ " print a.title()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -195,29 +370,39 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Fourscore and seven years ago our fathers\n"
+ "text": [
+ "Fourscore and seven years ago our fathers\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Fourscore And Seven Years Ago Our Fathers\n"
+ "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"
+ "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"
+ "text": [
+ "Brought Forth Upon This Continent A New Nation,\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "text": [
+ "\n"
+ ]
}
],
"prompt_number": 10
@@ -225,7 +410,22 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "a = raw_input()\n",
+ "l = a.split(' ')\n",
+ "nos = []\n",
+ "for i in l:\n",
+ " try:\n",
+ " i = int(i)\n",
+ " nos.append(i)\n",
+ " except:\n",
+ " continue\n",
+ "m = nos[0]\n",
+ "n = nos[1] \n",
+ "print m , \" + \" , n , \" = \" , m+n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -233,12 +433,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "what is 305 plus 9416 ?\n"
+ "text": [
+ "what is 305 plus 9416 ?\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "305 + 9416 = 9721\n"
+ "text": [
+ "305 + 9416 = 9721\n"
+ ]
}
],
"prompt_number": 11
@@ -246,43 +450,77 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "name = []\n",
+ "count=0\n",
+ "\n",
+ "print \"Enter at most 4 names with at most 19 characters:\\n\";\n",
+ "while (True):\n",
+ " n = raw_input()\n",
+ " if len(n) < 1:\n",
+ " break\n",
+ " name.append(n)\n",
+ " count += 1\n",
+ " \n",
+ "print \"The names are:\\n\"\n",
+ "for 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"
+ "text": [
+ "Enter at most 4 names with at most 19 characters:\n",
+ "\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "George Washington\n"
+ "text": [
+ "George Washington\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "John Adams\n"
+ "text": [
+ "John Adams\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Thomas Jefferson\n"
+ "text": [
+ "Thomas Jefferson\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "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"
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
}
],
"prompt_number": 12
@@ -290,43 +528,77 @@
{
"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] , \"]\" ",
+ "input": [
+ " \n",
+ "\n",
+ "name = []\n",
+ "count=0\n",
+ "\n",
+ "print \"Enter at most 4 names with at most 19 characters:\\n\";\n",
+ "while (True):\n",
+ " n = raw_input()\n",
+ " if len(n) < 1:\n",
+ " break\n",
+ " name.append(n)\n",
+ " count += 1\n",
+ " \n",
+ "print \"The names are:\\n\"\n",
+ "for 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"
+ "text": [
+ "Enter at most 4 names with at most 19 characters:\n",
+ "\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "George Washington\n"
+ "text": [
+ "George Washington\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "John Adams\n"
+ "text": [
+ "John Adams\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Thomas Jefferson\n"
+ "text": [
+ "Thomas Jefferson\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "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"
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
}
],
"prompt_number": 13
@@ -334,14 +606,28 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "name = [ \"George Washington\", \"John Adams\", \"Thomas Jefferson\"]\n",
+ "print \"The names are:\\n\"\n",
+ "for 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"
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
}
],
"prompt_number": 14
@@ -349,25 +635,42 @@
{
"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)",
+ "input": [
+ " \n",
+ "\n",
+ "s = \"ABCDEFG\"\n",
+ "print \"len(\" , s , \") = \" , len(s) \n",
+ "print \"len(\\\"\\\") = \" , len(\"\")\n",
+ "print \"Enter string: \"\n",
+ "b = raw_input()\n",
+ "print \"len(\" , b , \") = \" , len(b)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "len( ABCDEFG ) = 7\nlen(\"\") = 0\nEnter string: \n"
+ "text": [
+ "len( ABCDEFG ) = 7\n",
+ "len(\"\") = 0\n",
+ "Enter string: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "hello how are you !!!\n"
+ "text": [
+ "hello how are you !!!\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "len( hello how are you !!! ) = 21\n"
+ "text": [
+ "len( hello how are you !!! ) = 21\n"
+ ]
}
],
"prompt_number": 15
@@ -375,14 +678,36 @@
{
"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",
+ "input": [
+ " \n",
+ "s = \"The Mississippi is a long river.\"\n",
+ "print 's = \"' , s , '\"'\n",
+ "p = s.find(' ')\n",
+ "print \"find(s, ' ') points to s[\" , p , \"].\"\n",
+ "p = s.find('s')\n",
+ "print \"find(s, 's') points to s[\" , p , \"].\"\n",
+ "p = s.rfind('s')\n",
+ "print \"reverse find(s, 's') points to s[\" , p , \"].\"\n",
+ "p = s.find(\"is\")\n",
+ "print \"strstr(s, \\\"is\\\") points to s[\" , p , \"].\"\n",
+ "p = s.find(\"isi\")\n",
+ "if 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"
+ "text": [
+ "s = \" The Mississippi is a long river. \"\n",
+ "find(s, ' ') points to s[ 3 ].\n",
+ "find(s, 's') points to s[ 6 ].\n",
+ "reverse find(s, 's') points to s[ 17 ].\n",
+ "strstr(s, \"is\") points to s[ 5 ].\n",
+ "s.find(\"isi\") returns NULL\n"
+ ]
}
],
"prompt_number": 16
@@ -390,14 +715,34 @@
{
"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",
+ "input": [
+ " \n",
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before strcpy(s1,s2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 = s2\n",
+ "print \"After strcpy(s1,s2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\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"
+ "text": [
+ "Before strcpy(s1,s2):\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After strcpy(s1,s2):\n",
+ "\n",
+ "\ts1 = [ XYZ ], length = 3\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
}
],
"prompt_number": 17
@@ -405,14 +750,37 @@
{
"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",
+ "input": [
+ " \n",
+ "\n",
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before strcpy(s1,s2,2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 = s2[:2] + s1[2:]\n",
+ "print \"After strcpy(s1,s2,2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\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"
+ "text": [
+ "Before strcpy(s1,s2,2):\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After strcpy(s1,s2,2):\n",
+ "\n",
+ "\ts1 = [ XYCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
}
],
"prompt_number": 18
@@ -420,14 +788,34 @@
{
"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) ",
+ "input": [
+ " \n",
+ "\n",
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before string concatination :\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 += s2\n",
+ "print \"After string concatination :\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\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"
+ "text": [
+ "Before string concatination :\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After string concatination :\n",
+ "\ts1 = [ ABCDEFGXYZ ], length = 10\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
}
],
"prompt_number": 19
@@ -435,14 +823,34 @@
{
"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) ",
+ "input": [
+ " \n",
+ "\n",
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before string concatination :\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 += s2[:2]\n",
+ "print \"After string concatination :\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\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"
+ "text": [
+ "Before string concatination :\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After string concatination :\n",
+ "\ts1 = [ ABCDEFGXY ], length = 9\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
}
],
"prompt_number": 20
@@ -450,14 +858,35 @@
{
"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",
+ "input": [
+ "\n",
+ "s = \"Today's date is March 12, 2000.\"\n",
+ "\n",
+ "print \"The string is: [\" , s , \"] \\nIts tokens are: \"\n",
+ "p = s.split(\" \")\n",
+ "\n",
+ "for i in p:\n",
+ " print \"\\t[\" , i , \"] \"\n",
+ "\n",
+ "print \"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"
+ "text": [
+ "The string is: [ Today's date is March 12, 2000. ] \n",
+ "Its tokens are: \n",
+ "\t[ Today's ] \n",
+ "\t[ date ] \n",
+ "\t[ is ] \n",
+ "\t[ March ] \n",
+ "\t[ 12, ] \n",
+ "\t[ 2000. ] \n",
+ "Now the string is: [ Today's ] \n"
+ ]
}
],
"prompt_number": 21
@@ -465,14 +894,40 @@
{
"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'",
+ "input": [
+ " \n",
+ "\n",
+ "def 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",
+ "\n",
+ "s = \"The Mississippi is a long river.\"\n",
+ "print 's = \"' , s , '\"'\n",
+ "p = strpbrk(s, \"nopqr\")\n",
+ "print 'strpbrk(s, \"nopqr\") points to s[' , p , \"].\"\n",
+ "p = strpbrk(s, \"NOPQR\")\n",
+ "if (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"
+ "text": [
+ "s = \" The Mississippi is a long river. \"\n",
+ "strpbrk(s, \"nopqr\") points to s[ 12 ].\n",
+ "strpbrk(s, \"NOPQR\") returns NULL.\n",
+ "\n"
+ ]
}
],
"prompt_number": 22
@@ -480,7 +935,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb
index fabb9a56..d38faaa6 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch9"
+ "name": "",
+ "signature": "sha256:636fa14bc9bbc0edbdee2b8486af1239be5ff3d58b955831baa9b4ccd512a82c"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,7 +11,16 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 9.2 Using the Extraction Operation to Control a Loop\n'''\n\n\nwhile True:\n try:\n n = int(raw_input())\n print \"n = \" , n \n except:\n break",
+ "input": [
+ " \n",
+ "\n",
+ "while True:\n",
+ " try:\n",
+ " n = int(raw_input())\n",
+ " print \"n = \" , n \n",
+ " except:\n",
+ " break"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -18,62 +28,84 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "46\n"
+ "text": [
+ "46\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 46\n"
+ "text": [
+ "n = 46\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "22\n"
+ "text": [
+ "22\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 22\n"
+ "text": [
+ "n = 22\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 44\n"
+ "text": [
+ "n = 44\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "66\n"
+ "text": [
+ "66\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 66\n"
+ "text": [
+ "n = 66\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "88\n"
+ "text": [
+ "88\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 88\n"
+ "text": [
+ "n = 88\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33,\n"
+ "text": [
+ "33,\n"
+ ]
}
],
"prompt_number": 1
@@ -81,7 +113,21 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 9.4 Inputting Strings with the raw_input() Function\nThis program shows how to read text data line-by-line into an array \n'''\n\nking = [] # defines king to be an array \nn=0\nwhile True:\n name = raw_input()\n if len(name) < 1:\n break\n king.append(name)\n n += 1\n# now n == the number of names read\nfor i in range(n):\n print '\\t' , i+1 , \". \" , king[i] ",
+ "input": [
+ " \n",
+ "\n",
+ "king = [] # defines king to be an array \n",
+ "n=0\n",
+ "while True:\n",
+ " name = raw_input()\n",
+ " if len(name) < 1:\n",
+ " break\n",
+ " king.append(name)\n",
+ " n += 1\n",
+ "# now n == the number of names read\n",
+ "for i in range(n):\n",
+ " print '\\t' , i+1 , \". \" , king[i] "
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -89,60 +135,87 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Kenneth II (971-995)\n"
+ "text": [
+ "Kenneth II (971-995)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Constantine III (995-997)\n"
+ "text": [
+ "Constantine III (995-997)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Kenneth III (997-1005)\n"
+ "text": [
+ "Kenneth III (997-1005)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Malcolm II (1005-1034)\n"
+ "text": [
+ "Malcolm II (1005-1034)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Duncan I (1034-1040)\n"
+ "text": [
+ "Duncan I (1034-1040)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Macbeth (1040-1057)\n"
+ "text": [
+ "Macbeth (1040-1057)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Lulach (1057-1058)\n"
+ "text": [
+ "Lulach (1057-1058)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Malcolm III (1058-1093)\n"
+ "text": [
+ "Malcolm III (1058-1093)\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "\n"
+ "text": [
+ "\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "\t1 . Kenneth II (971-995)\n\t2 . Constantine III (995-997)\n\t3 . Kenneth III (997-1005)\n\t4 . Malcolm II (1005-1034)\n\t5 . Duncan I (1034-1040)\n\t6 . Macbeth (1040-1057)\n\t7 . Lulach (1057-1058)\n\t8 . Malcolm III (1058-1093)\n"
+ "text": [
+ "\t1 . Kenneth II (971-995)\n",
+ "\t2 . Constantine III (995-997)\n",
+ "\t3 . Kenneth III (997-1005)\n",
+ "\t4 . Malcolm II (1005-1034)\n",
+ "\t5 . Duncan I (1034-1040)\n",
+ "\t6 . Macbeth (1040-1057)\n",
+ "\t7 . Lulach (1057-1058)\n",
+ "\t8 . Malcolm III (1058-1093)\n"
+ ]
}
],
"prompt_number": 2
@@ -150,7 +223,19 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 9.6 Capitalizing All the Words in a Text File\nHere is a complete program that reads words from the external file named input.txt, capitalizes\nthem, and then writes them to the external file named output.txt:\nNote : Working good , you need input.txt in same directory.\n'''\n\ninfile = open(\"input.txt\",\"r\")\noutfile = open(\"output.txt\",\"w\")\n\nfor i in infile:\n s = i.title()\n outfile.write(s)\n\ninfile.close()\noutfile.close()",
+ "input": [
+ " \n",
+ "\n",
+ "infile = open(\"input.txt\",\"r\")\n",
+ "outfile = open(\"output.txt\",\"w\")\n",
+ "\n",
+ "for i in infile:\n",
+ " s = i.title()\n",
+ " outfile.write(s)\n",
+ "\n",
+ "infile.close()\n",
+ "outfile.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -170,7 +255,38 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 9.7 Merging Two Sorted Data Files\n'''\n\nfin1 = open(\"north.dat\",\"r\")\nfin2 = open(\"south.dat\",\"r\")\nfout = open(\"combined.dat\",\"w\")\n\nfile1 = []\nfile2 = []\nfor i in fin1:\n try:\n s = i.split(\" \")\n for j in s:\n file1.append(int(j))\n except:\n continue\n \nfor i in fin2:\n try:\n s = i.split(\" \")\n for j in s:\n file2.append(int(j))\n except:\n continue\n\n\nfor i in sorted(file1 + file2):\n fout.write(str(i) + \" \")\n\nfin1.close()\nfin2.close()\nfout.close()",
+ "input": [
+ " \n",
+ "fin1 = open(\"north.dat\",\"r\")\n",
+ "fin2 = open(\"south.dat\",\"r\")\n",
+ "fout = open(\"combined.dat\",\"w\")\n",
+ "\n",
+ "file1 = []\n",
+ "file2 = []\n",
+ "for i in fin1:\n",
+ " try:\n",
+ " s = i.split(\" \")\n",
+ " for j in s:\n",
+ " file1.append(int(j))\n",
+ " except:\n",
+ " continue\n",
+ " \n",
+ "for i in fin2:\n",
+ " try:\n",
+ " s = i.split(\" \")\n",
+ " for j in s:\n",
+ " file2.append(int(j))\n",
+ " except:\n",
+ " continue\n",
+ "\n",
+ "\n",
+ "for i in sorted(file1 + file2):\n",
+ " fout.write(str(i) + \" \")\n",
+ "\n",
+ "fin1.close()\n",
+ "fin2.close()\n",
+ "fout.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -190,14 +306,36 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 9.8 Using an Output String Stream\nThis program creates four objects: a character string s,\nan integer n, a floating-point number x, and an output string stream oss:\n'''\n\ndef print_(oss):\n print 'oss.str() = \"' , str(oss) , '\"'\n\ns=\"ABCDEFG\"\nn=33\nx=2.718\nl = ''\nprint_(l)\nl += s\nprint_(l)\nl += ( \" \" + str(n) )\nprint_(l)\nl += ( \" \" + str(x) )\nprint_(l)",
+ "input": [
+ " \n",
+ "\n",
+ "def print_(oss):\n",
+ " print 'oss.str() = \"' , str(oss) , '\"'\n",
+ "\n",
+ "s=\"ABCDEFG\"\n",
+ "n=33\n",
+ "x=2.718\n",
+ "l = ''\n",
+ "print_(l)\n",
+ "l += s\n",
+ "print_(l)\n",
+ "l += ( \" \" + str(n) )\n",
+ "print_(l)\n",
+ "l += ( \" \" + str(x) )\n",
+ "print_(l)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "oss.str() = \" \"\noss.str() = \" ABCDEFG \"\noss.str() = \" ABCDEFG 33 \"\noss.str() = \" ABCDEFG 33 2.718 \"\n"
+ "text": [
+ "oss.str() = \" \"\n",
+ "oss.str() = \" ABCDEFG \"\n",
+ "oss.str() = \" ABCDEFG 33 \"\n",
+ "oss.str() = \" ABCDEFG 33 2.718 \"\n"
+ ]
}
],
"prompt_number": 5
@@ -205,14 +343,38 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 9.9 Using an Input String Stream iss ABCDEFG 44 3.14 istringstream\nThis program is similar to the one in Example 9.8 \nexcept that it reads from an input string stream iss\ninstead of writing to an output string stream.:\n'''\n'''\nEXAMPLE 9.8 Using an Output String Stream\nThis program creates four objects: a character string s,\nan integer n, a floating-point number x, and an output string stream oss:\n'''\n\ndef print_(iss,s='',n=0,x=0.0):\n print 's = \"' , s , '\", n = ' , n , \", x = \" , x, ', iss.str() = \"' \\\n , iss , '\"' \n\ns=\"\"\nn=0\nx=0.0\nl = ''\niss = \"ABCDEFG 44 3.14\"\nprint_(iss)\ns = \"ABCDEFG\"\nprint_(iss,s)\nn = 44\nprint_(iss,s,n)\nx = 3.14\nprint_(iss,s,n,x)",
+ "input": [
+ " \n",
+ "\n",
+ "def print_(iss,s='',n=0,x=0.0):\n",
+ " print 's = \"' , s , '\", n = ' , n , \", x = \" , x, ', iss.str() = \"' \\\n",
+ " , iss , '\"' \n",
+ "\n",
+ "s=\"\"\n",
+ "n=0\n",
+ "x=0.0\n",
+ "l = ''\n",
+ "iss = \"ABCDEFG 44 3.14\"\n",
+ "print_(iss)\n",
+ "s = \"ABCDEFG\"\n",
+ "print_(iss,s)\n",
+ "n = 44\n",
+ "print_(iss,s,n)\n",
+ "x = 3.14\n",
+ "print_(iss,s,n,x)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "s = \" \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 44 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 44 , x = 3.14 , iss.str() = \" ABCDEFG 44 3.14 \"\n"
+ "text": [
+ "s = \" \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n",
+ "s = \" ABCDEFG \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n",
+ "s = \" ABCDEFG \", n = 44 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\n",
+ "s = \" ABCDEFG \", n = 44 , x = 3.14 , iss.str() = \" ABCDEFG 44 3.14 \"\n"
+ ]
}
],
"prompt_number": 6
@@ -220,7 +382,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []