diff options
author | Thomas Stephen Lee | 2015-09-04 22:04:10 +0530 |
---|---|---|
committer | Thomas Stephen Lee | 2015-09-04 22:04:10 +0530 |
commit | 41f1f72e9502f5c3de6ca16b303803dfcf1df594 (patch) | |
tree | f4bf726a3e3ce5d7d9ee3781cbacfe3116115a2c /The_Spirit_of_C | |
parent | 9c9779ba21b9bedde88e1e8216f9e3b4f8650b0e (diff) | |
download | Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.tar.gz Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.tar.bz2 Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.zip |
add/remove/update books
Diffstat (limited to 'The_Spirit_of_C')
-rwxr-xr-x | The_Spirit_of_C/Chapter1.ipynb | 198 | ||||
-rwxr-xr-x | The_Spirit_of_C/Chapter10.ipynb | 1020 | ||||
-rwxr-xr-x | The_Spirit_of_C/Chapter11.ipynb | 383 | ||||
-rwxr-xr-x | The_Spirit_of_C/Chapter12.ipynb | 455 | ||||
-rwxr-xr-x | The_Spirit_of_C/Chapter13.ipynb | 346 | ||||
-rwxr-xr-x | The_Spirit_of_C/Chapter14.ipynb | 298 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter2.ipynb | 119 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter3.ipynb | 241 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter4.ipynb | 148 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter5.ipynb | 218 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter6.ipynb | 494 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter7.ipynb | 690 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter8.ipynb | 567 | ||||
-rwxr-xr-x | The_Spirit_of_C/chapter9.ipynb | 590 | ||||
-rwxr-xr-x | The_Spirit_of_C/screenshots/Screenshot_1.png | bin | 414085 -> 0 bytes | |||
-rwxr-xr-x | The_Spirit_of_C/screenshots/Screenshot_2.png | bin | 319968 -> 0 bytes | |||
-rwxr-xr-x | The_Spirit_of_C/screenshots/Screenshot_3.png | bin | 211398 -> 0 bytes |
17 files changed, 0 insertions, 5767 deletions
diff --git a/The_Spirit_of_C/Chapter1.ipynb b/The_Spirit_of_C/Chapter1.ipynb deleted file mode 100755 index 80b53e61..00000000 --- a/The_Spirit_of_C/Chapter1.ipynb +++ /dev/null @@ -1,198 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:1c2e6d0b5736bd35dd626e4e22d0dbe67f82011189ddb91d07585ae3f73aa6b8" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 1, Elementary Programming" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 1-1 , Page number: 2" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An interactive program to calculate the hypotenuse of a right triangle\n", - "\n", - "import math ## library for using sqrt function\n", - "\n", - "def square(a):\n", - "\t\"\"\"Returns the square of the given number \"\"\"\n", - "\treturn a*a\n", - "\n", - "print \"Enter the lengths of the two legs of a right triangle\"\n", - "print \"The program will display the length of the hypotenuse.\"\n", - "print \"Enter zero or negative values to end program\"\n", - "\n", - "# Priming reads follow\n", - "print \"First leg (0.0 to quit)? \",\n", - "a_leg = 3\n", - "print a_leg\n", - "print \"Second leg (0.0 to quit)? \",\n", - "b_leg = 4\n", - "print b_leg\n", - "\n", - "while a_leg>0.0 and b_leg>0.0:\n", - "\tprint \"The legs of the right triangle are %.2f and %.2f,\\n\"%(a_leg,b_leg),\n", - "\t# Hypotenuse is square root of a*a + b*b\n", - "\thypotenuse = math.sqrt(square(a_leg) + square(b_leg))\n", - "\tprint \" so the hypotenuse is %.2f\\n\\n\"%hypotenuse,\n", - "\tprint \"First leg (0.0 to quit)? \",\n", - "\ta_leg = 0.0\t\t\t# To terminate the loop\n", - "\tprint a_leg\n", - "\tprint \"Second leg (0.0 to quit)? \",\n", - "\tb_leg = 0.0\n", - "\tprint b_leg\n", - "\n", - "print \"Thank you.\"\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the lengths of the two legs of a right triangle\n", - "The program will display the length of the hypotenuse.\n", - "Enter zero or negative values to end program\n", - "First leg (0.0 to quit)? 3\n", - "Second leg (0.0 to quit)? 4\n", - "The legs of the right triangle are 3.00 and 4.00,\n", - " so the hypotenuse is 5.00\n", - "\n", - "First leg (0.0 to quit)? 0.0\n", - "Second leg (0.0 to quit)? 0.0\n", - "Thank you.\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 1-2 , Page number: 3" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "def main(): \n", - "\t\"\"\"simply returns a string\"\"\"\n", - "\t# There's no main() function in python.This main() function is no different that any other user-defined functions\n", - "\treturn \"Welcome to C programming!\\n\"\n", - "\t\n", - "print main()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Welcome to C programming!\n", - "\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 1-3 , Page number: 6" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Use of braces \n", - "\n", - "def main():\n", - "\t# in python, proper indentation works as braces\n", - "\t# the following lines come under the main() function block due to indentation\n", - "\tprint \"Give me land, lots of land\",\n", - "\tprint \"And the starry skies above...\\n\",\n", - "\n", - "main()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Give me land, lots of land And the starry skies above...\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 1-4 , Page number: 8" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A short poem\n", - "\n", - "def main():\n", - "\tprint \"Mary had a little lamb,\",\n", - "\tprint \"its fleece was white as snow.\\n\",\n", - "\tprint \"And everywhere that Mary went,\",\n", - "\tprint \"the lamb was sure to go.\\n\"\n", - "\n", - "main()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Mary had a little lamb, its fleece was white as snow.\n", - "And everywhere that Mary went, the lamb was sure to go.\n", - "\n" - ] - } - ], - "prompt_number": 4 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/Chapter10.ipynb b/The_Spirit_of_C/Chapter10.ipynb deleted file mode 100755 index ed7b99e5..00000000 --- a/The_Spirit_of_C/Chapter10.ipynb +++ /dev/null @@ -1,1020 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:970f28e2206ea4472dd82a3808592db202377fcd9a01fc69b666e61a221db5b1" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 10, Arrays" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-1, Page Number : 227" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Introduction to arrays\n", - "\n", - "array = []\t\t# defining array which is rather called list in python\n", - "\n", - "print \"You are to enter 5 integers.\\n\"\n", - "\n", - "# Input the 5 integers into array \"array\"\n", - "\n", - "for i in range(5) : # equivalent to for i in range(0,5,1)\n", - "\tprint \"Enter integer #%d: \"%(i+1),\n", - "\tval = int(raw_input())\n", - "\tarray.insert(i,val)\t\t\t\t# insert function is used to add elements to list : \n", - "\t\t\t\t\t\t\t\t\t# insert(i,val) where i is the index and val is the value to be inserted\n", - "\n", - "print \"\\nThank you.\\n\"\n", - "\n", - "# Print out the integers stored in array \"array\"\n", - "j = 1\t# used as a counter\n", - "for i in array:\t\t\t\t# shorthand way of scanning through the list; using \"in\"\n", - "\tprint \"Integer #%d = %d.\"%(j,i)\n", - "\tj += 1" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "You are to enter 5 integers.\n", - "\n", - "Enter integer #1:" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " Enter integer #2:" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "12\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " Enter integer #3:" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "8\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " Enter integer #4:" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "63\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " Enter integer #5:" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "0\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - " \n", - "Thank you.\n", - "\n", - "Integer #1 = 3.\n", - "Integer #2 = 12.\n", - "Integer #3 = 8.\n", - "Integer #4 = 63.\n", - "Integer #5 = 0.\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-2, Page Number : 228" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Initializing a global array\n", - "\n", - "array = [ 4, 6, 5, 7, 2 ]\n", - "\n", - "def main() :\t\t\t# just a user-defined function ; not equivalent to the built-in main() function in C\n", - "\t\"\"\" prints the integers stored in array \"array\" \"\"\"\n", - "\tj = 1\t\t# counter \n", - "\tfor i in array :\n", - "\t\tprint \"Integer #%d = %d.\"%(j,i)\n", - "\t\tj += 1\n", - "\n", - "main()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Integer #1 = 4.\n", - "Integer #2 = 6.\n", - "Integer #3 = 5.\n", - "Integer #4 = 7.\n", - "Integer #5 = 2.\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-3, Page Number: 230" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The number of array elements actually used may vary\n", - "\n", - "# Read in the size of the array\n", - "print \"How many integers will you enter?\",\n", - "array_size = 6\n", - "print array_size\n", - "\n", - "# Read in the integers\n", - "print \"\\nPlease enter %d integers:\"%array_size\n", - "array = [ 12, 3, -5, 21, 0, 899 ]\t# Lists dynamically allocate memory \n", - "for i in range(array_size) :\n", - "\tprint array[i],\n", - "# Print out the integers\n", - "\n", - "print(\"\\n\")\n", - "for i in range(array_size) :\n", - "\tprint \"Integer #%d = %d.\"%(i+1,array[i])" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many integers will you enter? 6\n", - "\n", - "Please enter 6 integers:\n", - "12 3 -5 21 0 899 \n", - "\n", - "Integer #1 = 12.\n", - "Integer #2 = 3.\n", - "Integer #3 = -5.\n", - "Integer #4 = 21.\n", - "Integer #5 = 0.\n", - "Integer #6 = 899.\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-4, Page Number: 231" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The trailer method\n", - "\n", - "# Macro definition\n", - "MAX_ARRAY_SIZE = 20\n", - "TRAILER = -9999\n", - "\n", - "# Read in the integers\n", - "\n", - "print \"Please enter up to %d integers, using %d as a trailer:\"%(MAX_ARRAY_SIZE,TRAILER)\n", - "array = [ 3, 75, -34, 6, -1, 2, 41, 0, 90, -9999]\n", - "for array_size in range(MAX_ARRAY_SIZE) :\n", - "\tprint array[array_size],\n", - "\tif array[array_size] == -9999 :\n", - "\t\tbreak\t\n", - "\t\n", - "# Print out the integers\n", - "print \"\\n\\nYou have entered %d integers\"%array_size\n", - "for i in range(array_size) :\n", - "\tprint \"Integer #%d = %d.\"%(i+1,array[i])" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter up to 20 integers, using -9999 as a trailer:\n", - "3 75 -34 6 -1 2 41 0 90 -9999 \n", - "\n", - "You have entered 9 integers\n", - "Integer #1 = 3.\n", - "Integer #2 = 75.\n", - "Integer #3 = -34.\n", - "Integer #4 = 6.\n", - "Integer #5 = -1.\n", - "Integer #6 = 2.\n", - "Integer #7 = 41.\n", - "Integer #8 = 0.\n", - "Integer #9 = 90.\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-5, Page Number: 233" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Caculating the average grade for a class\n", - "\n", - "# Macro definition\n", - "\n", - "MAX_GRADES = 20 \t# The class may have up to 20 students\n", - "TRAILER = -9999\t\t# -9999 marks the end of the grade list\n", - "\n", - "# Read in the grades\n", - "\n", - "print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n", - "grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n", - "for num_grades in range(MAX_GRADES) :\n", - "\tprint grades[num_grades]\n", - "\tif grades[num_grades] == -9999 :\n", - "\t\tbreak\n", - "\n", - "print \"\\nYou have entered %d grades.\"%num_grades\n", - "\n", - "# Find the average\n", - "sum = 0.0 \n", - "for i in range(num_grades) :\n", - "\tsum += grades[i]\n", - "\n", - "print \"\\nThe average grade is %f.\"%(sum/num_grades)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter up to 20 grades, using -9999 as a trailer:\n", - "41\n", - "92\n", - "15\n", - "65\n", - "0\n", - "78\n", - "81\n", - "96\n", - "-9999\n", - "\n", - "You have entered 8 grades.\n", - "\n", - "The average grade is 58.500000.\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-6, Page Number: 235" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Determining which grades in a class are above the average\n", - "\n", - "# Macro definition\n", - "\n", - "MAX_GRADES = 20 \t# The class may have up to 20 students\n", - "TRAILER = -9999\t\t# -9999 marks the end of the grade list\n", - "\n", - "# Read in the grades\n", - "\n", - "print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n", - "grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n", - "for num_grades in range(MAX_GRADES) :\n", - "\tprint grades[num_grades]\n", - "\tif grades[num_grades] == -9999 :\n", - "\t\tbreak\n", - "\n", - "print \"\\nYou have entered %d grades.\"%num_grades\n", - "\n", - "# Find the average\n", - "sum = 0.0 \n", - "for i in range(num_grades) :\n", - "\tsum += grades[i]\n", - "\n", - "avg_grade = sum/num_grades\n", - "print \"\\nThe average grade is %f.\"%avg_grade\n", - "\n", - "# Find who got above average\n", - "\n", - "print \"\\nThe following grades were above average:\"\n", - "for i in range(num_grades) :\n", - "\tif grades[i] > avg_grade :\n", - "\t\tprint \"%f (student #%d)\"%(grades[i],i+1)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter up to 20 grades, using -9999 as a trailer:\n", - "41\n", - "92\n", - "15\n", - "65\n", - "0\n", - "78\n", - "81\n", - "96\n", - "-9999\n", - "\n", - "You have entered 8 grades.\n", - "\n", - "The average grade is 58.500000.\n", - "\n", - "The following grades were above average:\n", - "92.000000 (student #2)\n", - "65.000000 (student #4)\n", - "78.000000 (student #6)\n", - "81.000000 (student #7)\n", - "96.000000 (student #8)\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-7, Page Number 237" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Reversing an array\n", - "\n", - "# Macro definition\n", - "ARRAY_SIZE = 10 \t# The array has 10 elements \n", - "\n", - "# Read in the array\n", - "\n", - "print \"Please enter %d integers:\"%ARRAY_SIZE\n", - "array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]\n", - "for i in range(10) :\n", - "\tprint array[i],\n", - "\n", - "# Echo the array\n", - "\n", - "print \"\\n\\nThe original array was:\"\n", - "for i in range(10) :\n", - "\tprint array[i],\n", - "\n", - "# Print the array in reverse\n", - "\n", - "print \"\\n\\nThe reversed array is:\"\n", - "for i in range(ARRAY_SIZE-1,-1,-1) :\n", - "\tprint array[i]," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter 10 integers:\n", - "10 42 7 0 923 13 -5 6 19 3 \n", - "\n", - "The original array was:\n", - "10 42 7 0 923 13 -5 6 19 3 \n", - "\n", - "The reversed array is:\n", - "3 19 6 -5 13 923 0 7 42 10\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-8, Page Number: 240" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import sys\n", - "# Reversing a character string\n", - "\n", - "# Macro definition\n", - "MAX_STRING_SIZE = 10\n", - "\n", - "# Read in the word\n", - "\n", - "print \"Please enter a word of up to %d letters:\"%MAX_STRING_SIZE\n", - "word = \"bingo\"\n", - "print word\n", - "\n", - "# Echo the word \n", - "\n", - "print \"\\nThe word is %s.\"%word\n", - "\n", - "# Find thw word length\n", - "string_size = 0\n", - "for i in word :\t\t\t# To iterate over the entire string\n", - "\tstring_size += 1\n", - "\n", - "# Print the word in reverse\n", - "\n", - "print \"\\nThe reversed word is \",\n", - "for i in range(string_size-1,-1,-1) :\n", - "\tsys.stdout.write(\"%c\"%word[i]) # For printing in the same line without implicit whitespaces\n", - "\n", - "print " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter a word of up to 10 letters:\n", - "bingo\n", - "\n", - "The word is bingo.\n", - "\n", - "The reversed word is ognib\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-9, Page Number: 242" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Calculating the minimum and maximum grade in a class\n", - "\n", - "# Macro definition\n", - "\n", - "MAX_GRADES = 20 \t# The class may have up to 20 students\n", - "TRAILER = -9999\t\t# -9999 marks the end of the grade list\n", - "\n", - "# Read in the grades\n", - "\n", - "print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n", - "grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n", - "for num_grades in range(MAX_GRADES) :\n", - "\tprint grades[num_grades]\n", - "\tif grades[num_grades] == -9999 :\n", - "\t\tbreak\n", - "\n", - "print \"\\nYou have entered %d grades.\"%num_grades\n", - "\n", - "# Find the minimum grade\n", - "\n", - "min = grades[0]\n", - "for i in range(num_grades) :\n", - "\tif grades[i] < min :\n", - "\t\tmin = grades[i]\n", - "\n", - "print \"\\nThe lowest grade is %f\"%min\n", - "\n", - "# Find the maximum grade\n", - "\n", - "max = grades[0]\n", - "for i in range(num_grades) :\n", - "\tif grades[i] > min :\n", - "\t\tmax = grades[i]\n", - "\n", - "print \"\\nThe lowest grade is %f\"%max\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter up to 20 grades, using -9999 as a trailer:\n", - "41\n", - "92\n", - "15\n", - "65\n", - "0\n", - "78\n", - "81\n", - "96\n", - "-9999\n", - "\n", - "You have entered 8 grades.\n", - "\n", - "The lowest grade is 0.000000\n", - "\n", - "The lowest grade is 96.000000\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-10, Page Number: 243" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Sorting an array\n", - "\n", - "# Macro definition\n", - "MAX_ARRAY_SIZE = 20 \t# The array may have up to 20 elements\n", - "\n", - "array_size = 10\n", - "print \"How many numbers?\",\n", - "print array_size\n", - "\n", - "while array_size < 1 or array_size > MAX_ARRAY_SIZE\t:\n", - "\tprint \"How many numbers?\",\n", - "\tprint array_size\n", - "\n", - "# Read in the array\n", - "\n", - "print \"Please enter the integers:\"\n", - "array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]\n", - "for i in range(array_size) :\n", - "\tprint array[i],\n", - "\n", - "# Echo the array\n", - "\n", - "print \"\\n\\nThe original array is:\"\n", - "for i in range(array_size) :\n", - "\tprint array[i],\n", - "\n", - "# Sort the array\n", - "\n", - "for i in range(array_size-1) :\n", - "\tfor j in range(i+1, array_size) :\n", - "\t\tif array[i] > array[j] :\n", - "\t\t\ttemp = array[i]\n", - "\t\t\tarray[i] = array[j]\n", - "\t\t\tarray[j] = temp\n", - "\n", - "# Printing the sorted array\n", - "\n", - "print \"\\n\\nThe sorted array is\"\n", - "for i in range(array_size) :\n", - "\tprint array[i]," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many numbers? 10\n", - "Please enter the integers:\n", - "10 42 7 0 923 13 -5 6 19 3 \n", - "\n", - "The original array is:\n", - "10 42 7 0 923 13 -5 6 19 3 \n", - "\n", - "The sorted array is\n", - "-5 0 3 6 7 10 13 19 42 923\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-11, Page Number: 247" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Finding the median of an array\n", - "\n", - "# Macro definition\n", - "MAX_ARRAY_SIZE = 20 \t# The array may have up to 20 elements\n", - "\n", - "array_size = 0\n", - "array = []\n", - "\n", - "def get_size()\t:\n", - "\t\"\"\" reads the size of the array (until a valid value is entered) \"\"\"\n", - "\tglobal array_size\n", - "\tarray_size = 10\n", - "\tprint \"How many numbers?\",\n", - "\tprint array_size\n", - "\twhile array_size < 1 or array_size > MAX_ARRAY_SIZE\t:\n", - "\t\tprint \"How many numbers?\",\n", - "\t\tprint array_size\n", - "\n", - "\n", - "def read_array() :\n", - "\t\"\"\" read in the array \"\"\"\n", - "\tglobal array\n", - "\tprint \"Please enter the integers:\"\n", - "\tarray = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3 ]\n", - "\tfor i in range(array_size) :\n", - "\t\tprint array[i],\n", - "\n", - "def write_array() :\n", - "\t\"\"\" write out the array \"\"\"\n", - "\tfor i in range(array_size) :\n", - "\t\tprint array[i],\n", - "\n", - "def sort_array() :\n", - "\t\"\"\" sort the array \"\"\"\n", - "\tglobal array\n", - "\tfor i in range(array_size-1) :\n", - "\t\tfor j in range(i+1, array_size) :\n", - "\t\t\tif array[i] > array[j] :\n", - "\t\t\t\ttemp = array[i]\n", - "\t\t\t\tarray[i] = array[j]\n", - "\t\t\t\tarray[j] = temp\n", - "\n", - "def even(n) :\n", - "\t\"\"\" returns non-zero(true) if n is even else returns zero(false) \"\"\"\n", - "\treturn ((n/2)*2 == n)\n", - "\n", - "get_size()\t\t# Read in the size of the array\n", - "\n", - "read_array()\t# Read in the array\n", - "\n", - "print \"\\n\\nThe original array is:\"\n", - "write_array()\t# Echo the array\n", - "\n", - "sort_array()\t# Sort the array\n", - "\n", - "print \"\\n\\nThe sorted array is:\"\n", - "write_array()\t# Print the sorted array\n", - "\n", - "print \"\\n\\nThe median is:\"\n", - "# The equivalent of tertiary conditional operator in python is a if test else b\n", - "print \"%.1f\"%( ((array[ array_size / 2 -1] + array[ array_size / 2 ]) / 2.0 ) if even(array_size) else float(array[ array_size / 2 ]) ) " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many numbers? 10\n", - "Please enter the integers:\n", - "10 42 7 0 923 13 -5 6 19 3 \n", - "\n", - "The original array is:\n", - "10 42 7 0 923 13 -5 6 19 3 \n", - "\n", - "The sorted array is:\n", - "-5 0 3 6 7 10 13 19 42 923 \n", - "\n", - "The median is:\n", - "8.5\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-12, Page Number: 251" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Tally how many of each number within a range are entered\n", - "\n", - "# Macro definition\n", - "RANGE_LOW = 1\n", - "RANGE_HIGH = 10\t\t\t# Tallies numbers between RANGE_LOW and RANGE_HIGH\n", - "TRAILER = -1 \t\t\t# TRAILER marks end of data\n", - "\n", - "inputfeed = [ 3, 5, 1, 10, 8, 5, 10, 6, 2, 11, 4, 9, -2, 3, 2, 9, -1 ]\t\t# additional array for the sake of providing input\n", - "\n", - "tally = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\t\t# empty array\n", - "# Input numbers\n", - "print \"Please enter a number between %d and %d (%d to quit): \"%(RANGE_LOW,RANGE_HIGH,TRAILER),\n", - "num = inputfeed[0]\n", - "print num\n", - "\n", - "i = 1\n", - "while num != TRAILER :\n", - "\tif num < RANGE_LOW or num > RANGE_HIGH : \t\t# Out of range\n", - "\t\tprint \"%d is not between %d and %d.\"%(num,RANGE_LOW,RANGE_HIGH)\n", - "\telse :\n", - "\t\ttally[ num - RANGE_LOW] += 1\t\n", - "\tprint \"\\nEnter another number: \",\n", - "\tnum = inputfeed[i]\n", - "\tprint num\n", - "\ti += 1 \t\t\t# to iterate over the input array\n", - "\n", - "# Print totals\n", - "\n", - "print \"\\n------------\\n\\nYou entered:\"\n", - "for i in range(RANGE_HIGH - RANGE_LOW + 1) :\n", - "\tprint \"%2d %2d's\"%(tally[i],i + RANGE_LOW)\n", - "\n", - "print \"\\nThank you.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter a number between 1 and 10 (-1 to quit): 3\n", - "\n", - "Enter another number: 5\n", - "\n", - "Enter another number: 1\n", - "\n", - "Enter another number: 10\n", - "\n", - "Enter another number: 8\n", - "\n", - "Enter another number: 5\n", - "\n", - "Enter another number: 10\n", - "\n", - "Enter another number: 6\n", - "\n", - "Enter another number: 2\n", - "\n", - "Enter another number: 11\n", - "11 is not between 1 and 10.\n", - "\n", - "Enter another number: 4\n", - "\n", - "Enter another number: 9\n", - "\n", - "Enter another number: -2\n", - "-2 is not between 1 and 10.\n", - "\n", - "Enter another number: 3\n", - "\n", - "Enter another number: 2\n", - "\n", - "Enter another number: 9\n", - "\n", - "Enter another number: -1\n", - "\n", - "------------\n", - "\n", - "You entered:\n", - " 1 1's\n", - " 2 2's\n", - " 2 3's\n", - " 1 4's\n", - " 2 5's\n", - " 1 6's\n", - " 0 7's\n", - " 1 8's\n", - " 2 9's\n", - " 2 10's\n", - "\n", - "Thank you.\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 10-13, Page Number: 255" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to report car prices. User enters car make by number and car year. \n", - "# Program reports price. All data is fictitious\n", - "\n", - "# 2-D array (4 X 12 ) of prices : row = make , column = year\n", - "price_list = [\t[ 4775, 4980, 5222, 5305, 5483, 5547, 5596, 5713, 5842, 5903, 6043, 6230 ], # Volkswagen\n", - "\t\t\t\t[ 4853, 5140, 5413, 5590, 5723, 5848, 5762, 5944, 6104, 6255, 6370, 6526 ],\t# Chevy\n", - "\t\t\t\t[ 5627, 5772, 5973, 6210, 6539, 6720, 6792, 6930, 7054, 7202, 7355, 7562 ], # Cadillac\n", - "\t\t\t\t[ 1576, 1738, 1970, 2151, 2205, 2280, 2442, 2580, 2814, 1953, 3078, 3201 ],\t# Honda\n", - "\t\t\t]\t\n", - "\n", - "def get_make() : \n", - "\t\"\"\" read in and return the number (make) of the desired car \"\"\"\n", - "\t\n", - "\t# Print the choice numbers and names\n", - "\tfor i in range(1,5) :\n", - "\t\tprint \"%d=\"%i,\n", - "\t\tprint_make(i)\n", - "\t\tprint \",\",\n", - "\tprint \"0=END\"\n", - "\n", - "\t# Read in the selection by number\n", - "\tprint \" Which car do you want? \",\n", - "\tmake = 2\n", - "\tprint make\n", - "\twhile make < 0 or make > 4 :\n", - "\t\tprint \" Which car do you want? \",\n", - "\t\tmake = 2\n", - "\t\tprint make\n", - "\n", - "\treturn make\n", - "\n", - "def get_year() :\t\n", - "\t\"\"\" read in and return the year of the desired car \"\"\"\n", - "\n", - "\tprint \" What year (1975-1986)?\",\n", - "\tyear = 1980\n", - "\tprint year\n", - "\twhile year < 1975 and year > 1986 :\n", - "\t\tprint \" What year (1975-1986)?\",\n", - "\t\tyear = 1980\n", - "\t\tprint year\n", - "\n", - "\treturn year\n", - "\n", - "def price (car,yr) :\n", - "\t\"\"\" Use \"car\" and \"year\" to generate an index into \"price_list\" and return the corresponding entry \"\"\"\n", - "\treturn price_list[ car - 1 ][ yr - 1975]\n", - "\n", - "def print_make(car) :\n", - "\t\"\"\" print the make of \"car\" according to its value \"\"\"\n", - "\tif car == 1 : \n", - "\t\tprint \"Volkswagen\",\n", - "\telif car == 2 :\n", - "\t\tprint \"Chevy\",\n", - "\telif car == 3 :\n", - "\t\tprint \"Cadillac\",\n", - "\telse :\n", - "\t\tprint \"Honda\",\n", - "\n", - "make = get_make()\n", - "\n", - "while make != 0 :\t\t# O indicates end of input\n", - "\tyear = get_year()\n", - "\tprint \"The price of a %d\"%year,\n", - "\tprint_make(make)\n", - "\tprint \" is $%d.\\n\"%price(make,year)\n", - "\tmake = 0 # To terminate the loop\n", - "\n", - "print \"\\nThank you for buying all those cars!\"\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1= Volkswagen , 2= Chevy , 3= Cadillac , 4= Honda , 0=END\n", - " Which car do you want? 2\n", - " What year (1975-1986)? 1980\n", - "The price of a 1980 Chevy is $5848.\n", - "\n", - "\n", - "Thank you for buying all those cars!\n" - ] - } - ], - "prompt_number": 13 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/Chapter11.ipynb b/The_Spirit_of_C/Chapter11.ipynb deleted file mode 100755 index 241e1c1e..00000000 --- a/The_Spirit_of_C/Chapter11.ipynb +++ /dev/null @@ -1,383 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:3a9b6dffdf9d20cf5158c028c4012a0a2cf4ee76994f18e9b81499931df7cb5d" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 11, Pointers and Indirection" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 11-1, Page Number : 278" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Introduction to pointers\n", - "\n", - "# There is no concept of pointers in python\n", - "i = 3\n", - "\n", - "j = id(i)\t# id() returns the address of the variable supplied\n", - "j = i \t\t# assign value of i to j such that it seems like j points to i\n", - "k = j\n", - "j = 4\n", - "\n", - "print \"i = %d, *j = %d, k = %d\"%(i,j,k)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "i = 3, *j = 4, k = 3\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 11-2, Page Number: 280" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Demonstration of passing by reference for a function to be able to modify its parameters\n", - "\n", - "def modify(i) :\n", - "\ti = 3\n", - "\treturn i\n", - "\n", - "i = 1\n", - "print \"i is now equal to %d\"%i\n", - "\n", - "i = modify(i)\t\t\t\n", - "print \"i is now equal to %d\"%i" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "i is now equal to 1\n", - "i is now equal to 3\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 11-3, Page Number: 281" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Pointer parameters in action: the swap routine\n", - "\n", - "def swap(i,j) :\n", - "\t\"\"\" exchange i with j and return \"\"\"\n", - "\ttemp = i \t\t\t# temp variable can be avoided using i,j = j,i\n", - "\ti = j\n", - "\tj = temp\n", - "\treturn i,j\n", - "\n", - "i = 1\n", - "j = 9\n", - "print \"i = %d and j = %d\"%(i,j)\n", - "\n", - "# Now exchange them\n", - "i, j = swap(i,j)\n", - "print \"\\nnow i = %d and j = %d\"%(i,j)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "i = 1 and j = 9\n", - "\n", - "now i = 9 and j = 1\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 11-4, Page Number: 282" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Array names are constant pointers\n", - "\n", - "an_array = [ 3, 7 ]\n", - "\n", - "def main() :\n", - "\talso = an_array\n", - "\talso[0] = 14\n", - "\tprint \"an_array = { %d, %d }\"%(an_array[0],an_array[1])\n", - "\n", - "main()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "an_array = { 14, 7 }\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 11-5, Page Number: 284" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Sorting an array revisited\n", - "# Passing an array as parameter\n", - "\n", - "# Macro definition\n", - "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n", - "# Message flags for \"print array\"\n", - "ORIGINAL = 0\t# Display original array\n", - "SORTED = 1\t\t# Display sorted array\n", - "\n", - "numbers = [ 10, 42, 7, 0, 923, 12, -5, 6, 19, 3 ]\n", - "array_size = 0\n", - "\n", - "def get_size(size)\t:\n", - "\t\"\"\" reads the size of the array (until a valid value is entered) \"\"\"\n", - "\tglobal array_size\n", - "\tprint \"How many numbers?\",\n", - "\tsize = 10\n", - "\tprint size\n", - "\twhile size < 1 or size > MAX_ARRAY_SIZE\t:\n", - "\t\tprint \"How many numbers?\",\n", - "\t\tsize = 10\n", - "\t\tprint size\n", - "\tarray_size = size\n", - "\n", - "def get_array(array, size) :\n", - "\t\"\"\" read in the array called 'array' of size 'size' \"\"\"\n", - "\tprint \"Please enter the integers:\"\n", - "\tfor i in array :\n", - "\t\tprint i,\n", - "\tprint\n", - "\n", - "def print_array(array, size, message) :\n", - "\t\"\"\" print out the array on a single line. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n", - "\tif message == ORIGINAL :\n", - "\t\tprint \"\\nThe original array is:\"\n", - "\telse :\n", - "\t\tprint \"\\nThe sorted array is:\"\n", - "\tfor i in array :\n", - "\t\tprint i,\n", - "\tprint\n", - "\n", - "def sort_array(array,size) :\n", - "\t\"\"\" sort 'array' of size 'size' \"\"\"\n", - "\tfor i in range(size - 1) :\n", - "\t\tfor j in range(i+1, size) :\n", - "\t\t\tif array[i] > array[j] :\n", - "\t\t\t\telement_swap(array, i, j)\n", - "\n", - "def element_swap(array, i, j) :\n", - "\t\"\"\" swap elements 'i' and 'j' of 'array' \"\"\"\n", - "\tglobal numbers\n", - "\tnumbers[i],numbers[j] = numbers[j],numbers[i]\n", - "\n", - "\n", - "get_size(array_size)\t\t\t\t\t\t# Read in the array's size\n", - "get_array(numbers, array_size)\t\t\t\t\t# Read in the array 'numbers' of size 'array_size'\n", - "print_array(numbers, array_size, ORIGINAL) \t# Echo the array \n", - "sort_array(numbers, array_size)\n", - "print_array(numbers, array_size, SORTED)\t\t# Print the sorted array" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many numbers? 10\n", - "Please enter the integers:\n", - "10 42 7 0 923 12 -5 6 19 3\n", - "\n", - "The original array is:\n", - "10 42 7 0 923 12 -5 6 19 3\n", - "\n", - "The sorted array is:\n", - "-5 0 3 6 7 10 12 19 42 923\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 11-6, Page Number: 294" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Sorting of array revisited\n", - "# Two dimensional char array (array of strings)\n", - "\n", - "# Macro definition\n", - "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n", - "MAX_STRING_SIZE = 10 \t# Each string may have up to 10 characters\n", - "# Message flags for \"print_array\"\n", - "ORIGINAL = 0\n", - "SORTED = 1\n", - "\n", - "words = [ \"cat\", \"pig\", \"dog\", \"earlobe\", \"bingo\", \"elbow\", \"likely\", \"radar\" ]\n", - "array_size = 0\n", - "\n", - "def get_size(size) :\n", - "\t\"\"\" Read the size of the array into size (until a value is entered) \"\"\"\n", - "\tprint \"How many words? \",\n", - "\tsize = 8\n", - "\tprint size\n", - "\twhile size < 1 or size > MAX_ARRAY_SIZE :\n", - "\t\tprint \"How many words? \",\n", - "\t\tsize = 8\n", - "\t\tprint size\n", - "\tglobal array_size\n", - "\tarray_size = size\n", - "\n", - "def get_array(array,size) :\n", - "\t\"\"\" Read in the string array called 'array' of size 'size' \"\"\"\n", - "\tprint \"Please enter the words:\"\n", - "\tfor i in array :\n", - "\t\tprint i\n", - "\t\n", - "def print_array(array,size,message) :\n", - "\t\"\"\" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n", - "\tif message == ORIGINAL :\n", - "\t\tprint \"\\nThe original array is:\"\n", - "\telse :\n", - "\t\tprint \"\\nThe sorted array is:\"\n", - "\tfor i in array :\n", - "\t\tprint i\n", - "\n", - "def sort_array(array,size) :\n", - "\t\"\"\" sort string array \"\"\"\n", - "\tfor i in range (size-1) :\n", - "\t\tfor j in range (i + 1, size) :\n", - "\t\t\tif array[i] > array[j] :\n", - "\t\t\t\tstring_swap(i,j)\n", - "\n", - "def string_swap(i, j) :\n", - "\t\"\"\" Exchange strings \"\"\"\n", - "\tglobal words\n", - "\twords[i],words[j] = words[j],words[i]\n", - "\n", - "get_size(array_size)\t\t# Read in the array size\n", - "get_array(words, array_size)\t# Read in the string array \"words\" of size \"array_size\"\n", - "print_array(words, array_size, ORIGINAL)\t# Echo the original array\n", - "sort_array(words, array_size)\t\n", - "print_array(words, array_size, SORTED) # Print the sorted array" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many words? 8\n", - "Please enter the words:\n", - "cat\n", - "pig\n", - "dog\n", - "earlobe\n", - "bingo\n", - "elbow\n", - "likely\n", - "radar\n", - "\n", - "The original array is:\n", - "cat\n", - "pig\n", - "dog\n", - "earlobe\n", - "bingo\n", - "elbow\n", - "likely\n", - "radar\n", - "\n", - "The sorted array is:\n", - "bingo\n", - "cat\n", - "dog\n", - "earlobe\n", - "elbow\n", - "likely\n", - "pig\n", - "radar\n" - ] - } - ], - "prompt_number": 6 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/Chapter12.ipynb b/The_Spirit_of_C/Chapter12.ipynb deleted file mode 100755 index 0195ed25..00000000 --- a/The_Spirit_of_C/Chapter12.ipynb +++ /dev/null @@ -1,455 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:bfcc6bf3e86f2ff3b103be284b1b027f6faa9020241e866b9b4398fd89f8923c" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 12, Strings and String Functions" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-1, Page Number: 331" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The control string is just a string\n", - "\n", - "format = \"%s\"\n", - "message = \"hello\"\n", - "\n", - "print format % message" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "hello\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-2, Page Number: 334" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The control string is just a string - part 2\n", - "\n", - "format = \"%.15s\"\n", - "message = \"hellohellohello\"\n", - "\n", - "# Read in the number of characters to be displayed from \"message\"\n", - "print \"How many characters do you want displayed?\",\n", - "size = 5\n", - "print size\n", - "\n", - "# Convert size to 2-digit number in format[2] and format[3]\n", - "\n", - "format = \"%.\" + str(size / 10) + str(size % 10) + \"s\"\n", - "print \"The format is %s\"%format\n", - "\n", - "# Print the string\n", - "print format % message" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many characters do you want displayed? 5\n", - "The format is %.05s\n", - "hello\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-3, Page Number 336" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# sprintf function \n", - "\n", - "# There is no sprintf functionality in python\n", - "# However an alternate way to solve the problem is...\n", - "\n", - "print \"Please enter a number:\",\n", - "number = 17\n", - "print number\n", - "\n", - "string = \"The number is %d\"%number\n", - "\n", - "print \"%s\"%string\n", - "\n", - "# OR \n", - "\n", - "print string" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter a number: 17\n", - "The number is 17\n", - "The number is 17\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-4, Page Number 337" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Variable control string using the sprintf function\n", - "\n", - "message = \"hellohellohello\"\n", - "\n", - "# Read in the number of characters to be displayed from message \n", - "print \"How many characters do you want displayed?\",\n", - "size = 7\n", - "print size\n", - "\n", - "# Create format in \"format\" using \"size\"\n", - "format = \"%%.%ds\\n\"%size\n", - "print \"The format is: %s\"%format\n", - "\n", - "# Print the string\n", - "print format % message" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many characters do you want displayed? 7\n", - "The format is: %.7s\n", - "\n", - "hellohe\n", - "\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-5, Page Number 338" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A terse string copy function\n", - "\n", - "def strcpy(copy, original):\n", - "\t\"\"\" copies one string into another \"\"\"\n", - "\tcopy = original\n", - "\treturn copy\n", - "\n", - "string1 = \"\"\n", - "string1 = strcpy(string1, \"This is a message\")\n", - "print \"String 1 = %s\"%string1\n", - "\n", - "string2 = \"\"\n", - "string2 = strcpy(string2, string1)\n", - "print \"String 2 = %s\"%string2" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "String 1 = This is a message\n", - "String 2 = This is a message\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-7, Page Number 344" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "string_point = \"hello\"\n", - "\n", - "string_var1 = string_point\n", - "\n", - "print \"%s\\n%s\"%(string_point,string_var1)\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "hello\n", - "hello\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-8, Page Number 346" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# strcat function in python\n", - "# simple addition of strings results in concatenation in python\n", - "\n", - "def strcat(string1, string2) :\n", - "\t\"\"\" concatenates the string and returns the concatenated string \"\"\"\n", - "\tstring2 = string1 + string2\n", - "\treturn string2\n", - "\n", - "string1 = \"hello\"\n", - "string2 = strcat(string1,\" there\")\n", - "print \"string1 = %s\\nstring2 = %s\"%(string1,string2)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "string1 = hello\n", - "string2 = hello there\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-9, Page Number 348" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# strcat version 2\n", - "\n", - "def strcat(string1, string2) :\n", - "\tstring1 = string1 + string2\n", - "\tstring2 = string1\n", - "\treturn string1,string2\n", - "\n", - "string1 = \"hello\"\n", - "string1,string2 = strcat(string1, \" there\")\n", - "\n", - "print \"string1 = %s\\nstring2 = %s\"%(string1,string2)\n", - "\n", - "if string1 == string2 :\n", - "\tprint \"string2 points to string1\"\n", - "else :\n", - "\tprint \"string2 does not point to string1\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "string1 = hello there\n", - "string2 = hello there\n", - "string2 points to string1\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 12-10, Page Number 351" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Alphabetizing program using dynamically allocated arrays\n", - "\n", - "# Macro definition\n", - "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n", - "MAX_STRING_SIZE = 10 \t# Each string may have up to 10 characters\n", - "# Message flags for \"print_array\"\n", - "ORIGINAL = 0\n", - "SORTED = 1\n", - "\n", - "words = [ \"cat\", \"pig\", \"dog\", \"earlobe\", \"bingo\", \"elbow\", \"likely\", \"radar\" ]\n", - "array_size = 0\n", - "\n", - "def get_size(size) :\n", - "\t\"\"\" Read the size of the array into size (until a value is entered) \"\"\"\n", - "\tprint \"How many words? \",\n", - "\tsize = 8\n", - "\tprint size\n", - "\twhile size < 1 or size > MAX_ARRAY_SIZE :\n", - "\t\tprint \"How many words? \",\n", - "\t\tsize = 8\n", - "\t\tprint size\n", - "\tglobal array_size\n", - "\tarray_size = size\n", - "\n", - "def get_array(array,size) :\n", - "\t\"\"\" Read in the string array called 'array' of size 'size' \"\"\"\n", - "\tprint \"Please enter the words:\"\n", - "\tfor i in array :\n", - "\t\tprint i\n", - "\t\n", - "def print_array(array,size,message) :\n", - "\t\"\"\" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n", - "\tprintout = [ \"\\nThe original array is:\", \"\\nThe sorted array is:\", \"\\n\" ]\n", - "\t# Print the appropriate message\n", - "\tprint \"%s\"%printout[message]\n", - "\n", - "\tfor i in array :\n", - "\t\tprint i\n", - "\n", - "def pointer_sort(array,size) :\n", - "\t\"\"\" sort string array \"\"\"\n", - "\tfor i in range (size-1) :\n", - "\t\tfor j in range (i + 1, size) :\n", - "\t\t\tif array[i] > array[j] :\n", - "\t\t\t\tswap(i,j)\n", - "\n", - "def swap(i, j) :\n", - "\t\"\"\" Exchange strings \"\"\"\n", - "\tglobal words\n", - "\twords[i],words[j] = words[j],words[i]\n", - "\n", - "get_size(array_size)\t\t# Read in the array size\n", - "get_array(words, array_size)\t# Read in the string array \"words\" of size \"array_size\"\n", - "print_array(words, array_size, ORIGINAL)\t# Echo the original array\n", - "pointer_sort(words, array_size)\t\n", - "print_array(words, array_size, SORTED) # Print the sorted array" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many words? 8\n", - "Please enter the words:\n", - "cat\n", - "pig\n", - "dog\n", - "earlobe\n", - "bingo\n", - "elbow\n", - "likely\n", - "radar\n", - "\n", - "The original array is:\n", - "cat\n", - "pig\n", - "dog\n", - "earlobe\n", - "bingo\n", - "elbow\n", - "likely\n", - "radar\n", - "\n", - "The sorted array is:\n", - "bingo\n", - "cat\n", - "dog\n", - "earlobe\n", - "elbow\n", - "likely\n", - "pig\n", - "radar\n" - ] - } - ], - "prompt_number": 10 - } - ], - "metadata": {} - } - ] -} diff --git a/The_Spirit_of_C/Chapter13.ipynb b/The_Spirit_of_C/Chapter13.ipynb deleted file mode 100755 index d363dc7a..00000000 --- a/The_Spirit_of_C/Chapter13.ipynb +++ /dev/null @@ -1,346 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:133c881c520fd0cdef07f65db120109cc62a996dd5161cf0f664c2ffcaa3adc4" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 13, Structures" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 13-1, Page Number: 370" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Introduction to structures\n", - "\n", - "from collections import namedtuple\n", - "\n", - "#Structure defintion\n", - "struct = namedtuple(\"struct\", \"integer real\")\n", - "\n", - "first_structure = struct( 7, 3.14 )\n", - "second_structure = first_structure\n", - "\n", - "print \"integer = %d, real = %f\"%(second_structure.integer,second_structure.real)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "integer = 7, real = 3.140000\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 13-2, Page Number: 371" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Initializing a structure\n", - "\n", - "from collections import namedtuple\n", - "\n", - "#Structure defintion\n", - "struct = namedtuple(\"struct\", \"integer real\")\n", - "\n", - "structure = struct( 7, 3.14 )\n", - "\n", - "print \"integer = %d, real = %f\"%(structure.integer,structure.real)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "integer = 7, real = 3.140000\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 13-3, Page Number: 372" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The structure tag\n", - "\n", - "from collections import namedtuple\n", - "\n", - "#Structure defintion\n", - "struct = namedtuple(\"struct\", \"integer real\")\n", - "\n", - "first_structure = struct( 7, 3.14 )\n", - "second_structure = first_structure\n", - "\n", - "print \"integer = %d, real = %f\"%(second_structure.integer,second_structure.real)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "integer = 7, real = 3.140000\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 13-4, Page Number: 375" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An array of structures\n", - "\n", - "from collections import namedtuple\n", - "\n", - "#Structure defintion\n", - "struct = namedtuple(\"struct\", \"code price\")\n", - "\n", - "#code = [ \"a\", \"b\", \"c\", \"d\", \"e\" ]\n", - "#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]\n", - "\n", - "table = [ struct(\"a\",3.29) , struct(\"b\",2.99), struct(\"c\",0.59), struct(\"d\",1.59), struct(\"e\",2.39) ]\n", - "\n", - "print \"Enter item code:\",\n", - "item_code = \"a\"\n", - "print item_code\n", - "i = 0\n", - "\n", - "while item_code != \".\" :\n", - "\n", - "\t#Search for 'item_code' in 'table'\n", - "\tfor i in range(4) :\n", - "\t\tif table[i].code == item_code :\n", - "\t\t\tbreak\n", - "\n", - "\tif i < 4:\n", - "\t\tprint \"Enter quantity: \",\n", - "\t\tquantity = 1\n", - "\t\tprint quantity\n", - "\t\n", - "\t\tprint \"\\nUnit price = %.2f, total price = %.2f.\\n\\n\"%(table[i].price,quantity * table[i].price)\n", - "\telse :\n", - "\t\tprint \"\\nItem code %c does not exist.\\n\\n\"%item_code\n", - "\tprint \"Enter item code:\",\n", - "\titem_code = \".\"\n", - "\tprint item_code\n", - "\n", - "\n", - "print \"Thank you.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter item code: a\n", - "Enter quantity: 1\n", - "\n", - "Unit price = 3.29, total price = 3.29.\n", - "\n", - "\n", - "Enter item code: .\n", - "Thank you.\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 13-5, Page Number: 378" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A pointer in a structure\n", - "\n", - "from collections import namedtuple\n", - "\n", - "#Structure defintion\n", - "struct = namedtuple(\"struct\", \"name price\")\n", - "\n", - "#code = [ \"a\", \"b\", \"c\", \"d\", \"e\" ]\n", - "#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]\n", - "\n", - "table = [ struct(\"pickles\",2.29) , struct(\"soda\",0.99), struct(\"yogurt\",0.59), struct(\"bread\",1.09), struct(\"milk\",0.69) ]\n", - "\n", - "print \"Enter item name:\",\n", - "item_name = \"pickles\"\n", - "print item_name\n", - "i = 0\n", - "\n", - "while item_name != \".\" :\n", - "\n", - "\t#Search for 'item_code' in 'table'\n", - "\tfor i in range(4) :\n", - "\t\tif table[i].name == item_name :\n", - "\t\t\tbreak\n", - "\n", - "\tif i < 4:\n", - "\t\tprint \"Enter quantity: \",\n", - "\t\tquantity = 1\n", - "\t\tprint quantity\n", - "\t\n", - "\t\tprint \"\\nUnit price = %.2f, total price = %.2f.\\n\\n\"%(table[i].price,quantity * table[i].price)\n", - "\telse :\n", - "\t\tprint \"\\nItem code %c does not exist.\\n\\n\"%item_name\n", - "\tprint \"Enter item code:\",\n", - "\titem_name = \".\"\n", - "\tprint item_name\n", - "\n", - "\n", - "print \"Thank you.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter item name: pickles\n", - "Enter quantity: 1\n", - "\n", - "Unit price = 2.29, total price = 2.29.\n", - "\n", - "\n", - "Enter item code: .\n", - "Thank you.\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 13-6, Page Number: 381" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An array in a structure\n", - "\n", - "from collections import namedtuple\n", - "\n", - "#Structure defintion\n", - "struct = namedtuple(\"struct\", \"name price\")\n", - "\n", - "#code = [ \"a\", \"b\", \"c\", \"d\", \"e\" ]\n", - "#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]\n", - "\n", - "table = [ struct(\"pickles\",2.29) , struct(\"soda\",0.99), struct(\"yogurt\",0.59), struct(\"bread\",1.09), struct(\"milk\",0.69) ]\n", - "\n", - "print \"Enter item name:\",\n", - "item_name = \"milk\"\n", - "print item_name\n", - "i = 0\n", - "\n", - "while item_name != \".\" :\n", - "\n", - "\t#Search for 'item_code' in 'table'\n", - "\tfor i in range(5) :\n", - "\t\tif table[i].name == item_name :\n", - "\t\t\tbreak\n", - "\n", - "\tif i < 5:\n", - "\t\tprint \"Enter quantity: \",\n", - "\t\tquantity = 5\n", - "\t\tprint quantity\n", - "\t\n", - "\t\tprint \"\\nUnit price = %.2f, total price = %.2f.\\n\\n\"%(table[i].price,quantity * table[i].price)\n", - "\telse :\n", - "\t\tprint \"\\nItem code %c does not exist.\\n\\n\"%item_name\n", - "\tprint \"Enter item code:\",\n", - "\titem_name = \".\"\n", - "\tprint item_name\n", - "\n", - "\n", - "print \"Thank you.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter item name: milk\n", - "Enter quantity: 5\n", - "\n", - "Unit price = 0.69, total price = 3.45.\n", - "\n", - "\n", - "Enter item code: .\n", - "Thank you.\n" - ] - } - ], - "prompt_number": 6 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/Chapter14.ipynb b/The_Spirit_of_C/Chapter14.ipynb deleted file mode 100755 index 380e83ef..00000000 --- a/The_Spirit_of_C/Chapter14.ipynb +++ /dev/null @@ -1,298 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:3748f84e3bd0279efe47855bd2c4e23912ef725121f3591980fa33e19d6ccae4" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 14, Input/Output and Files" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 14-5, Page Number: 424" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Files : Opening and Closing\n", - "\n", - "# Open the file for writing\n", - "\n", - "file_pointer = open(\"first_file\",\"w\")\n", - "\n", - "print \"Enter text:\"\n", - "\n", - "# Get characters and place them in the file\n", - "\n", - "text = \"BASIC is easy,\\nFORTAN is fun,\\nPascal may be structured,\\nBut C's the one!\"\n", - "print text\n", - "file_pointer.write(text)\n", - "\n", - "file_pointer.close()\n", - "\n", - "file_pointer = open(\"first_file\",\"r\")\n", - "\n", - "#Display the text from file\n", - "\n", - "print \"\\nYou entered:\"\n", - "print file_pointer.read()\n", - "\n", - "file_pointer.close()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter text:\n", - "BASIC is easy,\n", - "FORTAN is fun,\n", - "Pascal may be structured,\n", - "But C's the one!\n", - "\n", - "You entered:\n", - "BASIC is easy,\n", - "FORTAN is fun,\n", - "Pascal may be structured,\n", - "But C's the one!\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 14-6, Page Number: 426" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Using several files\n", - "\n", - "# Open the source file for reading\n", - "\n", - "first_file_pointer = open(\"first_file\",\"r\")\n", - "\n", - "# Open the destination file for writing\n", - "\n", - "second_file_pointer = open(\"second_file\",\"w\")\n", - "\n", - "# Transfer characters from source to destination file\n", - "\n", - "c = first_file_pointer.read()\n", - "second_file_pointer.write(c)\n", - "\n", - "# Close both files\n", - "\n", - "first_file_pointer.close()\n", - "second_file_pointer.close()\n", - "\n", - "# Open second file for displaying contentes\n", - "\n", - "second_file_pointer = open(\"second_file\",\"r\")\n", - "print(\"'second_file' contains:\")\n", - "print second_file_pointer.read()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "'second_file' contains:\n", - "BASIC is easy,\n", - "FORTAN is fun,\n", - "Pascal may be structured,\n", - "But C's the one!\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 14-7, Page Number : 428" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Append mode\n", - "\n", - "# Open the first file for writing\n", - "\n", - "first_file_pointer = open(\"first_file\",\"w\")\n", - "\n", - "# Open the second file for appending\n", - "\n", - "second_file_pointer = open(\"second_file\",\"a\")\n", - "\n", - "# Writing to both files\n", - "\n", - "first_file_pointer.write(\"\\n--Anonymous\\n\")\n", - "second_file_pointer.write(\"\\n--Anonymous\\n\")\n", - "\n", - "# Close both files\n", - "\n", - "first_file_pointer.close()\n", - "second_file_pointer.close()\n", - "\n", - "# Display the contents of first file\n", - "first_file_pointer = open(\"first_file\",\"r\")\n", - "print(\"After write, 'first_file contains:\")\n", - "print first_file_pointer.read()\n", - "\n", - "# Display the contents of second file\n", - "second_file_pointer = open(\"second_file\",\"r\")\n", - "print(\"After append, 'second_file' contains:\")\n", - "print second_file_pointer.read()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "After write, 'first_file contains:\n", - "\n", - "--Anonymous\n", - "\n", - "After append, 'second_file' contains:\n", - "BASIC is easy,\n", - "FORTAN is fun,\n", - "Pascal may be structured,\n", - "But C's the one!\n", - "--Anonymous\n", - "\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 14-8, Page Number: 431" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The seek function\n", - "\n", - "# Open file for reading\n", - "try :\n", - "\tfile_pointer = open(\"second_file\",\"r\")\n", - "except :\n", - "\tprint \"'second_file' cannot be opened\"\n", - "\texit ()\n", - "\n", - "# Skip the first 6 characters\n", - "\n", - "file_pointer.seek(6,0)\n", - "print file_pointer.read()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "is easy,\n", - "FORTAN is fun,\n", - "Pascal may be structured,\n", - "But C's the one!\n", - "--Anonymous\n", - "\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 14-9, Page Number: 433" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# The 'w+' mode and seek to rewind a file\n", - "\n", - "# Open a file for read/write\n", - "file_pointer = open(\"first_file\",\"w+\")\n", - "\n", - "print \"Enter text:\"\n", - "text = \"BASIC is easy,\\nFORTAN is fun,\\nPascal may be structured,\\nBut C's the one!\"\n", - "print text\n", - "file_pointer.write(text)\n", - "\n", - "file_pointer.seek(0,0)\n", - "\n", - "# Display the file\n", - "print \"\\nYou entered:\"\n", - "print file_pointer.read()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter text:\n", - "BASIC is easy,\n", - "FORTAN is fun,\n", - "Pascal may be structured,\n", - "But C's the one!\n", - "\n", - "You entered:\n", - "BASIC is easy,\n", - "FORTAN is fun,\n", - "Pascal may be structured,\n", - "But C's the one!\n" - ] - } - ], - "prompt_number": 5 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter2.ipynb b/The_Spirit_of_C/chapter2.ipynb deleted file mode 100755 index dc84a28d..00000000 --- a/The_Spirit_of_C/chapter2.ipynb +++ /dev/null @@ -1,119 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:7c8e11b3dcc5781949652c0b5c450d78713cb6e6b69326955d0f763c48805147" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 2, An Introduction to Declarations, Assignments and Variables" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 2-1 , Page number: 20" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An introduction to variable declarations \n", - "\n", - "number = 7 # no need of declaration of variable. Python is dynamically-typed language\n", - "print \"There are %d days in a week.\"%number" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "There are 7 days in a week.\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 2-2 , Page number: 21" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A simple program with two declarations\n", - "\n", - "minutes = 60\n", - "hours = 24\n", - "\n", - "print \"There are %d minutes in an hour.\"%minutes\n", - "print \"And %d hours in a day.\"%hours" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "There are 60 minutes in an hour.\n", - "And 24 hours in a day.\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 2-4 , Page number: 22" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# prints values of two variables\n", - "\n", - "dozens = 17\n", - "units = dozens * 12\n", - "\n", - "print \"%d units is equivalent to %d dozen.\"%(units,dozens)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "204 units is equivalent to 17 dozen.\n" - ] - } - ], - "prompt_number": 3 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter3.ipynb b/The_Spirit_of_C/chapter3.ipynb deleted file mode 100755 index b498486d..00000000 --- a/The_Spirit_of_C/chapter3.ipynb +++ /dev/null @@ -1,241 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:c466b513c45bfe09da1b0507614f538770318fb66aadf4da4e1995363df32911" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 3 , Integer Arithmetic Expressions" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 3-1 , Page number: 33" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An introduction to arithmetic \n", - "\n", - "a = 2\n", - "b = 3\n", - "c = 4\n", - "\n", - "answer = a + b\n", - "print \"a + b = %d\"%answer\n", - "\n", - "answer = a - b\n", - "print \"a - b = %d\"%answer\n", - "\n", - "answer = a * b\n", - "print \"a * b = %d\"%answer\n", - "\n", - "answer = a / b\n", - "print \"a / b = %d\"%answer\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "a + b = 5\n", - "a - b = -1\n", - "a * b = 6\n", - "a / b = 0\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 3-2 , Page number: 34" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of mathematical hierarchy\n", - "\n", - "a = 2\n", - "b = 3\n", - "c = 4\n", - "\n", - "answer = a + b * c\n", - "\n", - "print \"a = %d, b = %d, c= %d\"%(a,b,c)\n", - "print \"a + b * c = %d\\n\"%answer" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "a = 2, b = 3, c= 4\n", - "a + b * c = 14\n", - "\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 3-3 , Page number: 35" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A more advanced illustration of mathematical hierarchy\n", - "\n", - "a = 5\n", - "b = 10\n", - "c = 15\n", - "\n", - "answer = a + b / c\n", - "print \"%d + %d / %d = %d\"%(a,b,c,answer)\n", - "\n", - "answer = b * c - a\n", - "print \"%d * %d - %d = %d\"%(a,b,c,answer)\n", - "\n", - "answer = a * b / c\n", - "print \"%d * %d / %d = %d\"%(a,b,c,answer)\n", - "\n", - "answer = (a + c) * b / a\n", - "print \"(%d + %d) * %d / %d = %d\"%(a,c,b,a,answer)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "5 + 10 / 15 = 5\n", - "5 * 10 - 15 = 145\n", - "5 * 10 / 15 = 3\n", - "(5 + 15) * 10 / 5 = 40\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 3-4 , Page number: 37" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of modulus operator\n", - "\n", - "a = 4\n", - "b = 8\n", - "c = 13\n", - "\n", - "print \"%d mod %d = %d\"%(b,a,b%a)\n", - "print \"%d mod %d = %d\"%(c,a,c%a)\n", - "print \"%d mod %d = %d\"%(c,b,c%b)\n", - "\n", - "print \"The modulus operator is: %\"\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "8 mod 4 = 0\n", - "13 mod 4 = 1\n", - "13 mod 8 = 5\n", - "The modulus operator is: %\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 3-5 , Page number: 38" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of the unary minus\n", - "\n", - "a = -4\n", - "b = 5\n", - "c = -6\n", - "\n", - "print \"a = %d, b = %d, c = %d\\n\\n\"%(a,b,c)\n", - "print \" and now\\n\\n\"\n", - "\n", - "a = -a\n", - "b = -b\n", - "c = -c\n", - "\n", - "print \"a = %d, b = %d, c = %d\\n\\n\"%(a,b,c)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "a = -4, b = 5, c = -6\n", - "\n", - "\n", - " and now\n", - "\n", - "\n", - "a = 4, b = -5, c = 6\n", - "\n", - "\n" - ] - } - ], - "prompt_number": 5 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter4.ipynb b/The_Spirit_of_C/chapter4.ipynb deleted file mode 100755 index d13f3710..00000000 --- a/The_Spirit_of_C/chapter4.ipynb +++ /dev/null @@ -1,148 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:03f3f522026b7ff6e1acac7746805bcac18faba022541a5d8a73470a897fdf7a" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 4, Some More Data Types" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Prgoram 4-1 , Page number: 47" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An illustration of the use of floating point numbers\n", - "\n", - "# There is no loss of precision in python\n", - "a = 246.8\n", - "b = 135.79\n", - "\n", - "answer = a + b\n", - "print \"%f + %f = %f\"%(a,b,answer)\n", - "\n", - "answer = a - b\n", - "print \"%f - %f = %f\"%(a,b,answer)\n", - "\n", - "answer = a * b\n", - "print \"%f * %f = %f\"%(a,b,answer)\n", - "\n", - "answer = a / b\n", - "print \"%f / %f = %f\"%(a,b,answer)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "246.800000 + 135.790000 = 382.590000\n", - "246.800000 - 135.790000 = 111.010000\n", - "246.800000 * 135.790000 = 33512.972000\n", - "246.800000 / 135.790000 = 1.817512\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 4-2 , Page number: 49" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of mixed mode expressions\n", - "\n", - "a = 2\n", - "b = 12.34\n", - "\n", - "result = a * b\n", - "print \"%d * %f = %f\"%(a,b,result)\n", - "\n", - "print \"%f in scientific notation is %e\"%(result,result)\n", - "\n", - "answer = a * b\n", - "print \"%d * %f = %d\"%(a,b,answer)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "2 * 12.340000 = 24.680000\n", - "24.680000 in scientific notation is 2.468000e+01\n", - "2 * 12.340000 = 24\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 4-3 , Page number: 52" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "ltr_1 = \"C\"\n", - "ltr_2 = \" \"\n", - "ltr_3 = \"i\"\n", - "ltr_4 = \"s\"\n", - "ltr_5 = \"g\"\n", - "ltr_6 = \"r\"\n", - "ltr_7 = \"e\"\n", - "ltr_8 = \"a\"\n", - "ltr_9 = \"t\"\n", - "ltr_10 = \"!\"\n", - "\n", - "print \"%c%c%c%c%c%c%c%c%c%c%c\"%(ltr_1,ltr_2,ltr_3,ltr_4,ltr_2,ltr_5,ltr_6,ltr_7,ltr_8,ltr_9,ltr_10)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "C is great!\n" - ] - } - ], - "prompt_number": 3 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter5.ipynb b/The_Spirit_of_C/chapter5.ipynb deleted file mode 100755 index db420a5b..00000000 --- a/The_Spirit_of_C/chapter5.ipynb +++ /dev/null @@ -1,218 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:28fc7922ba91eac91816a02729f8986381706b8cffd1eaa9d68ce0cf435191aa" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 5, Making Decisions in C" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 5-2 , Page number: 65" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Using If command\n", - "\n", - "print \"Enter a positive integer under 10: \",\n", - "n = 7\n", - "print n\n", - "\n", - "if n == 7 :\t\t\t\t\t# In this case it is 7\n", - "\tprint \"I knew you'd select 7!\"\n", - "print \"Thank you for your cooperation.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a positive integer under 10: 7\n", - "I knew you'd select 7!\n", - "Thank you for your cooperation.\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 5-3 , Page number: 67" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Testing for leap years\n", - "\n", - "print \"Please enter a year:\",\n", - "year = 1948\t\t\n", - "print year\n", - "# If \"year\" is divisble by 4 and not divisible by 100 or divisible by 400, \"year\" is a leap year\n", - "\n", - "if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) :\n", - "\tprint \"%d is a leap year.\"%year\n", - "else :\n", - "\tprint \"%d is not a leap year.\"%year" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter a year: 1948\n", - "1948 is a leap year.\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 5-4 , Page number: 69" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of if...else clause\n", - "# Program to tell if a number is odd or even\n", - "\n", - "print \"Please enter an integer:\",\n", - "n = 13\n", - "print n\n", - "\n", - "# A number is even if it is divisible by 2\n", - "\n", - "if n % 2 != 0:\n", - "\tprint \"%d is odd\"%n\n", - "else:\n", - "\tprint \"%d is even\"%n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter an integer: 13\n", - "13 is odd\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 5-5 , Page number: 70" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Testing if a number is divisible by 7\n", - "\n", - "print \"Enter an integer:\",\n", - "n = 13\n", - "print n\n", - "\n", - "# A number is even if it is divisible by 2\n", - "\n", - "if n % 7 == 0:\n", - "\tprint \"%d is divisible by 7\"%n\n", - "else:\n", - "\tprint \"%d is not divisible by 7\"%n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter an integer: 13\n", - "13 is not divisible by 7\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 5-6 , Page number: 71" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Determining which of 2 numbers is larger\n", - "\n", - "print \"Enter an integer:\",\n", - "a = 4\n", - "print a\n", - "print \"Enter another integer: \",\n", - "b = 7\n", - "print b\n", - "\n", - "if a > b :\n", - "\tprint \"%d is greater than %d.\"%(a,b)\n", - "else:\n", - "\tprint \"%d is smaller than %d.\"%(a,b)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter an integer: 4\n", - "Enter another integer: 7\n", - "4 is smaller than 7.\n" - ] - } - ], - "prompt_number": 5 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter6.ipynb b/The_Spirit_of_C/chapter6.ipynb deleted file mode 100755 index f50ff06b..00000000 --- a/The_Spirit_of_C/chapter6.ipynb +++ /dev/null @@ -1,494 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:c565a0de8f316cbc19d18a7bdba735b6241f6d0c0c10c2d49245aa88403df9e7" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 6, The While and do...While Loops" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-1 , Page number: 86" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A first look at the while loop\n", - "\n", - "i = 1\n", - "\n", - "while i < 11:\n", - "\tprint \"This is line number %d\"%i\n", - "\ti += 1 \t\t\t# Python doesn't support \"++\" or \"--\" shorthand expressions" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is line number 1\n", - "This is line number 2\n", - "This is line number 3\n", - "This is line number 4\n", - "This is line number 5\n", - "This is line number 6\n", - "This is line number 7\n", - "This is line number 8\n", - "This is line number 9\n", - "This is line number 10\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-2 , Page number: 89" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Countdown to blastoff\n", - "\n", - "i = 10\n", - "\n", - "print \"Countdown\"\n", - "\n", - "while i >= 0 :\n", - "\tprint \"%d\"%i\n", - "\ti -= 1\n", - "\n", - "print \"\\n\\nBlastoff!\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Countdown\n", - "10\n", - "9\n", - "8\n", - "7\n", - "6\n", - "5\n", - "4\n", - "3\n", - "2\n", - "1\n", - "0\n", - "\n", - "\n", - "Blastoff!\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-3 , Page number: 90" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Hailstones\n", - "\n", - "counter = 0;\n", - "\n", - "print \"Enter a positive integer:\",\n", - "n = 51\n", - "print n\n", - "\n", - "nsave = n\n", - "\n", - "\n", - "while n > 1:\n", - "\tif n % 2 :\t\t\t# if (n % 2) is non-zero, n is not divisible by 2 and is therefore odd\n", - "\t\tn = n * 3 + 1\n", - "\telse :\n", - "\t\tn /= 2\n", - "\tcounter += 1\n", - "\tprint \"Step %4d: n = %4d\"%(counter,n)\n", - "\n", - "print \"\\n\\n%d went to 1 in %d steps.\"%(nsave,counter)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a positive integer: 51\n", - "Step 1: n = 154\n", - "Step 2: n = 77\n", - "Step 3: n = 232\n", - "Step 4: n = 116\n", - "Step 5: n = 58\n", - "Step 6: n = 29\n", - "Step 7: n = 88\n", - "Step 8: n = 44\n", - "Step 9: n = 22\n", - "Step 10: n = 11\n", - "Step 11: n = 34\n", - "Step 12: n = 17\n", - "Step 13: n = 52\n", - "Step 14: n = 26\n", - "Step 15: n = 13\n", - "Step 16: n = 40\n", - "Step 17: n = 20\n", - "Step 18: n = 10\n", - "Step 19: n = 5\n", - "Step 20: n = 16\n", - "Step 21: n = 8\n", - "Step 22: n = 4\n", - "Step 23: n = 2\n", - "Step 24: n = 1\n", - "\n", - "\n", - "51 went to 1 in 24 steps.\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-4 , Page number: 92" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Computing the factorial\n", - "\n", - "print \"Type in a positive integer:\",\n", - "n = 6\n", - "print n\n", - "\n", - "nsave = n\n", - "factorial = 1\n", - "\n", - "while n > 1 :\t\n", - "\tfactorial *= n\n", - "\tn -= 1\n", - "\n", - "print \"Factorial of %d = %d\"%(nsave,factorial)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Type in a positive integer: 6\n", - "Factorial of 6 = 720\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-5 , Page number: 93" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Computing the factorial (shorter version)\n", - "\n", - "print \"Type in a positive integer: \",\n", - "n = 6\n", - "print n\n", - "\n", - "nsave = n\n", - "factorial = 1\n", - "\n", - "while n > 1 :\n", - "\tfactorial *= n # Since python doesn't support '--' expression, the given program cannot be shortened\n", - "\tn -= 1\n", - "\n", - "print \"Factorial of %d = %d\"%(nsave,factorial)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Type in a positive integer: 6\n", - "Factorial of 6 = 720\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-6 , Page number: 95" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of do..while loops\n", - "\n", - "# This program reads in an integer, adds up the values of each of its individual digits, and prints out the sum.\n", - "\n", - "sum = 0\n", - "\n", - "print \"Type in an integer:\",\n", - "n = 1776\n", - "print n\n", - "\n", - "# There is no do...while loop in python\n", - "# An equivalent while loop can work\n", - "\n", - "while n > 0 :\n", - "\trightmost_digit = n % 10\t# Extract rightmost digit\n", - "\tsum += rightmost_digit\n", - "\tn /= 10\t\t\t\t\t\t# Move next digit into rightmost position\n", - "\n", - "print \"The sum of the digits is %d\"%sum\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Type in an integer: 1776\n", - "The sum of the digits is 21\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-7 , Page number: 97" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import sys\n", - "\n", - "# This program reads in an integer and prints it out backwards\n", - "\n", - "print \"Type in an integer: \",\n", - "n = 5746\n", - "print n\n", - "\n", - "print \"%d backwards is \"%n,\n", - "\n", - "while n > 0 :\n", - "\trightmost_digit = n % 10\n", - "\tsys.stdout.write(\"%d\"%rightmost_digit) # For printing in the same line without implicit whitespaces\n", - "\tn /= 10\n", - "print \t\t\t# For the new line\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Type in an integer: 5746\n", - "5746 backwards is 6475\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-8 , Page number: 99" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A program to read in numbers and print out their squares\n", - "\n", - "print \"Enter an integer (negative to quit):\",\n", - "n = 5\n", - "print n\n", - "\n", - "while n > 0 :\n", - "\tprint \"%d squared is %d\"%(n,n*n)\n", - "\tprint \"\\nEnter an integer (negative to quit):\",\n", - "\tn = -1\n", - "\tprint n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter an integer (negative to quit): 5\n", - "5 squared is 25\n", - "\n", - "Enter an integer (negative to quit): -1\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-9 , Page number: 100" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A program to read in radius values and print out the circumference and area\n", - "\n", - "pi = 3.1416\n", - "\n", - "print \"Enter radius (negative to quit):\",\n", - "radius = 4.5\n", - "print radius\n", - "\n", - "while radius >= 0.0 :\n", - "\tprint \"The circumference is %f\"%(2.0 * pi * radius)\n", - "\tprint \"The area is %f\"%(pi * radius * radius)\n", - "\tprint \"\\nEnter radius (negative to quit):\",\n", - "\tradius = -1.0\n", - "\tprint radius\n", - "\t\n", - "\t" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter radius (negative to quit): 4.5\n", - "The circumference is 28.274400\n", - "The area is 63.617400\n", - "\n", - "Enter radius (negative to quit): -1.0\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 6-10 , Page number: 101" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "\n", - "# A program to read in length and width and print out the perimeter and area\n", - "\n", - "print \"Enter length and width\"\n", - "print \" (one or both negative to quit):\",\n", - "length = 4\n", - "width = 5\n", - "print length,width\n", - "\n", - "while length > 0 and width > 0 : \n", - "\tprint \"The perimeter is %d\"%(2*(length+width))\n", - "\tprint \"The area is %d\"%(length*width)\n", - "\tprint \"\\nEnter length and width\"\n", - "\tprint \" (one or both negative to quit):\",\n", - "\tlength = -1\n", - "\twidth = 0\n", - "\tprint length,width\t" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter length and width\n", - " (one or both negative to quit): 4 5\n", - "The perimeter is 18\n", - "The area is 20\n", - "\n", - "Enter length and width\n", - " (one or both negative to quit): -1 0\n" - ] - } - ], - "prompt_number": 10 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter7.ipynb b/The_Spirit_of_C/chapter7.ipynb deleted file mode 100755 index 33f7a4f3..00000000 --- a/The_Spirit_of_C/chapter7.ipynb +++ /dev/null @@ -1,690 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:245ea275fa7c7004a79eeeeb6b3fc9780b5a19707d8b95c7a675d2142428b630" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 7, The for loop" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-1 , Page number: 119" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A simple illustration of for loop\n", - "\n", - "for i in range(1,11,1):\t\t# The loop starts with i=1 and continues until i<11 with its value increasing by 1 each time \n", - "\tprint \"This is line number %d\"%i" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is line number 1\n", - "This is line number 2\n", - "This is line number 3\n", - "This is line number 4\n", - "This is line number 5\n", - "This is line number 6\n", - "This is line number 7\n", - "This is line number 8\n", - "This is line number 9\n", - "This is line number 10\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-2 , Page number: 120" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another simple illustration of a for loop\n", - "\n", - "print \"Countdown\"\n", - "\n", - "for i in range(10,-1,-1):\n", - "\tprint i\n", - "print \"\\n\\nBlastoff!\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Countdown\n", - "10\n", - "9\n", - "8\n", - "7\n", - "6\n", - "5\n", - "4\n", - "3\n", - "2\n", - "1\n", - "0\n", - "\n", - "\n", - "Blastoff!\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-3 , Page number: 121" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Calculating the squares of integers 1 through 10\n", - "\n", - "for i in range (1,11,1):\n", - "\tprint \"%d squared = %d\"%(i,i*i)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "1 squared = 1\n", - "2 squared = 4\n", - "3 squared = 9\n", - "4 squared = 16\n", - "5 squared = 25\n", - "6 squared = 36\n", - "7 squared = 49\n", - "8 squared = 64\n", - "9 squared = 81\n", - "10 squared = 100\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-4 , Page number: 121" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A better looking program to calculate squares\n", - "\n", - "print \"Table of Results\\n\"\n", - "print \" i i squared\"\n", - "print \" - ---------\"\n", - "\n", - "for i in range(1,11,1):\n", - "\tprint \"%4d%13d\"%(i,i*i)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Table of Results\n", - "\n", - " i i squared\n", - " - ---------\n", - " 1 1\n", - " 2 4\n", - " 3 9\n", - " 4 16\n", - " 5 25\n", - " 6 36\n", - " 7 49\n", - " 8 64\n", - " 9 81\n", - " 10 100\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-5 , Page number: 122" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Finding the sum of integers 1 through 100\n", - "\n", - "sum = 0\n", - "n = 100\n", - "\n", - "for i in range(1,n+1,1):\t\t# The loop starts with i=1 and continues until i<11 with its value increasing by 1 each time \n", - "\tsum += i\t\n", - "\n", - "print \"The sum of integers from 1 to %d is %d\"%(n,sum)\n", - "print \"\\n\\nThis is confirmed by the Gauss Method: %d\"%(n*(n+1)/2)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The sum of integers from 1 to 100 is 5050\n", - "\n", - "\n", - "This is confirmed by the Gauss Method: 5050\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-6 , Page number: 124" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Finding the sum of the integers 1 through MAX_VAL\n", - "\n", - "# There is no #define command for defining constants\n", - "# We can use a function that returns the value (although it is not very efficient to do so)\n", - "\n", - "def MAX_VAL():\n", - "\treturn 100\t\t\t# The value of MAX_VAL\n", - "\n", - "sum = 0\n", - "\n", - "for i in range (0,MAX_VAL()+1,1):\n", - "\tsum += i\n", - "\n", - "print \"The sum of the integers from 1 to %d is %d\"%(MAX_VAL(),sum)\n", - "print \"\\n\\nThis is confirmed by the Gauss Method : %d\"%(MAX_VAL()*(MAX_VAL()+1)/2)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The sum of the integers from 1 to 100 is 5050\n", - "\n", - "\n", - "This is confirmed by the Gauss Method : 5050\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-7 , Page number: 125" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Calculating the sum of integers 1 through n\n", - "\n", - "sum = 0\n", - "\n", - "print \"Please type in a positive integer:\",\n", - "n = 50\n", - "print n\n", - "\n", - "for i in range (1,n+1,1):\n", - "\tsum += i\n", - "\n", - "print \"\\nThe sum of the integers from 1 to %d is: %d\"%(n,sum)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please type in a positive integer: 50\n", - "\n", - "The sum of the integers from 1 to 50 is: 1275\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-8 , Page number: 125" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Calculating the sum of integers low through high\n", - "\n", - "sum = 0\n", - "\n", - "print \"Please type in the low bound:\",\n", - "low = 3\n", - "print low\n", - "print \"Please type in the high bound:\",\n", - "high = 9\n", - "print high\n", - "\n", - "for i in range(low,high+1,1):\n", - "\tsum += i\n", - "\n", - "print \"\\nThe sum of integers from %d to %d is: %d\"%(low,high,sum)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please type in the low bound: 3\n", - "Please type in the high bound: 9\n", - "\n", - "The sum of integers from 3 to 9 is: 42\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-9 , Page number: 127" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Calculating the sum of integers low through high with an exchange if necessary\n", - "\n", - "sum = 0\n", - "\n", - "print \"Please type in the low bound:\",\n", - "low = 30\n", - "print low\n", - "print \"Please type in the high bound:\",\n", - "high = 15\n", - "print high\n", - "\n", - "if high < low :\n", - "\tprint \"Oops! Wrong order.\"\n", - "\tprint \"Never mind, I'll switch them.\"\n", - "\t# Code for swapping using temporary variable\n", - "\ttemp = low\n", - "\tlow = high\n", - "\thigh = temp\n", - "\n", - "for i in range(low,high+1,1):\n", - "\tsum += i\n", - "\n", - "print \"\\nThe sum of integers from %d to %d is: %d\"%(low,high,sum)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please type in the low bound: 30\n", - "Please type in the high bound: 15\n", - "Oops! Wrong order.\n", - "Never mind, I'll switch them.\n", - "\n", - "The sum of integers from 15 to 30 is: 360\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-10 ,Page number: 128" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of a nest of for loops\n", - "\n", - "n = 5\n", - "for i in range(1,7,1):\n", - "\tprint \"\\n\\nPlease type in a positive integer: \",\n", - "\tprint n\n", - "\n", - "\tsum = 0\n", - "\tfor j in range(1,n+1,1):\n", - "\t\tsum += j\n", - "\tprint \"\\nThe sum of integers from 1 to %d is %d\"%(n,sum)\n", - "\t\n", - "\tn += 5\t\t\t# Just to match the exercise's values" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "\n", - "Please type in a positive integer: 5\n", - "\n", - "The sum of integers from 1 to 5 is 15\n", - "\n", - "\n", - "Please type in a positive integer: 10\n", - "\n", - "The sum of integers from 1 to 10 is 55\n", - "\n", - "\n", - "Please type in a positive integer: 15\n", - "\n", - "The sum of integers from 1 to 15 is 120\n", - "\n", - "\n", - "Please type in a positive integer: 20\n", - "\n", - "The sum of integers from 1 to 20 is 210\n", - "\n", - "\n", - "Please type in a positive integer: 25\n", - "\n", - "The sum of integers from 1 to 25 is 325\n", - "\n", - "\n", - "Please type in a positive integer: 30\n", - "\n", - "The sum of integers from 1 to 30 is 465\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-11 , Page number: 130" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Another illustration of a nest of for loops\n", - "\n", - "print \"How many values of n do you wish to enter? \",\n", - "runs = 3\n", - "print runs\n", - "print \"\\n\\n\"\n", - "\n", - "n = 9\n", - "\n", - "for i in range(1,runs+1,1) :\n", - "\tprint \"\\n\\nPLease type in a positive integer: \",\n", - "\tprint n\n", - "\n", - "\tsum = 0\n", - "\tfor j in range(1,n+1,1) :\n", - "\t\tsum += j\n", - "\tprint \"\\nThe sum of the integers from 1 to %d is: %d\"%(n,sum)\n", - "\tn *= 3\t\t\t# Just a random increment" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many values of n do you wish to enter? 3\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "PLease type in a positive integer: 9\n", - "\n", - "The sum of the integers from 1 to 9 is: 45\n", - "\n", - "\n", - "PLease type in a positive integer: 27\n", - "\n", - "The sum of the integers from 1 to 27 is: 378\n", - "\n", - "\n", - "PLease type in a positive integer: 81\n", - "\n", - "The sum of the integers from 1 to 81 is: 3321\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-12 , Page number: 131" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Newton-Rapshon method for calculating square roots\n", - "\n", - "def EPSILON() :\n", - "\t\"\"\" USING AS A CONSTANT \"\"\"\n", - "\treturn 0.0001\n", - "\n", - "print \"What number do you want to kate the square root of?\",\n", - "n = 17.0\n", - "print n\n", - "\n", - "while n < 0.0 :\n", - "\tprint \"Please enter a non-negative number!\"\n", - "\tn = 3 \t\t\t\t\t# Just a random positive number\n", - "\n", - "# The for loop statement given in the example(from the book) is not possible to code in python\n", - "# So, breaking it down to while loop\n", - "\n", - "guess = n / 2.0\n", - "while (guess * guess - n > EPSILON() or guess * guess - n < -EPSILON()) :\n", - "\tguess = (guess + n/guess) / 2.0\n", - "\t\n", - "print \"I calculate the square root of %f to be %.4f\"%(n,guess)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "What number do you want to kate the square root of? 17.0\n", - "I calculate the square root of 17.000000 to be 4.1231\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-13 , Page number: 133" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# General addition table for a number n\n", - "\n", - "print \"What number do you want a table for?\",\n", - "n = 10\n", - "print n\n", - "\n", - "j = n\n", - "for i in range (0,n+1,1) :\n", - "\tprint \"%3d + %3d = %3d\"%(i,j,n)\n", - "\tj -= 1" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "What number do you want a table for? 10\n", - " 0 + 10 = 10\n", - " 1 + 9 = 10\n", - " 2 + 8 = 10\n", - " 3 + 7 = 10\n", - " 4 + 6 = 10\n", - " 5 + 5 = 10\n", - " 6 + 4 = 10\n", - " 7 + 3 = 10\n", - " 8 + 2 = 10\n", - " 9 + 1 = 10\n", - " 10 + 0 = 10\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 7-14 , Page number: 135" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Generate addition table for a number n, shorter loop\n", - "\n", - "print \"What number do you want a table for?\",\n", - "n = 10\n", - "print n\n", - "\n", - "# Since the range() function supports at max 3 arguments, no possible shortening is possilbe in python\n", - "j = n\n", - "for i in range (0,n+1,1):\n", - "\tprint \"%3d + %3d = %3d\"%(i,j,n)\n", - "\tj -= 1\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "What number do you want a table for? 10\n", - " 0 + 10 = 10\n", - " 1 + 9 = 10\n", - " 2 + 8 = 10\n", - " 3 + 7 = 10\n", - " 4 + 6 = 10\n", - " 5 + 5 = 10\n", - " 6 + 4 = 10\n", - " 7 + 3 = 10\n", - " 8 + 2 = 10\n", - " 9 + 1 = 10\n", - " 10 + 0 = 10\n" - ] - } - ], - "prompt_number": 8 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter8.ipynb b/The_Spirit_of_C/chapter8.ipynb deleted file mode 100755 index baae6f9a..00000000 --- a/The_Spirit_of_C/chapter8.ipynb +++ /dev/null @@ -1,567 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:db9d52743fdd42516fb59e7618f0fc71d76d4ba3cb21fd098bf4fd004c40683b" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 8, A Detailed Look at the printf and scanf Functions" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-1 , Page number: 149" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A print demo\n", - "# The conversion specification for numbers.\n", - "\n", - "print \"Case 1 :%d:\"%123\n", - "\n", - "print \"Case 2 :%0d:\"%123\n", - "\n", - "print \"Case 3 :%2d:\"%123\n", - "\n", - "print \"Case 4 :%8d:\"%123\n", - "\n", - "print \"Case 5 :%-8d:\"%123\n", - "\n", - "print \"Case 6 :%8d:\"%-123\n", - "\n", - "print \"Case 7 :%f:\"%123.456\n", - "\n", - "print \"Case 8 :%f:\"%123.4567896\n", - "\n", - "print \"Case 9 :%e:\"%123.456\n", - "\n", - "print \"Case 10 :%e:\"%1.23456e2\n", - "\n", - "print \"Case 11 :%e:\"%123.456e+0\n", - "\n", - "print \"Case 12 :%f:\"%1.23456E+02\n", - "\n", - "print \"Case 13 :%4.2f:\"%123.456\n", - "\n", - "print \"Case 14 :%8.2f:\"%123.456\n", - "\n", - "print \"Case 15 :%.3e:\"%123.456\n", - "\n", - "print \"Case 16 :%12.3e:\"%123.456\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Case 1 :123:\n", - "Case 2 :123:\n", - "Case 3 :123:\n", - "Case 4 : 123:\n", - "Case 5 :123 :\n", - "Case 6 : -123:\n", - "Case 7 :123.456000:\n", - "Case 8 :123.456790:\n", - "Case 9 :1.234560e+02:\n", - "Case 10 :1.234560e+02:\n", - "Case 11 :1.234560e+02:\n", - "Case 12 :123.456000:\n", - "Case 13 :123.46:\n", - "Case 14 : 123.46:\n", - "Case 15 :1.235e+02:\n", - "Case 16 : 1.235e+02:\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-2 , Page number: 152" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# A print demo:\n", - "# Interpretation of values\n", - "\n", - "# python is dynamically-typed language\n", - "# which is why declaration of variables is omitted\n", - "n = -123\n", - "c = \"V\"\n", - "\n", - "print \"%d\"%n\n", - "print \"%u\"%n\t\t# python correctly interprets the negative(unsigned) integer\n", - "\n", - "print \"%c\"%c\n", - "# print \"%d\"%c \t# python doesn't support implicit type-casting from integer to character or character to integer\n", - "print \"%c\"%86" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "-123\n", - "-123\n", - "V\n", - "V\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-3 , Page number: 153" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Printing out the ASCII value of a character\n", - "\n", - "# There is no do..while loop in python\n", - "# Hence, we will be using while loop \n", - "\n", - "\n", - "answer = \"y\"\n", - "while answer == \"y\" :\n", - "\tprint \"Enter a character:\",\n", - "\tin_char = \"a\"\n", - "\tprint in_char\n", - "\n", - "\tprint \"The ASCII value of %c is %d\"%(in_char,ord(in_char)) \t# ord() function returns the ascii(int) value of a character\n", - "\n", - "\tprint \"\\nAnother? (y/n)\",\n", - "\tanswer = \"n\"\t\t\t# To terminate the loop\n", - "\tprint answer\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a character: a\n", - "The ASCII value of a is 97\n", - "\n", - "Another? (y/n) n\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-4 , Page number: 154" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Converting a digit to a number by using its ASCII value\n", - "\n", - "print \"Enter a digit:\",\n", - "digit = \"A\"\n", - "print digit\n", - "\n", - "if digit < \"0\" or digit > \"9\" :\n", - "\tprint \"That's not a digit\"\n", - "else :\n", - "\tprint \"The digit you typed is %c\"%digit" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a digit: A\n", - "That's not a digit\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-5 , Page number: 156" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Converting a letter from lower to upper case or vice-versa\n", - "\n", - "answer = \"y\"\n", - "while answer == \"y\" :\n", - "\tprint \"\\n\\n\\nEnter a letter:\",\n", - "\tletter = \"A\"\n", - "\tprint letter\n", - "\n", - "\tif letter >= \"A\" and letter < \"Z\" :\t\t# Letter is uppercase\n", - "\t\tprint \"Lower case is: %c\"%chr(ord(letter) + (ord(\"a\")-ord(\"A\")))\n", - "\telif letter >= \"a\" and letter <= \"z\" :\t# Letter is lowercase\n", - "\t\tprint \"Upper case is: %c\"%chr(ord(letter) - (ord(\"a\")-ord(\"A\")))\n", - "\telse :\n", - "\t\tprint \"That's not a letter\"\n", - "\n", - "\t# Ask if the user wants to go again\n", - "\tprint \"\\nAnother? (y/n)\",\n", - "\tanswer = \"n\"\t\t# To terminate the loop\n", - "\tprint answer" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "\n", - "\n", - "Enter a letter: A\n", - "Lower case is: a\n", - "\n", - "Another? (y/n) n\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-6 , Page number: 157" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Example of using control characters\n", - "\n", - "print \"The word apple\\b\\b\\b\\b\\b_____ is underlined.\\n\"\n", - "print \"The last word in this sentence is way over\\t\\there.\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "The word apple\b\b\b\b\b_____ is underlined.\n", - "\n", - "The last word in this sentence is way over\t\there.\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-7 , Page number: 158" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# How to print special characters\n", - "\n", - "print \"This is how to print a double-quote: \\\"\"\n", - "print \"This is how to print a backslash: \\\\\"\n", - "# this is how to write an apostrophe character\n", - "c = '\\''\n", - "print \"c now contains: %c\"%c\n", - "print \"This is how to print a percent-sign: %\"\t\t# no escape sequence required for % in python" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is how to print a double-quote: \"\n", - "This is how to print a backslash: \\\n", - "c now contains: '\n", - "This is how to print a percent-sign: %\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-8 , Page number: 160" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Octal and hexadecimal notation\n", - "\n", - "a = 27\n", - "b = 033\n", - "c = 0x1b\n", - "\n", - "print \"a = %d, b = %d, c = %d\"%(a,b,c)\n", - "print \"a = %o, b = %o, c = %o\"%(a,b,c)\n", - "print \"a = %x, b = %x, c = %x\"%(a,b,c)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "a = 27, b = 27, c = 27\n", - "a = 33, b = 33, c = 33\n", - "a = 1b, b = 1b, c = 1b\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-9 , Page number: 162" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of some ways to print a string\n", - "\n", - "print \"This is simply a control string.\"\n", - "\n", - "print \"%s\"%(\"The control string just tells how to print this.\")\n", - "\n", - "print \"%s\"%(\"Newline may be put here, instead!.\\n\"),\n", - "\n", - "print \"%s %s %s %s %s\"%(\"This\",\"is\",\"valid\",\"in\",\"C.\")\n", - "\n", - "print \"%s %s also %s %s %s\"%(\"This\",\"is\",\"valid\",\"in\",\"C.\")\n", - "\n", - "print \"%s %s %s %s %s %s %s\"%(\"This\",\"is\",\"valid\",\"in\",\"C\",\"also.\",\"\\n\")," - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This is simply a control string.\n", - "The control string just tells how to print this.\n", - "Newline may be put here, instead!.\n", - "This is valid in C.\n", - "This is also valid in C.\n", - "This is valid in C also. \n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-10 , Page number: 163" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# %s conversion specification demonstrated\n", - "\n", - "print \"Case 1 :%s:\"%(\"A string\")\n", - "\n", - "print \"Case 2 :%3s:\"%(\"A string\")\n", - "\n", - "print \"Case 3 :%12s:\"%(\"A string\")\n", - "\n", - "print \"Case 4 :%-12s:\"%(\"A string\")\n", - "\n", - "print \"Case 5 :%12.6s:\"%(\"A string\")\n", - "\n", - "print \"Case 6 :%12.12s:\"%(\"A string\")\n", - "\n", - "print \"Case 7 :%.6s:\"%(\"A string\")\n", - "\n", - "print \"Case 8 :%-12.6s:\"%(\"A string\")" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Case 1 :A string:\n", - "Case 2 :A string:\n", - "Case 3 : A string:\n", - "Case 4 :A string :\n", - "Case 5 : A stri:\n", - "Case 6 : A string:\n", - "Case 7 :A stri:\n", - "Case 8 :A stri :\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-11 , Page number: 164 " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Some miscellaneous conversion sppecifications\n", - "\n", - "a = 123456789L\n", - "\n", - "print \"%d\"%a\t\t\t# in python both int and long are unified..so %d specifier valid unlike in C\n", - "print \"%ld\"%a\n", - "print \":%c:\"%(\"V\")\n", - "print \":%0c:\"%(\"V\")\n", - "print \":%5c:\"%(\"V\")\n", - "print \":%-5c:\"%(\"V\")" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "123456789\n", - "123456789\n", - ":V:\n", - ":V:\n", - ": V:\n", - ":V :\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 8-13 , Page number: 171" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Reading decimal,octal and hex numbers\n", - "\n", - "print \"Enter a number in decimal:\",\n", - "a = 27\n", - "print a\n", - "print \"You entered %d\"%a\n", - "\n", - "print \"\\nEnter a number in octal:\",\n", - "a = 033\t\t\t# O is added in the front to specify octal value\n", - "print a\n", - "print \"You entered %o, or %d in decimal\"%(a,a)\n", - "\n", - "print \"\\nEnter a number in hex:\",\n", - "a = 0x1B\n", - "print a\n", - "print \"You entered %x, or %d in decimal\"%(a,a)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a number in decimal: 27\n", - "You entered 27\n", - "\n", - "Enter a number in octal: 27\n", - "You entered 33, or 27 in decimal\n", - "\n", - "Enter a number in hex: 27\n", - "You entered 1b, or 27 in decimal\n" - ] - } - ], - "prompt_number": 12 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/chapter9.ipynb b/The_Spirit_of_C/chapter9.ipynb deleted file mode 100755 index ae4771e3..00000000 --- a/The_Spirit_of_C/chapter9.ipynb +++ /dev/null @@ -1,590 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:13a3347e13ed5ee09ae2833e49a517873801778756103a4da905a96a5997fa89" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 9, User-Defined Functions" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-1 , Page number: 187" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Using a function as an abbreviation\n", - "\n", - "# In python, the user defined functions have to defined before making a call to them\n", - "\n", - "def chorus() :\n", - "\tprint \"%60s\"%\"With a knick-knack-paddy-whack,\"\n", - "\tprint \"%60s\"%\"Give the dog a bone,\"\n", - "\tprint \"%60s\\n\"%\"This old man went rolling home.\"\n", - "\n", - "print \"This old man, he played two,\"\n", - "print \"He played knick-knack on my thumb,\"\n", - "chorus()\n", - "\n", - "print \"This old man, he played one,\"\n", - "print \"He played knick-knack on my shoe,\"\n", - "chorus()\n", - "\n", - "print \"This old man, he played three,\"\n", - "print \"He played knick-knack on my knee,\"\n", - "chorus()\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "This old man, he played two,\n", - "He played knick-knack on my thumb,\n", - " With a knick-knack-paddy-whack,\n", - " Give the dog a bone,\n", - " This old man went rolling home.\n", - "\n", - "This old man, he played one,\n", - "He played knick-knack on my shoe,\n", - " With a knick-knack-paddy-whack,\n", - " Give the dog a bone,\n", - " This old man went rolling home.\n", - "\n", - "This old man, he played three,\n", - "He played knick-knack on my knee,\n", - " With a knick-knack-paddy-whack,\n", - " Give the dog a bone,\n", - " This old man went rolling home.\n", - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-2 , Page number: 189" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Demonstration of one use of a function\n", - "\n", - "def get_an_int() :\n", - "\t\"\"\"RETURNS AN INTEGER\"\"\"\t\t# This statement is just a description of the function (specific feature of python)\n", - "\treturn 5\n", - "\n", - "print \"Enter the length of a rectangle:\",\n", - "length = get_an_int()\n", - "print length\n", - "\n", - "print \"Enter the width:\",\n", - "width = get_an_int()\n", - "print width\n", - "\n", - "print \"\\nThe area is %d\"%(length*width)\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the length of a rectangle: 5\n", - "Enter the width: 5\n", - "\n", - "The area is 25\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-3 , Page number: 191" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# An attempt at communication between functions\n", - "\n", - "def modify_i() :\n", - "\ti = 3\n", - "\t\n", - "i = 1\n", - "print \"i is now equal to %d\"%i\n", - "\n", - "modify_i()\n", - "print \"i is now equal to %d\"%i\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "i is now equal to 1\n", - "i is now equal to 1\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-4 , Page number: 192" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Demonstration of the use of a global variable\n", - "\n", - "# Making variable global\n", - "\n", - "def get_an_int() :\n", - "\treturn 5\n", - "\n", - "def calc_area() :\n", - "\tarea = length*width\n", - "\n", - "globals()\t\t\t# The command simply makes all the variables declared so far as global variables\n", - "print \"Enter the length of a rectangle:\",\n", - "length = get_an_int()\n", - "print length\n", - "\n", - "print \"Enter the width:\",\n", - "width = get_an_int()\n", - "print width\n", - "\n", - "calc_area()\n", - "print \"\\nThe area is %d\"%(length*width)\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the length of a rectangle: 5\n", - "Enter the width: 5\n", - "\n", - "The area is 25\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-5 , Page number: 194" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Introduction to parameters\n", - "\n", - "def print_an_int(i) :\n", - "\t\"\"\"The function prints an integer passed as a parameter\"\"\"\n", - "\tprint i\n", - "\n", - "\n", - "n = 3\n", - "\n", - "print \"Passing a constant:\",\n", - "print_an_int(5)\n", - "\n", - "print \"\\n\\nPassing a variable's value:\",\n", - "print_an_int(n)\n", - "\n", - "print \"\\n\\nPassing the value of an expression:\",\n", - "print_an_int(n * 4 + 1)\n", - "\n", - "\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Passing a constant: 5\n", - "\n", - "\n", - "Passing a variable's value: 3\n", - "\n", - "\n", - "Passing the value of an expression: 13\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-6 , Page number: 196" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Demonstration of how a procedure cannot modify the contents of a variable passed as a parameter\n", - "\n", - "def modify(i) :\n", - "\ti = 3\n", - "\n", - "i = 1\n", - "\n", - "print \"i is now equal to %d\"%i\n", - "\n", - "modify(i)\n", - "\n", - "print \"i is now equal to %d\"%i" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "i is now equal to 1\n", - "i is now equal to 1\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-7 , Page number: 197 " - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Hailstones, demonstrating the use of a parameter\n", - "\n", - "def step(i) :\n", - "\t\"\"\"This function returns its parameter i divided by 2 if i is even else it returns i multiplied by 3 and incremented by 1\"\"\"\n", - "\tif (i % 2) :\n", - "\t\treturn (i * 3 + 1)\n", - "\telse : \n", - "\t\treturn (i / 2)\n", - "\n", - "counter = 0\n", - "\n", - "print \"Enter a positive integer\",\n", - "n = 25\n", - "print n\n", - "\n", - "nsave = n\n", - "\n", - "while n > 1 :\n", - "\tn = step(n)\n", - "\tcounter += 1\n", - "\tprint \"Step %4d: n = %4d\"%(counter,n)\n", - "\n", - "print \"\\n\\n%d went to 1 in %d steps.\"%(nsave,counter)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter a positive integer 25\n", - "Step 1: n = 76\n", - "Step 2: n = 38\n", - "Step 3: n = 19\n", - "Step 4: n = 58\n", - "Step 5: n = 29\n", - "Step 6: n = 88\n", - "Step 7: n = 44\n", - "Step 8: n = 22\n", - "Step 9: n = 11\n", - "Step 10: n = 34\n", - "Step 11: n = 17\n", - "Step 12: n = 52\n", - "Step 13: n = 26\n", - "Step 14: n = 13\n", - "Step 15: n = 40\n", - "Step 16: n = 20\n", - "Step 17: n = 10\n", - "Step 18: n = 5\n", - "Step 19: n = 16\n", - "Step 20: n = 8\n", - "Step 21: n = 4\n", - "Step 22: n = 2\n", - "Step 23: n = 1\n", - "\n", - "\n", - "25 went to 1 in 23 steps.\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-8 , Page number: 200" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Square root program demonstrating a non-integer function and parameter\n", - "\n", - "def EPSILON() :\n", - "\t\"\"\"replacement of #define constant\"\"\"\n", - "\treturn 0.0001\n", - "\n", - "def sqrt(n) :\n", - "\t\"\"\"returns the square root of the number supplied as parameter using Newton-Raphson method\"\"\"\n", - "\tif n > 0.0 :\n", - "\t\t# The for loop statement given in the example(from the book) is not possible to code in python\n", - "\t\t# So, breaking it down to while loop\n", - "\t\tguess = n / 2.0\n", - "\t\twhile (guess * guess - n > EPSILON() or guess * guess - n < -EPSILON()) :\n", - "\t\t\tguess = (guess + n/guess) / 2.0\n", - "\t\treturn guess\n", - "\telse :\n", - "\t\treturn (-1.0)\n", - "\n", - "print \"Enter the number whose square root you want:\",\n", - "x = 17.1\n", - "print x\n", - "\n", - "print \"The square root of %f is %f\"%(x,sqrt(x))" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the number whose square root you want: 17.1\n", - "The square root of 17.100000 is 4.135215\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-9 , Page number: 201" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Testing for leap years using a boolean function\n", - "\n", - "def is_leap(year) :\n", - "\t\"\"\"A function that returns true if the given year is a leap year else returns false\"\"\"\n", - "\treturn (year % 4 == 0 and year % 100 != 0 or year % 400 == 0)\n", - "\n", - "print \"Please enter a year:\",\n", - "year = 2000\n", - "print year\n", - "\n", - "if (is_leap(year)) :\n", - "\tprint \"%d is a leap year.\"%year\n", - "else :\n", - "\tprint \"%d is not a leap year.\"%year" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Please enter a year: 2000\n", - "2000 is a leap year.\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-10 , Page number: 203" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Demonstration of the use of multiple parameters\n", - "\n", - "def get_an_int() :\n", - "\t\"\"\"returns a random integer\"\"\"\n", - "\treturn 7\n", - "\n", - "def area(length,width) :\n", - "\t\"\"\"returns the area of the rectangle using its parameters length and width\"\"\"\n", - "\treturn length * width\n", - "\n", - "print \"Enter the length of a rectangle:\",\n", - "length = get_an_int()\n", - "print length\n", - "\n", - "print \"Enter the width:\",\n", - "width = get_an_int()\n", - "print width\n", - "\n", - "print \"\\nThe area is %d.\"%area(length,width)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter the length of a rectangle: 7\n", - "Enter the width: 7\n", - "\n", - "The area is 49.\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Program 9-11 , Page number: 204" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Illustration of a 2-parameter function: raising a number to a power \n", - "\n", - "def power(i,x) :\n", - "\t\"\"\"returns the values of i to the power of x\"\"\"\n", - "\taccum = 1\n", - "\tif x < 0 :\n", - "\t\treturn 0\n", - "\tfor index in range (0,x,1) :\n", - "\t\taccum *= i\n", - "\treturn accum\n", - "\n", - "answer = \"y\"\n", - "\n", - "while answer == \"y\" or answer == \"Y\" :\n", - "\tprint \"\\nPlease enter an integer:\",\n", - "\tnumber = 4\n", - "\tprint number\n", - "\n", - "\tprint \"Please enter a non-negative integral power\"\n", - "\tprint \" to which the first number is to be raised\",\n", - "\texponent = 2\n", - "\tprint exponent\n", - "\n", - "\twhile exponent < 0 :\n", - "\t\tprint \"***\\7Please make that a non-negative integer\"\n", - "\t\texponent = 2 \t\t\t\t# randomly making the exponent non-negative\n", - "\n", - "\tprint \"%d raised to the power %d is %ld.\"%(number,exponent,power(number,exponent))\n", - "\n", - "\t# see if the user wants to keep going\n", - "\n", - "\tprint \"\\nAnother calculation? (y/n)\",\n", - "\tanswer = \"n\"\n", - "\tprint answer" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "\n", - "Please enter an integer: 4\n", - "Please enter a non-negative integral power\n", - " to which the first number is to be raised 2\n", - "4 raised to the power 2 is 16.\n", - "\n", - "Another calculation? (y/n) n\n" - ] - } - ], - "prompt_number": 11 - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file diff --git a/The_Spirit_of_C/screenshots/Screenshot_1.png b/The_Spirit_of_C/screenshots/Screenshot_1.png Binary files differdeleted file mode 100755 index dc7d579e..00000000 --- a/The_Spirit_of_C/screenshots/Screenshot_1.png +++ /dev/null diff --git a/The_Spirit_of_C/screenshots/Screenshot_2.png b/The_Spirit_of_C/screenshots/Screenshot_2.png Binary files differdeleted file mode 100755 index 6762ca01..00000000 --- a/The_Spirit_of_C/screenshots/Screenshot_2.png +++ /dev/null diff --git a/The_Spirit_of_C/screenshots/Screenshot_3.png b/The_Spirit_of_C/screenshots/Screenshot_3.png Binary files differdeleted file mode 100755 index e55dd42a..00000000 --- a/The_Spirit_of_C/screenshots/Screenshot_3.png +++ /dev/null |