summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb')
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb136
1 files changed, 136 insertions, 0 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb
new file mode 100644
index 00000000..827142fe
--- /dev/null
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb
@@ -0,0 +1,136 @@
+{
+ "metadata": {
+ "name": "ch13"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "44\n22\nintStack2 is empty.\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "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",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "8\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "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",
+ "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"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file