diff options
Diffstat (limited to '_Programming_With_C/chapter7.ipynb')
-rw-r--r-- | _Programming_With_C/chapter7.ipynb | 708 |
1 files changed, 0 insertions, 708 deletions
diff --git a/_Programming_With_C/chapter7.ipynb b/_Programming_With_C/chapter7.ipynb deleted file mode 100644 index 89247348..00000000 --- a/_Programming_With_C/chapter7.ipynb +++ /dev/null @@ -1,708 +0,0 @@ -{
- "metadata": {
- "name": ""
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h1>Chapter 7: Functions<h1>"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.1, Page number: 7.2<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def lower_to_upper(c1):\n",
- "\n",
- " if c1>='a' and c1<='z':\n",
- " c2=chr(ord('A')+ord(c1)-ord('a'))\n",
- "\n",
- " else:\n",
- " c2=c1\n",
- "\n",
- " return c2\n",
- "\n",
- "lower='g'\n",
- "upper=lower_to_upper(lower)\n",
- "print \"the upper case equivalent is : \",upper\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "the upper case equivalent is : G\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.3, Page number: 7.5<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def lower_to_upper(c1):\n",
- "\n",
- " if c1>='a' and c1<='z':\n",
- " return (chr(ord('A')+ord(c1)-ord('a')))\n",
- " else:\n",
- " return c1\n",
- "\n",
- "lower='g'\n",
- "upper=lower_to_upper(lower)\n",
- "print \"The uppercase equivalent is : \",upper\n",
- "\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The uppercase equivalent is : G\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.4, Page number: 7.5<h3> "
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def maximum(x,y):\n",
- " if x>=y:\n",
- " z=x\n",
- " else:\n",
- " z=y\n",
- "\n",
- " print \"Maximum value is : \",z\n",
- "\n",
- " return\n",
- "\n",
- "maximum(5,6)\n",
- "maximum(6,5)\n",
- "maximum(5,5)\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Maximum value is : 6\n",
- "Maximum value is : 6\n",
- "Maximum value is : 5\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.5, Page number: 7.5<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def factorial(n):\n",
- " prod=1\n",
- " if n>1:\n",
- " for i in range(2,n+1):\n",
- " prod=prod*i\n",
- "\n",
- " return prod\n",
- "\n",
- "fact=factorial(6)\n",
- "print \"Its factorial is : \",fact\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Its factorial is : 720\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.7, Page number: 7.7<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def maximum(x,y):\n",
- "\n",
- " if x>=y:\n",
- " z=x\n",
- " else:\n",
- " z=y\n",
- "\n",
- " print 'Maximum value is = ',z\n",
- " return\n",
- "\n",
- "maximum(5,9)\n",
- "maximum(8,3)\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Maximum value is = 9\n",
- "Maximum value is = 8\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.9, Page number: 7.8<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def maximum(x,y):\n",
- " if x>=y:\n",
- " z=x\n",
- " else:\n",
- " z=y\n",
- "\n",
- " return z\n",
- "\n",
- "a=5\n",
- "b=4\n",
- "c=7\n",
- "\n",
- "d=maximum(a,b)\n",
- "print \"Maximum = \",maximum(c,d)\n",
- "\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Maximum = 7\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.10, Page number: 7.10<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def factorial(n):\n",
- " prod=1\n",
- " if n>1:\n",
- " for i in range(2,n+1):\n",
- " prod=prod*i\n",
- "\n",
- " return prod\n",
- "\n",
- "n=7\n",
- "fact=factorial(n)\n",
- "print \"Its factorial is : \",fact\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Its factorial is : 5040\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.11, Page number: 7.12<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "\n",
- "def play():\n",
- " print \"Throwing the dice....\"\n",
- " score1=throw()\n",
- " print \"%2d\" %(score1)\n",
- "\n",
- " if score1==7 or score1==11:\n",
- " print \"Congratulations!! you WIN on the first throw\"\n",
- "\n",
- " elif score1==2 or score1==3 or score1==12:\n",
- " print \"sorry!! you LOSE on the first throw\"\n",
- "\n",
- " else:\n",
- " while(True):\n",
- " print \"Throwing the dice again...\"\n",
- " score2=throw()\n",
- " print \"%2d\" %(score2)\n",
- " if score2==score1 or score2==7:\n",
- " break\n",
- "\n",
- " if score2==score1:\n",
- " print \"You WIN by matching your first score\"\n",
- " else:\n",
- " print \"You LOSE by failing to match your first score\"\n",
- "\n",
- "\n",
- " return\n",
- "\n",
- "\n",
- "def throw():\n",
- "\n",
- " n1=random.randrange(1,7)\n",
- " n2=random.randrange(1,7)\n",
- " return n1+n2\n",
- "\n",
- "\n",
- "import random\n",
- "\n",
- "\n",
- "print \"Welcome to the Game of Craps \\n\\n\"\n",
- "random.seed(2365)\n",
- "play()\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Welcome to the Game of Craps \n",
- "\n",
- "\n",
- "Throwing the dice....\n",
- " 9\n",
- "Throwing the dice again...\n",
- " 4\n",
- "Throwing the dice again...\n",
- "11\n",
- "Throwing the dice again...\n",
- " 9\n",
- "You WIN by matching your first score\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.12, Page number: 7.18<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def modify(a):\n",
- " a=a*3\n",
- " print \"a= %d (from the function, after being modified)\" %(a)\n",
- " return\n",
- "\n",
- "a=2\n",
- "print \"a=%d (from main, before calling the function)\" %(a)\n",
- "modify(a)\n",
- "print \"a=%d (from main, after calling the function)\" %(a)\n",
- "\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "a=2 (from main, before calling the function)\n",
- "a= 6 (from the function, after being modified)\n",
- "a=2 (from main, after calling the function)\n"
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.13, Page number: 7.19<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def sl(val,n):\n",
- " deprec=val/n\n",
- " print \"in \",n\n",
- " \n",
- " for year in range(1,n+1):\n",
- " val=val-deprec\n",
- " writeoutput(year,deprec,val)\n",
- "\n",
- " return\n",
- "\n",
- "def ddb(val,n):\n",
- " for year in range(1,n+1):\n",
- " deprec=2*val/n\n",
- " val=val-deprec\n",
- " writeoutput(year,deprec,val)\n",
- "\n",
- " return\n",
- "\n",
- "def syd(val,n):\n",
- " tag=val\n",
- " for year in range(1,n+1):\n",
- " deprec=(n-year+1)*tag/(n*(n+1)/2)\n",
- " val=val-deprec\n",
- " writeoutput(year,deprec,val)\n",
- "\n",
- " return\n",
- "\n",
- "def writeoutput(year,depreciation,value):\n",
- " print \"End of the year %2d Depreciation: %7.2f Current Value: %8.2f\" %(year,depreciation,value)\n",
- "\n",
- " return\n",
- "\n",
- "\n",
- "\n",
- "def main(choice,val,n):\n",
- " \n",
- " print \"Original value : \",val\n",
- " val=float(val)\n",
- " print \"Number of years : \",n\n",
- "\n",
- " if choice==1:\n",
- " print \"Straight-Line Method\\n\\n\"\n",
- " sl(val,n)\n",
- " elif choice==2:\n",
- " print \"Double-Declining-Balance Method \\n\\n\"\n",
- " ddb(val,n)\n",
- " elif choice==3:\n",
- " print \"Sum-Of-The-Years'-Digits Method\"\n",
- " syd(val,n)\n",
- "\n",
- " return\n",
- "\n",
- "\n",
- "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
- "main(1,8000,10)\n",
- "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
- "main(2,8000,10)\n",
- "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
- "main(3,8000,10)\n",
- "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
- "main(1,5000,4)\n",
- "\n",
- "\n",
- " \n",
- " \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "\n",
- "Method: (1-SL 2-DDB 3-SYD)\n",
- "Original value : 8000\n",
- "Number of years : 10\n",
- "Straight-Line Method\n",
- "\n",
- "\n",
- "in 10\n",
- "End of the year 1 Depreciation: 800.00 Current Value: 7200.00\n",
- "End of the year 2 Depreciation: 800.00 Current Value: 6400.00\n",
- "End of the year 3 Depreciation: 800.00 Current Value: 5600.00\n",
- "End of the year 4 Depreciation: 800.00 Current Value: 4800.00\n",
- "End of the year 5 Depreciation: 800.00 Current Value: 4000.00\n",
- "End of the year 6 Depreciation: 800.00 Current Value: 3200.00\n",
- "End of the year 7 Depreciation: 800.00 Current Value: 2400.00\n",
- "End of the year 8 Depreciation: 800.00 Current Value: 1600.00\n",
- "End of the year 9 Depreciation: 800.00 Current Value: 800.00\n",
- "End of the year 10 Depreciation: 800.00 Current Value: 0.00\n",
- "\n",
- "\n",
- "Method: (1-SL 2-DDB 3-SYD)\n",
- "Original value : 8000\n",
- "Number of years : 10\n",
- "Double-Declining-Balance Method \n",
- "\n",
- "\n",
- "End of the year 1 Depreciation: 1600.00 Current Value: 6400.00\n",
- "End of the year 2 Depreciation: 1280.00 Current Value: 5120.00\n",
- "End of the year 3 Depreciation: 1024.00 Current Value: 4096.00\n",
- "End of the year 4 Depreciation: 819.20 Current Value: 3276.80\n",
- "End of the year 5 Depreciation: 655.36 Current Value: 2621.44\n",
- "End of the year 6 Depreciation: 524.29 Current Value: 2097.15\n",
- "End of the year 7 Depreciation: 419.43 Current Value: 1677.72\n",
- "End of the year 8 Depreciation: 335.54 Current Value: 1342.18\n",
- "End of the year 9 Depreciation: 268.44 Current Value: 1073.74\n",
- "End of the year 10 Depreciation: 214.75 Current Value: 858.99\n",
- "\n",
- "\n",
- "Method: (1-SL 2-DDB 3-SYD)\n",
- "Original value : 8000\n",
- "Number of years : 10\n",
- "Sum-Of-The-Years'-Digits Method\n",
- "End of the year 1 Depreciation: 1454.55 Current Value: 6545.45\n",
- "End of the year 2 Depreciation: 1309.09 Current Value: 5236.36\n",
- "End of the year 3 Depreciation: 1163.64 Current Value: 4072.73\n",
- "End of the year 4 Depreciation: 1018.18 Current Value: 3054.55\n",
- "End of the year 5 Depreciation: 872.73 Current Value: 2181.82\n",
- "End of the year 6 Depreciation: 727.27 Current Value: 1454.55\n",
- "End of the year 7 Depreciation: 581.82 Current Value: 872.73\n",
- "End of the year 8 Depreciation: 436.36 Current Value: 436.36\n",
- "End of the year 9 Depreciation: 290.91 Current Value: 145.45\n",
- "End of the year 10 Depreciation: 145.45 Current Value: 0.00\n",
- "\n",
- "\n",
- "Method: (1-SL 2-DDB 3-SYD)\n",
- "Original value : 5000\n",
- "Number of years : 4\n",
- "Straight-Line Method\n",
- "\n",
- "\n",
- "in 4\n",
- "End of the year 1 Depreciation: 1250.00 Current Value: 3750.00\n",
- "End of the year 2 Depreciation: 1250.00 Current Value: 2500.00\n",
- "End of the year 3 Depreciation: 1250.00 Current Value: 1250.00\n",
- "End of the year 4 Depreciation: 1250.00 Current Value: 0.00\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.14, Page number: 7.24<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def factorial(n):\n",
- "\n",
- " if n<=1:\n",
- " return 1\n",
- "\n",
- " else:\n",
- " return (n*factorial(n-1))\n",
- "\n",
- "\n",
- "n=10\n",
- "fact=factorial(n)\n",
- "print \"Its factorial is \",fact\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Its factorial is 3628800\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.15, Page number: 7.26<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "def reverse(text,n):\n",
- "\n",
- " if n<0:\n",
- " return\n",
- " else:\n",
- " print text[n],\n",
- " reverse(text,n-1)\n",
- "\n",
- "\n",
- "\n",
- "text='Now is the time for all good men to come to tje aid of their country!'\n",
- "n=len(text)\n",
- "reverse(text,n-1)\n",
- " \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "! y r t n u o c r i e h t f o d i a e j t o t e m o c o t n e m d o o g l l a r o f e m i t e h t s i w o N\n"
- ]
- }
- ],
- "prompt_number": 16
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 7.16, Page number: 7.16<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def transfer(n,From,to,temp):\n",
- "\n",
- " if n>0:\n",
- " transfer(n-1,From,temp,to)\n",
- " print \"Move disk %d from %c to %c\" %(n,From,to)\n",
- " transfer(n-1,temp,to,From)\n",
- "\n",
- " return\n",
- "\n",
- "print \"Welcome to the TOWERS OF HANOI \\n\\n\"\n",
- "transfer(3,'L','R','C')"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Welcome to the TOWERS OF HANOI \n",
- "\n",
- "\n",
- "Move disk 1 from L to R\n",
- "Move disk 2 from L to C\n",
- "Move disk 1 from R to C\n",
- "Move disk 3 from L to R\n",
- "Move disk 1 from C to L\n",
- "Move disk 2 from C to R\n",
- "Move disk 1 from L to R\n"
- ]
- }
- ],
- "prompt_number": 17
- }
- ],
- "metadata": {}
- }
- ]
-}
\ No newline at end of file |