summaryrefslogtreecommitdiff
path: root/_Programming_With_C/chapter8.ipynb
diff options
context:
space:
mode:
Diffstat (limited to '_Programming_With_C/chapter8.ipynb')
-rw-r--r--_Programming_With_C/chapter8.ipynb555
1 files changed, 0 insertions, 555 deletions
diff --git a/_Programming_With_C/chapter8.ipynb b/_Programming_With_C/chapter8.ipynb
deleted file mode 100644
index e9a55558..00000000
--- a/_Programming_With_C/chapter8.ipynb
+++ /dev/null
@@ -1,555 +0,0 @@
-{
- "metadata": {
- "name": ""
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h1>Chapter 8: Program Structure <h1>"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.2, Page number: 8.2<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "def factorial(n):\n",
- "\t\n",
- "\tprod=1\n",
- "\tif n>1:\n",
- "\t\t\n",
- "\t\tfor i in xrange(2,n+1):\n",
- "\t\t\tprod*=i\n",
- "\t\t\n",
- "\t\n",
- "\treturn prod\n",
- "\t\n",
- "n=7\n",
- "print 'n!=%d' %factorial(n)\n",
- "\n",
- "\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "n!=5040\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.3, Page number: 8.3<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "def linecount(text):\n",
- "\t\n",
- "\treturn len(text)\n",
- "\n",
- "Sum=0\n",
- "lines=['Now is the time for all good men','to come to the aid of their country.']\n",
- "for i in lines:\n",
- "\tSum+=linecount(i)\n",
- "\n",
- "count=len(lines)\n",
- "count=float(count)\n",
- "avg=Sum/count\n",
- "\n",
- "print 'Average numbers of characters per line: ',avg\n",
- "\t\n",
- "\t"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Average numbers of characters per line: 34.0\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.4, Page number: 8.5<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "import math\n",
- "\n",
- "a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001\n",
- "\n",
- "def reduce():\n",
- "\n",
- "\tglobal x1,xr,y1,yr,a,b\n",
- "\t\n",
- "\tx1 = a+0.5*(b-a-CNST)\n",
- "\txr = x1 + CNST\n",
- "\ty1 = curve(x1)\n",
- "\tyr = curve(xr)\n",
- "\t\n",
- "\tif y1 > yr:\n",
- "\t\tb = xr\n",
- "\t\n",
- "\telif y1 < yr:\n",
- "\t\ta = x1\n",
- "\t\t\n",
- "\treturn\n",
- "\n",
- "def curve(x):\n",
- "\t\n",
- "\treturn x*math.cos(x)\n",
- "\t\n",
- "\t\n",
- "a = 0\n",
- "b = 3.141593\n",
- "\n",
- "while True:\n",
- "\treduce()\n",
- "\tif y1 == yr or b-a <= 3*CNST:\n",
- "\t\tbreak\n",
- "\t\t\n",
- "\n",
- "xmax = 0.5*(x1+xr)\n",
- "ymax = curve(xmax)\n",
- "print 'xmax = %8.6f ymax = %8.6f' %(xmax,ymax)\n",
- "\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "xmax = 0.860394 ymax = 0.561096\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.5, Page number: 8.9<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "def linecount(text):\n",
- "\t\n",
- "\tglobal Sum\n",
- "\tSum += len(text)\n",
- "\n",
- "Sum=0\n",
- "lines=['Now is the time for all good men','to come to the aid of their country.']\n",
- "for i in lines:\n",
- "\tlinecount(i)\n",
- "\n",
- "count=len(lines)\n",
- "count=float(count)\n",
- "avg=Sum/count\n",
- "\n",
- "print 'Average numbers of characters per line: ',avg\n",
- "\t\n",
- "\t\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Average numbers of characters per line: 34.0\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.7, Page number: 8.12<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "def fibonacci(count):\n",
- "\t\n",
- "\tif count<3:\n",
- "\t\tf=1\n",
- "\telse:\n",
- "\t\tf = fibonacci.f1 + fibonacci.f2\n",
- "\t\tfibonacci.f2 = fibonacci.f1\n",
- "\t\tfibonacci.f1 = f\n",
- "\t\t\n",
- "\treturn f\n",
- "\t\t\n",
- "fibonacci.f1,fibonacci.f2=1,1\n",
- "n=30\n",
- "for count in range(1,n+1):\n",
- "\t\n",
- "\tprint 'i = %2d F = %d' %(count,fibonacci(count))\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "i = 1 F = 1\n",
- "i = 2 F = 1\n",
- "i = 3 F = 2\n",
- "i = 4 F = 3\n",
- "i = 5 F = 5\n",
- "i = 6 F = 8\n",
- "i = 7 F = 13\n",
- "i = 8 F = 21\n",
- "i = 9 F = 34\n",
- "i = 10 F = 55\n",
- "i = 11 F = 89\n",
- "i = 12 F = 144\n",
- "i = 13 F = 233\n",
- "i = 14 F = 377\n",
- "i = 15 F = 610\n",
- "i = 16 F = 987\n",
- "i = 17 F = 1597\n",
- "i = 18 F = 2584\n",
- "i = 19 F = 4181\n",
- "i = 20 F = 6765\n",
- "i = 21 F = 10946\n",
- "i = 22 F = 17711\n",
- "i = 23 F = 28657\n",
- "i = 24 F = 46368\n",
- "i = 25 F = 75025\n",
- "i = 26 F = 121393\n",
- "i = 27 F = 196418\n",
- "i = 28 F = 317811\n",
- "i = 29 F = 514229\n",
- "i = 30 F = 832040\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3> Example 8.9, Page number: 8.18<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(563)\n",
- "play()\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Welcome to the Game of Craps \n",
- "\n",
- "\n",
- "Throwing the dice....\n",
- "10\n",
- "Throwing the dice again...\n",
- " 3\n",
- "Throwing the dice again...\n",
- "12\n",
- "Throwing the dice again...\n",
- " 9\n",
- "Throwing the dice again...\n",
- " 7\n",
- "You LOSE by failing to match your first score\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.11, Page Number: 8.21<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "import math\n",
- "\n",
- "a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001\n",
- "\n",
- "def reduce():\n",
- "\n",
- "\tglobal x1,xr,y1,yr,a,b\n",
- "\t\n",
- "\tx1 = a+0.5*(b-a-CNST)\n",
- "\txr = x1 + CNST\n",
- "\ty1 = curve(x1)\n",
- "\tyr = curve(xr)\n",
- "\t\n",
- "\tif y1 > yr:\n",
- "\t\tb = xr\n",
- "\t\n",
- "\telif y1 < yr:\n",
- "\t\ta = x1\n",
- "\t\t\n",
- "\treturn\n",
- "\n",
- "def curve(x):\n",
- "\t\n",
- "\treturn x*math.cos(x)\n",
- "\t\n",
- "\t\n",
- "a = 0\n",
- "b = 3.141593\n",
- "\n",
- "while True:\n",
- "\treduce()\n",
- "\tif y1 == yr or b-a <= 3*CNST:\n",
- "\t\tbreak\n",
- "\t\t\n",
- "\n",
- "xmax = 0.5*(x1+xr)\n",
- "ymax = curve(xmax)\n",
- "print 'xmax = %8.6f ymax = %8.6f' %(xmax,ymax)\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "xmax = 0.860394 ymax = 0.561096\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3> Example 8.12, Page Number: 8.23<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "def fibonacci(count):\n",
- "\t\n",
- "\tif count<3:\n",
- "\t\tf=1\n",
- "\telse:\n",
- "\t\tf = fibonacci.f1 + fibonacci.f2\n",
- "\t\tfibonacci.f2 = fibonacci.f1\n",
- "\t\tfibonacci.f1 = f\n",
- "\t\t\n",
- "\treturn f\n",
- "\t\t\n",
- "fibonacci.f1,fibonacci.f2=1,1\n",
- "n=40\n",
- "for count in range(1,n+1):\n",
- "\t\n",
- "\tprint 'i = %2d F = %d' %(count,fibonacci(count))\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "i = 1 F = 1\n",
- "i = 2 F = 1\n",
- "i = 3 F = 2\n",
- "i = 4 F = 3\n",
- "i = 5 F = 5\n",
- "i = 6 F = 8\n",
- "i = 7 F = 13\n",
- "i = 8 F = 21\n",
- "i = 9 F = 34\n",
- "i = 10 F = 55\n",
- "i = 11 F = 89\n",
- "i = 12 F = 144\n",
- "i = 13 F = 233\n",
- "i = 14 F = 377\n",
- "i = 15 F = 610\n",
- "i = 16 F = 987\n",
- "i = 17 F = 1597\n",
- "i = 18 F = 2584\n",
- "i = 19 F = 4181\n",
- "i = 20 F = 6765\n",
- "i = 21 F = 10946\n",
- "i = 22 F = 17711\n",
- "i = 23 F = 28657\n",
- "i = 24 F = 46368\n",
- "i = 25 F = 75025\n",
- "i = 26 F = 121393\n",
- "i = 27 F = 196418\n",
- "i = 28 F = 317811\n",
- "i = 29 F = 514229\n",
- "i = 30 F = 832040\n",
- "i = 31 F = 1346269\n",
- "i = 32 F = 2178309\n",
- "i = 33 F = 3524578\n",
- "i = 34 F = 5702887\n",
- "i = 35 F = 9227465\n",
- "i = 36 F = 14930352\n",
- "i = 37 F = 24157817\n",
- "i = 38 F = 39088169\n",
- "i = 39 F = 63245986\n",
- "i = 40 F = 102334155\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.13, Page Number: 8.25<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "import math\n",
- "\n",
- "p,r,n=10000,10,3\n",
- "i=r/100.0\n",
- "\n",
- "f=p*math.pow(1+i,n)\n",
- "print \"The final value (F) is: %.2f\" %f\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The final value (F) is: 13310.00\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [],
- "language": "python",
- "metadata": {},
- "outputs": []
- }
- ],
- "metadata": {}
- }
- ]
-} \ No newline at end of file