summaryrefslogtreecommitdiff
path: root/Programming_With_C/chapter6.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_With_C/chapter6.ipynb')
-rwxr-xr-xProgramming_With_C/chapter6.ipynb1297
1 files changed, 1297 insertions, 0 deletions
diff --git a/Programming_With_C/chapter6.ipynb b/Programming_With_C/chapter6.ipynb
new file mode 100755
index 00000000..5da628a1
--- /dev/null
+++ b/Programming_With_C/chapter6.ipynb
@@ -0,0 +1,1297 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 6: Control Statements<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.4, Page number: 6.3<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "\n",
+ "\n",
+ "inputs=[5,25,4,8]\n",
+ "Sum,SumSq,SumSqrt=0,0,0\n",
+ "\n",
+ "for x in inputs:\n",
+ "\n",
+ " Sum+=x\n",
+ " SumSq+=x*x\n",
+ " SumSqrt+=math.sqrt(x)\n",
+ "\n",
+ "\n",
+ "print \" Sum = %d \\n SumSq = %d \\n SumSqrt = %f\" %(Sum,SumSq,SumSqrt)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Sum = 42 \n",
+ " SumSq = 730 \n",
+ " SumSqrt = 12.064495\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.5, Page number: 6.4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def main(first,second):\n",
+ " \n",
+ " if first>second:\n",
+ " print \"first number is bigger\"\n",
+ " if second>first:\n",
+ " print \"second number is bigger\"\n",
+ " if first==second:\n",
+ " print \"both are equal\"\n",
+ " return\n",
+ "\n",
+ "main(5,6)\n",
+ "main(6,5)\n",
+ "main(1,1)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "second number is bigger\n",
+ "first number is bigger\n",
+ "both are equal\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.7, Page naumber: 6.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def main(time):\n",
+ "\n",
+ " if time>=0. and time<12.:\n",
+ " print \"Good Morning\"\n",
+ " elif time>=12. and time<18.:\n",
+ " print \"Good Afternoon\"\n",
+ " elif time>=18. and time<=24.:\n",
+ " print \"Good Evening\"\n",
+ " else:\n",
+ " print \"time is out of range\"\n",
+ " return\n",
+ "\n",
+ "main(9)\n",
+ "main(15)\n",
+ "main(21)\n",
+ "main(36)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Good Morning\n",
+ "Good Afternoon\n",
+ "Good Evening\n",
+ "time is out of range\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.8, Page number: 6.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "digit=0\n",
+ "while digit<=9:\n",
+ " print digit\n",
+ " digit=digit+1\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.9, Page number: 6.9<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "letter=\"Fourscore and seven years ago aou fathers brought forth...\"\n",
+ "letter=list(letter)\n",
+ "count=0\n",
+ "tag=len(letter)\n",
+ "\n",
+ "while count<tag:\n",
+ " letter[count]=letter[count].upper()\n",
+ " count=count+1\n",
+ "letter=''.join(letter)\n",
+ "print letter\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "FOURSCORE AND SEVEN YEARS AGO AOU FATHERS BROUGHT FORTH...\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.10, Page number: 6.11<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "Sum,count=0,0\n",
+ "\n",
+ "numbers=[1,2,3,4,5,6]\n",
+ "n=6.0\n",
+ "\n",
+ "while count<n:\n",
+ " x=numbers[count]\n",
+ " print \"x = \",x\n",
+ " Sum+=x\n",
+ " count+=1\n",
+ "\n",
+ "average=Sum/n\n",
+ "\n",
+ "print 'Average = ',average\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1\n",
+ "x = 2\n",
+ "x = 3\n",
+ "x = 4\n",
+ "x = 5\n",
+ "x = 6\n",
+ "Average = 3.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1\n",
+ "x = 2\n",
+ "x = 3\n",
+ "x = 4\n",
+ "x = 5\n",
+ "x = 6\n",
+ "Average = 3.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.11, Page number: 6.12<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "digit=0\n",
+ "while True:\n",
+ " print digit\n",
+ " digit=digit+1\n",
+ " if digit>9:\n",
+ " break\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.12, Page number: 6.13<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "letter=\"Fourscore and seven years ago aou fathers brought forth...\"\n",
+ "letter=list(letter)\n",
+ "\n",
+ "count=0\n",
+ "tag=len(letter)\n",
+ "\n",
+ "while True:\n",
+ " letter[count]=letter[count].upper()\n",
+ " count=count+1\n",
+ " if count>=tag:\n",
+ " break\n",
+ "\n",
+ "letter=''.join(letter)\n",
+ "print letter\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "FOURSCORE AND SEVEN YEARS AGO AOU FATHERS BROUGHT FORTH...\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.13, Page number: 6.14<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "Sum,count=0,0\n",
+ "\n",
+ "numbers=[1,2,3,4,5,6]\n",
+ "n=6.0\n",
+ "\n",
+ "while True:\n",
+ " x=numbers[count]\n",
+ " print \"x = \",x\n",
+ " Sum+=x\n",
+ " count+=1\n",
+ " if count>=n:\n",
+ " break\n",
+ "\n",
+ "average=Sum/n\n",
+ "\n",
+ "print 'Average = ',average\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1\n",
+ "x = 2\n",
+ "x = 3\n",
+ "x = 4\n",
+ "x = 5\n",
+ "x = 6\n",
+ "Average = 3.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.14, Page number: 6.16<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "for digit in range(0,10):\n",
+ " print digit\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.16, Page number: 6.17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "letter=\"Fourscore and seven years ago aou fathers brought forth...\"\n",
+ "letter=list(letter)\n",
+ "n=len(letter)\n",
+ "\n",
+ "for count in range(0,n):\n",
+ " \n",
+ " letter[count]=letter[count].upper()\n",
+ "\n",
+ "letter=''.join(letter)\n",
+ "print letter\n",
+ "\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "FOURSCORE AND SEVEN YEARS AGO AOU FATHERS BROUGHT FORTH...\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.17, Page number: 6.17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n=6\n",
+ "Sum=0.0\n",
+ "\n",
+ "for count in range(1,n+1):\n",
+ " Sum=Sum+count\n",
+ " \n",
+ "average=Sum/n\n",
+ "print \"the average is \",average\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "the average is 3.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.18, Page number: 6.19<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "numbers=[[1.5,2.5,6.2,3.0],[4,-2,7],[5.4,8.0,2.2,1.7,-3.9]]\n",
+ "loopcount=3\n",
+ "loop=0\n",
+ "\n",
+ "while loop<loopcount:\n",
+ "\n",
+ " Sum=0\n",
+ "\n",
+ " for x in numbers[loop]:\n",
+ "\n",
+ " print 'x = ',x\n",
+ " Sum+=x\n",
+ "\n",
+ " print 'The average is ',Sum/len(numbers[loop])\n",
+ " loop+=1\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1.5\n",
+ "x = 2.5\n",
+ "x = 6.2\n",
+ "x = 3.0\n",
+ "The average is 3.3\n",
+ "x = 4\n",
+ "x = -2\n",
+ "x = 7\n",
+ "The average is 3\n",
+ "x = 5.4\n",
+ "x = 8.0\n",
+ "x = 2.2\n",
+ "x = 1.7\n",
+ "x = -3.9\n",
+ "The average is 2.68\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.19, Page number: 6.20<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "text=['Now is the time for all good men to come to aid..','Fourscore and seven years ago our fathers brought forth...','*']\n",
+ "\n",
+ "\n",
+ "loop=0\n",
+ "\n",
+ "while True:\n",
+ " letter=text[loop]\n",
+ " if letter=='*':\n",
+ " print \"\\nGood bye\"\n",
+ " break\n",
+ "\n",
+ " print '\\n',letter\n",
+ " letter=list(letter)\n",
+ " n=len(letter)\n",
+ "\n",
+ " for count in range(0,n):\n",
+ " letter[count]=letter[count].upper()\n",
+ "\n",
+ " letter=''.join(letter)\n",
+ " print letter\n",
+ " loop+=1\n",
+ " \n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Now is the time for all good men to come to aid..\n",
+ "NOW IS THE TIME FOR ALL GOOD MEN TO COME TO AID..\n",
+ "\n",
+ "Fourscore and seven years ago our fathers brought forth...\n",
+ "FOURSCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH...\n",
+ "\n",
+ "Good bye\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.20, Page number: 6.22<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "line=\"The White House. 1600 Pennsylvania Avenue. Washington. DC\"\n",
+ "line=list(line)\n",
+ "line2=[]\n",
+ "for count in line:\n",
+ " if (count>='0' and count<'9') or \\\n",
+ " (count>='a' and count<'z') or \\\n",
+ " (count>='A' and count<'Z'):\n",
+ "\n",
+ " line2.extend(chr(ord(count)+1))\n",
+ "\n",
+ " elif count=='9':\n",
+ " line2.extend('0')\n",
+ " elif count=='z':\n",
+ " line2.extend('a')\n",
+ " elif count=='Z':\n",
+ " line2.extend('A')\n",
+ " else:\n",
+ " line2.extend('.')\n",
+ "\n",
+ "line2=''.join(line2)\n",
+ "print line2\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Uif.Xijuf.Ipvtf..2711.Qfootzmwbojb.Bwfovf..Xbtijohupo..ED\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.21, Page number: 6.23<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def main(p,r,n):\n",
+ " \n",
+ "\n",
+ " if p<0 or r<0 or n<0:\n",
+ " print \"ERROR! Wrong input\"\n",
+ " return\n",
+ " \n",
+ "\n",
+ " i=r/100.0\n",
+ " f=p*((i+1)**n)\n",
+ "\n",
+ " print \"The Final value (F) is %.2f\" %f\n",
+ "\n",
+ " return\n",
+ "main(1000,6,20)\n",
+ "main(5000,-7.5,12)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Final value (F) is 3207.14\n",
+ "ERROR! Wrong input\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.22, Page number: 6.25<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "def main(guess):\n",
+ " count=0\n",
+ " flag=True\n",
+ "\n",
+ " while flag:\n",
+ " count=count+1\n",
+ " if count==50:\n",
+ " flag=False\n",
+ "\n",
+ " test=10.-3.*guess*guess\n",
+ " if test>0:\n",
+ " root=test**.2\n",
+ " print \"Iteration number : %2d x= %7.5f\" %(count,root)\n",
+ " error=math.fabs(root-guess)\n",
+ " if error>0.00001:\n",
+ " guess=root\n",
+ " else:\n",
+ " flag=False\n",
+ " print \"\\n\\n root= %7.5f number of iterations %2d\" %(root,count)\n",
+ "\n",
+ " else:\n",
+ " flag=False\n",
+ " print \"number out of range... try another initial guess\"\n",
+ "\n",
+ "\n",
+ "\n",
+ " if count==50 and error>0.00001:\n",
+ " print \"convergence not obtained after 50 iterations\"\n",
+ " \n",
+ " return\n",
+ "\n",
+ "print \"initial guess: 1\"\n",
+ "main(1)\n",
+ "print \"\\ninitial guess: 10\"\n",
+ "main(10)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "initial guess: 1\n",
+ "Iteration number : 1 x= 1.47577\n",
+ "Iteration number : 2 x= 1.28225\n",
+ "Iteration number : 3 x= 1.38344\n",
+ "Iteration number : 4 x= 1.33613\n",
+ "Iteration number : 5 x= 1.35951\n",
+ "Iteration number : 6 x= 1.34826\n",
+ "Iteration number : 7 x= 1.35375\n",
+ "Iteration number : 8 x= 1.35109\n",
+ "Iteration number : 9 x= 1.35238\n",
+ "Iteration number : 10 x= 1.35175\n",
+ "Iteration number : 11 x= 1.35206\n",
+ "Iteration number : 12 x= 1.35191\n",
+ "Iteration number : 13 x= 1.35198\n",
+ "Iteration number : 14 x= 1.35195\n",
+ "Iteration number : 15 x= 1.35196\n",
+ "Iteration number : 16 x= 1.35195\n",
+ "\n",
+ "\n",
+ " root= 1.35195 number of iterations 16\n",
+ "\n",
+ "initial guess: 10\n",
+ "number out of range... try another initial guess\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.23, Page number: 6.30<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def main(ch):\n",
+ "\n",
+ " if ch=='r' or ch=='R':\n",
+ " print \"RED\"\n",
+ "\n",
+ " elif ch=='w' or ch=='W':\n",
+ " print \"WHITE\"\n",
+ "\n",
+ " elif ch=='b' or ch=='B':\n",
+ " print \"BLUE\"\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main('r')\n",
+ "main('W')\n",
+ "main('b')\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RED\n",
+ "WHITE\n",
+ "BLUE\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.24, Page number: 6.31<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def main(ch):\n",
+ " ch=ch.upper()\n",
+ "\n",
+ " if ch=='R':\n",
+ " print \"RED\"\n",
+ "\n",
+ " elif ch=='W':\n",
+ " print \"WHITE\"\n",
+ "\n",
+ " elif ch=='B':\n",
+ " print \"BLUE\"\n",
+ "\n",
+ " else:\n",
+ " print \"ERROR\"\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main('r')\n",
+ "main('W')\n",
+ "main('b')\n",
+ "main('y')\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RED\n",
+ "WHITE\n",
+ "BLUE\n",
+ "ERROR\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.25, Page number: 6.31<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import math\n",
+ "\n",
+ "def main(x,flag):\n",
+ "\n",
+ " if flag==-1:\n",
+ " y=math.fabs(x)\n",
+ " elif flag==0:\n",
+ " y=x**0.5\n",
+ " elif flag==1:\n",
+ " y=x\n",
+ " elif flag==2 or flag==3:\n",
+ " y=2*(x-1)\n",
+ " else:\n",
+ " y=0\n",
+ " print \"y= \",y\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main(36,-1)\n",
+ "main(36,0)\n",
+ "main(36,1)\n",
+ "main(36,2)\n",
+ "main(36,3)\n",
+ "main(36,5)\n",
+ "main(0,6)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y= 36.0\n",
+ "y= 6.0\n",
+ "y= 36\n",
+ "y= 70\n",
+ "y= 70\n",
+ "y= 0\n",
+ "y= 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.26, Page number: 6.32<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "choice=0\n",
+ "\n",
+ "def main(choice,val,n):\n",
+ " \n",
+ " if choice==1:\n",
+ "\n",
+ " print \"Straight Line Method\\n\\n\"\n",
+ " val=float(val)\n",
+ " deprec=val/n\n",
+ " for year in range(1,n+1):\n",
+ " val=val-deprec\n",
+ " print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
+ " %(year,deprec,val)\n",
+ "\n",
+ "\n",
+ " elif choice==2:\n",
+ " print \"Double Declining Balance Method\\n\\n\"\n",
+ " val=float(val)\n",
+ " for year in range(1,n+1):\n",
+ " deprec=2*val/n\n",
+ " val=val-deprec\n",
+ " print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
+ " %(year,deprec,val)\n",
+ "\n",
+ " elif choice==3:\n",
+ " print \"Sum of the years' -Digit Method\\n\\n\"\n",
+ " val=float(val)\n",
+ " tag=val\n",
+ "\n",
+ " for year in range(1,n+1):\n",
+ " deprec=(n-year+1)*tag/(n*(n+1)/2)\n",
+ " val=val-deprec\n",
+ " print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
+ " %(year,deprec,val)\n",
+ "\n",
+ " else:\n",
+ " print \"incorrect data entry...\"\n",
+ "\n",
+ " return\n",
+ "\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 1 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(1,8000,10)\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 2 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(2,8000,10)\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 3 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(3,8000,10)\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 5 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(5,8000,10)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 1 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "Straight Line Method\n",
+ "\n",
+ "\n",
+ "End of year 1 Depreciation: 800.00 Current value: 7200.00\n",
+ "End of year 2 Depreciation: 800.00 Current value: 6400.00\n",
+ "End of year 3 Depreciation: 800.00 Current value: 5600.00\n",
+ "End of year 4 Depreciation: 800.00 Current value: 4800.00\n",
+ "End of year 5 Depreciation: 800.00 Current value: 4000.00\n",
+ "End of year 6 Depreciation: 800.00 Current value: 3200.00\n",
+ "End of year 7 Depreciation: 800.00 Current value: 2400.00\n",
+ "End of year 8 Depreciation: 800.00 Current value: 1600.00\n",
+ "End of year 9 Depreciation: 800.00 Current value: 800.00\n",
+ "End of year 10 Depreciation: 800.00 Current value: 0.00\n",
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 2 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "Double Declining Balance Method\n",
+ "\n",
+ "\n",
+ "End of year 1 Depreciation: 1600.00 Current value: 6400.00\n",
+ "End of year 2 Depreciation: 1280.00 Current value: 5120.00\n",
+ "End of year 3 Depreciation: 1024.00 Current value: 4096.00\n",
+ "End of year 4 Depreciation: 819.20 Current value: 3276.80\n",
+ "End of year 5 Depreciation: 655.36 Current value: 2621.44\n",
+ "End of year 6 Depreciation: 524.29 Current value: 2097.15\n",
+ "End of year 7 Depreciation: 419.43 Current value: 1677.72\n",
+ "End of year 8 Depreciation: 335.54 Current value: 1342.18\n",
+ "End of year 9 Depreciation: 268.44 Current value: 1073.74\n",
+ "End of year 10 Depreciation: 214.75 Current value: 858.99\n",
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 3 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "Sum of the years' -Digit Method\n",
+ "\n",
+ "\n",
+ "End of year 1 Depreciation: 1454.55 Current value: 6545.45\n",
+ "End of year 2 Depreciation: 1309.09 Current value: 5236.36\n",
+ "End of year 3 Depreciation: 1163.64 Current value: 4072.73\n",
+ "End of year 4 Depreciation: 1018.18 Current value: 3054.55\n",
+ "End of year 5 Depreciation: 872.73 Current value: 2181.82\n",
+ "End of year 6 Depreciation: 727.27 Current value: 1454.55\n",
+ "End of year 7 Depreciation: 581.82 Current value: 872.73\n",
+ "End of year 8 Depreciation: 436.36 Current value: 436.36\n",
+ "End of year 9 Depreciation: 290.91 Current value: 145.45\n",
+ "End of year 10 Depreciation: 145.45 Current value: 0.00\n",
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 5 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "incorrect data entry...\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.31, Page number: 6.41<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "navg=0\n",
+ "Sum=0.0\n",
+ "n=6\n",
+ "\n",
+ "for count in range(-6,n+1):\n",
+ " x=count\n",
+ " if(x<0):\n",
+ " continue\n",
+ " Sum=Sum+x\n",
+ " navg=navg+1\n",
+ "\n",
+ "\n",
+ "average=Sum/navg\n",
+ "print \"The average is \",average\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The average is 3.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.32, Page number: 6.42<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "def main(letter):\n",
+ " flag=True\n",
+ "\n",
+ " letter=list(letter)\n",
+ "\n",
+ " tag=len(letter)-1\n",
+ " countback=tag\n",
+ "\n",
+ " for count in range(0,(tag/2)+1):\n",
+ " if letter[count]!=letter[countback]:\n",
+ " flag=False\n",
+ " break\n",
+ " countback=countback-1\n",
+ "\n",
+ " letter=''.join(letter)\n",
+ " if flag:\n",
+ " print \"\\n %s IS a palindrome\" %letter\n",
+ " else:\n",
+ " print \"\\n %s is NOT a palindrome\" %letter\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main('TOOT')\n",
+ "main('FALSE')\n",
+ "main('PULLUP')\n",
+ "main('ABLE WAS I ERE I SAW ELBA')\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " TOOT IS a palindrome\n",
+ "\n",
+ " FALSE is NOT a palindrome\n",
+ "\n",
+ " PULLUP IS a palindrome\n",
+ "\n",
+ " ABLE WAS I ERE I SAW ELBA IS a palindrome\n"
+ ]
+ }
+ ],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.34, Page number: 6.47<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "text=['Now is the time for all good men to come to aid..','Fourscore and seven years ago our fathers brought forth.$$..','*']\n",
+ "\n",
+ "\n",
+ "loop,flag=0,False\n",
+ "\n",
+ "while True:\n",
+ " letter=text[loop]\n",
+ " if letter=='*' or flag:\n",
+ " print \"\\nGood bye\"\n",
+ " break\n",
+ "\n",
+ " print '\\n',letter\n",
+ " letter=list(letter)\n",
+ " n=len(letter)\n",
+ "\n",
+ " for count in range(0,n):\n",
+ " letter[count]=letter[count].upper()\n",
+ "\n",
+ " letter2=''.join(letter)\n",
+ " print letter2\n",
+ "\n",
+ " for count in range(0,n-1):\n",
+ " if letter[count]=='$' and letter[count+1]=='$':\n",
+ " print 'BREAK CONDITION DETECTED - TERMINATE EXECUTION'\n",
+ " flag=True\n",
+ " break\n",
+ " \n",
+ " loop+=1\n",
+ " \n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Now is the time for all good men to come to aid..\n",
+ "NOW IS THE TIME FOR ALL GOOD MEN TO COME TO AID..\n",
+ "\n",
+ "Fourscore and seven years ago our fathers brought forth.$$..\n",
+ "FOURSCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH.$$..\n",
+ "BREAK CONDITION DETECTED - TERMINATE EXECUTION\n",
+ "\n",
+ "Good bye\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file