diff options
Diffstat (limited to 'Beginning_C_By_Ivon_Horton/chapter4.ipynb')
-rw-r--r-- | Beginning_C_By_Ivon_Horton/chapter4.ipynb | 954 |
1 files changed, 954 insertions, 0 deletions
diff --git a/Beginning_C_By_Ivon_Horton/chapter4.ipynb b/Beginning_C_By_Ivon_Horton/chapter4.ipynb new file mode 100644 index 00000000..1dd29164 --- /dev/null +++ b/Beginning_C_By_Ivon_Horton/chapter4.ipynb @@ -0,0 +1,954 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Loops" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.1, page no. 139" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "List ten integers\n", + "\"\"\"\n", + "\n", + "for count in range (1, 11):\n", + " print count,\n", + "print \"\\nAfter the loop count has the value %d.\" %count" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3 4 5 6 7 8 9 10 \n", + "After the loop count has the value 10.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.2, page no. 140" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Drawing a box\n", + "\"\"\"\n", + "\n", + "print \"**************\"\n", + "for count in range(1, 9):\n", + " print \"* *\"\n", + "print \"**************\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "**************\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "**************\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.3, page no. 144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Sum the integers from 1 to a user-specified number\n", + "\"\"\"\n", + "\n", + "sum = 0\n", + "\n", + "print \"Enter the number of integers you want to sum: \",\n", + "count = int(raw_input())\n", + " \n", + "for i in range(1, count+1):\n", + " sum = sum + i\n", + " \n", + "print \"Total of the first %d numbers is %d\" %(count, sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of integers you want to sum: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total of the first 10 numbers is 55\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.4, page no. 144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Sum the integers from 1 to a user-specified number (compact version)\n", + "note: the way specified in book is not possible in Python, it will be same as\n", + "previous progam\n", + "\"\"\"\n", + "\n", + "sum = 0\n", + "\n", + "print \"Enter the number of integers you want to sum: \",\n", + "count = int(raw_input())\n", + " \n", + "for i in range(1, count+1):\n", + " sum = sum + i\n", + " \n", + "print \"Total of the first %d numbers is %d\" %(count, sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of integers you want to sum: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total of the first 10 numbers is 55\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.5, page no. 147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "summing the integers backward\n", + "\"\"\"\n", + "\n", + "sum = 0\n", + "\n", + "print \"Enter the number of integers you want to sum: \",\n", + "count = int(raw_input())\n", + "for i in range(count, 0, -1):\n", + " sum = sum + i\n", + "print \"Total of the first %d numbers is %d\" %(count, sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of integers you want to sum: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total of the first 10 numbers is 55\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.6, page no. 148" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "The almost indefinite loop - computing an average\n", + "note: there cannot be infinit for loop in python, we will use while loop\n", + "\"\"\"\n", + "\n", + "print \"This program calculates the average of any number of values.\"\n", + "total = 0\n", + "count = 0\n", + "while True: # infinite loop\n", + " print \"\\nEnter a value: \",\n", + " value = float(raw_input())\n", + " total += value\n", + " count += 1\n", + " \n", + " print \"Do you want to enter another value? (Y or N): \", \n", + " answer = raw_input()\n", + " if(answer.lower() == 'n'):\n", + " break\n", + "\n", + "print \"The average is %.2f\" %(total/count)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This program calculates the average of any number of values.\n", + "\n", + "Enter a value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another value? (Y or N): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter a value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another value? (Y or N): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Enter a value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another value? (Y or N): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The average is 8.00\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.7, page no. 151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "A Guessing Game\n", + "\"\"\"\n", + "import sys\n", + "\n", + "chosen = 15\n", + "guess = 0\n", + "count = 3\n", + " \n", + "print \"This is a guessing game.\"\n", + "print \"I have chosen a number between 1 and 20 which you must guess.\"\n", + "\n", + "for i in range(count, 0, -1):\n", + " print \"You have %d tr%s left.\" %(i, (\"y\" if i == 1 else \"ies\"));\n", + " print \"\\nEnter a guess: \",\n", + " guess = int(raw_input())\n", + " if(guess == chosen):\n", + " print \"Congratulations. You guessed it!\"\n", + " sys.exit()\n", + " elif(guess < 1 or guess > 20):\n", + " print \"I said the number is between 1 and 20.\"\n", + " else:\n", + " print \"Sorry, %d is wrong. My number is %s than that.\\n\" %(guess, (\"greater\" if chosen > guess else \"less\"))\n", + "print \"You have had three tries and failed. The number was %d\" % chosen" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is a guessing game.\n", + "I have chosen a number between 1 and 20 which you must guess.\n", + "You have 3 tries left.\n", + "\n", + "Enter a guess: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Sorry, 5 is wrong. My number is greater than that.\n", + "\n", + "You have 2 tries left.\n", + "\n", + "Enter a guess: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Sorry, 9 is wrong. My number is greater than that.\n", + "\n", + "You have 1 try left.\n", + "\n", + "Enter a guess: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Sorry, 14 is wrong. My number is greater than that.\n", + "\n", + "You have had three tries and failed. The number was 15\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.7A, page no. 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "A More Interesting Guessing Game\n", + "\"\"\"\n", + "\n", + "from random import *\n", + "\n", + "guess = 0\n", + "count = 3\n", + "limit = 20\n", + "\n", + "chosen = 1 + randint(1, limit) #Random int 1 to limit\n", + " \n", + "print \"This is a guessing game.\"\n", + "print \"I have chosen a number between 1 and 20 which you must guess.\"\n", + "\n", + "for i in range(count, 0, -1):\n", + " print \"You have %d tr%s left.\" %(i, (\"y\" if i == 1 else \"ies\"));\n", + " print \"Enter a guess: \",\n", + " guess = int(raw_input())\n", + " if(guess == chosen):\n", + " print \"Congratulations. You guessed it!\"\n", + " sys.exit()\n", + " elif(guess < 1 or guess > 20):\n", + " print \"I said the number is between 1 and 20.\"\n", + " else:\n", + " print \"Sorry, %d is wrong. My number is %s than that.\\n\" %(guess, (\"greater\" if chosen > guess else \"less\"))\n", + "print \"You have had three tries and failed. The number was %d\" % chosen" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is a guessing game.\n", + "I have chosen a number between 1 and 20 which you must guess.\n", + "You have 3 tries left.\n", + "Enter a guess: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Sorry, 6 is wrong. My number is greater than that.\n", + "\n", + "You have 2 tries left.\n", + "Enter a guess: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Sorry, 14 is wrong. My number is greater than that.\n", + "\n", + "You have 1 try left.\n", + "Enter a guess: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "16\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Sorry, 16 is wrong. My number is greater than that.\n", + "\n", + "You have had three tries and failed. The number was 20\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.8, page no. 158" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "While programming and summing integers\n", + "\"\"\"\n", + "\n", + "sum = 0\n", + "i = 1\n", + "\n", + "print \"Enter the number of integers you want to sum: \",\n", + "count = int(raw_input())\n", + " \n", + "while(i <= count):\n", + " sum += i\n", + " i += 1\n", + " \n", + "print \"Total of the first %d numbers is %d\" %(count, sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of integers you want to sum: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total of the first 10 numbers is 55\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.9, page no. 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Output a box with given width and height\n", + "\"\"\"\n", + "\n", + "MIN_SIZE = 3;\n", + " \n", + "print \"Enter values for the width and height (minimum of %d): \" % MIN_SIZE\n", + "width = int(raw_input(\"Width: \"))\n", + "height = int(raw_input(\"Height: \"))\n", + " \n", + "if(width < MIN_SIZE):\n", + " print \"Width value of %d is too small. Setting it to %d.\" %(width, MIN_SIZE)\n", + " width = MIN_SIZE\n", + "if(height < MIN_SIZE):\n", + " print \"Height value of %d is too small. Setting it to %d.\" %(height, MIN_SIZE)\n", + " height = MIN_SIZE\n", + "\n", + "\n", + "for i in range(0, width):\n", + " print \"*\",\n", + "print \"\\n\"\n", + "for j in range(0, height-2):\n", + " print \"*\",\n", + " for i in range(0, width-2):\n", + " print \" \",\n", + " print \"*\"\n", + "\n", + "print \"\\n\"\n", + "for i in range(0, width):\n", + " print \"*\"," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter values for the width and height (minimum of 3): \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Width: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height: 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "* * * * * * * * * * * * * * * * * * * * * * * * \n", + "\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "\n", + "\n", + "* * * * * * * * * * * * * * * * * * * * * * * *\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.10, page no. 162" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Sums of successive integer sequences\n", + "\"\"\"\n", + "\n", + "sum = 0\n", + "count = 0\n", + "\n", + "print \"Enter the number of integers you want to sum: \",\n", + "count = int(raw_input())\n", + " \n", + "for i in range(1, count+1):\n", + " sum = 0\n", + " for j in range(1, i+1):\n", + " sum += j\n", + " print \"%d\\t%5d\" %(i, sum)\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of integers you want to sum: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1\t 1\n", + "2\t 3\n", + "3\t 6\n", + "4\t 10\n", + "5\t 15\n", + "6\t 21\n", + "7\t 28\n", + "8\t 36\n", + "9\t 45\n", + "10\t 55\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.11, page no. 164" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Sums of integers with a while loop nested in a for loop\n", + "\"\"\"\n", + "\n", + "print \"Enter the number of integers you want to sum: \",\n", + "count = int(raw_input())\n", + "\n", + "for i in range(1, count+1):\n", + " sum = 1\n", + " j = 1\n", + " print \"1\",\n", + " while(j < i):\n", + " j += 1\n", + " sum += j\n", + " print \"+ %d\" % j,\n", + " print \"= %d\" % sum\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of integers you want to sum: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1 = 1\n", + "1 + 2 = 3\n", + "1 + 2 + 3 = 6\n", + "1 + 2 + 3 + 4 = 10\n", + "1 + 2 + 3 + 4 + 5 = 15\n", + "1 + 2 + 3 + 4 + 5 + 6 = 21\n", + "1 + 2 + 3 + 4 + 5 + 6 + 7 = 28\n", + "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36\n", + "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45\n", + "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 4.12, page no. 167" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Reversing the digits\n", + "\"\"\"\n", + "rebmun = 0\n", + "print \"\\nEnter a positive integer: \",\n", + "number = int(raw_input())\n", + "temp = number\n", + "while(temp):\n", + " rebmun = 10*rebmun + temp % 10\n", + " temp = temp/10\n", + "\n", + "print \"The number %d reversed is %d rebmun ehT\" %(number, rebmun)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter a positive integer: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number 7 reversed is 7 rebmun ehT\n" + ] + } + ], + "prompt_number": 20 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |