diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /Beginning_C_By_Ivon_Horton/chapter11.ipynb | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2 Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip |
adding book
Diffstat (limited to 'Beginning_C_By_Ivon_Horton/chapter11.ipynb')
-rw-r--r-- | Beginning_C_By_Ivon_Horton/chapter11.ipynb | 2119 |
1 files changed, 2119 insertions, 0 deletions
diff --git a/Beginning_C_By_Ivon_Horton/chapter11.ipynb b/Beginning_C_By_Ivon_Horton/chapter11.ipynb new file mode 100644 index 00000000..b85dcda1 --- /dev/null +++ b/Beginning_C_By_Ivon_Horton/chapter11.ipynb @@ -0,0 +1,2119 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11: Structuring Data" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.1, page no. 403" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Exercising the horse\n", + "\"\"\"\n", + "\n", + "class Horse:\n", + " age = 0\n", + " height = 0\n", + " name = \"\"\n", + " father = \"\"\n", + " mother = \"\"\n", + "\n", + "my_horse = Horse()\n", + "print \"Enter the name of the horse: \",\n", + "my_horse.name = raw_input()\n", + " \n", + "print \"How old is %s? \" % my_horse.name,\n", + "my_horse.age = int(raw_input())\n", + " \n", + "print \"How high is %s ( in hands )? \" %my_horse.name,\n", + "my_horse.height = int(raw_input())\n", + " \n", + "print \"Who is %s's father? \" %my_horse.name,\n", + "my_horse.father = raw_input()\n", + " \n", + "print \"Who is %s's mother? \" %my_horse.name,\n", + "my_horse.mother = raw_input()\n", + " \n", + "print \"%s is %d years old, %d hands high, \" %(my_horse.name, my_horse.age, my_horse.height),\n", + "print \"and has %s and %s as parents. \" %(my_horse.father, my_horse.mother)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name of the horse: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Neddy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How old is Neddy? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How high is Neddy ( in hands )? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's father? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bertie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's mother? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Nellie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Neddy is 12 years old, 14 hands high, and has Bertie and Nellie as parents. \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.2, page no. 436" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Exercising the horses\n", + "\"\"\"\n", + "\n", + "class Horse:\n", + " age = 0\n", + " height = 0\n", + " name = \"\"\n", + " father = \"\"\n", + " mother = \"\"\n", + " \n", + "my_horses = []\n", + "test = ''\n", + "hcount = 0\n", + "\n", + "while(True):\n", + " print \"Do you want to enter details of a horse (Y or N)? \"\n", + " choice = raw_input()\n", + " if(choice.lower() == 'n'):\n", + " break\n", + " my_horse = Horse()\n", + " print \"Enter the name of the horse: \",\n", + " my_horse.name = raw_input()\n", + " print \"How old is %s? \" % my_horse.name,\n", + " my_horse.age = int(raw_input())\n", + " print \"How high is %s ( in hands )? \" %my_horse.name,\n", + " my_horse.height = int(raw_input())\n", + " print \"Who is %s's father? \" %my_horse.name,\n", + " my_horse.father = raw_input()\n", + " print \"Who is %s's mother? \" %my_horse.name,\n", + " my_horse.mother = raw_input()\n", + " my_horses.append(my_horse)\n", + " hcount += 1\n", + "\n", + "for i in range(hcount):\n", + " print \"%s is %d years old, %d hands high, \" %(my_horses[i].name, my_horses[i].age, my_horses[i].height),\n", + " print \"and has %s and %s as parents. \" %(my_horses[i].father, my_horses[i].mother)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Do you want to enter details of a horse (Y or N)? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name of the horse: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Neddy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How old is Neddy? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How high is Neddy ( in hands )? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's father? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bertie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's mother? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Nellie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter details of a horse (Y or N)? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name of the horse: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Stallion\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How old is Stallion? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How high is Stallion ( in hands )? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Stallion's father? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Father Stallion\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Stallion's mother? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mother Stallion\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter details of a horse (Y or N)? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Neddy is 12 years old, 14 hands high, and has Bertie and Nellie as parents. \n", + "Stallion is 10 years old, 15 hands high, and has Father Stallion and Mother Stallion as parents. \n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.3, page no. 440" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Pointing out the horses\n", + "\"\"\"\n", + "\n", + "class Horse:\n", + " age = 0\n", + " height = 0\n", + " name = \"\"\n", + " father = \"\"\n", + " mother = \"\"\n", + " \n", + "my_horses = []\n", + "test = ''\n", + "hcount = 0\n", + "\n", + "while(True):\n", + " print \"Do you want to enter details of a horse (Y or N)? \"\n", + " choice = raw_input()\n", + " if(choice.lower() == 'n'):\n", + " break\n", + " my_horse = Horse()\n", + " print \"Enter the name of the horse: \",\n", + " my_horse.name = raw_input()\n", + " print \"How old is %s? \" % my_horse.name,\n", + " my_horse.age = int(raw_input())\n", + " print \"How high is %s ( in hands )? \" %my_horse.name,\n", + " my_horse.height = int(raw_input())\n", + " print \"Who is %s's father? \" %my_horse.name,\n", + " my_horse.father = raw_input()\n", + " print \"Who is %s's mother? \" %my_horse.name,\n", + " my_horse.mother = raw_input()\n", + " my_horses.append(my_horse)\n", + " hcount += 1\n", + "\n", + "ptr_my_horses = my_horses\n", + "\n", + "for i in range(hcount):\n", + " print \"%s is %d years old, %d hands high, \" %(ptr_my_horses[i].name, ptr_my_horses[i].age, ptr_my_horses[i].height),\n", + " print \"and has %s and %s as parents. \" %(ptr_my_horses[i].father, ptr_my_horses[i].mother)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Do you want to enter details of a horse (Y or N)? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the name of the horse: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Neddy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How old is Neddy? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How high is Neddy ( in hands )? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's father? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bertie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's mother? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Nellie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter details of a horse (Y or N)? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Neddy is 12 years old, 14 hands high, and has Bertie and Nellie as parents. \n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.4, page no. 445" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Daisy chaining the horses\n", + "\"\"\"\n", + "\n", + "class Horse:\n", + " age = 0\n", + " height = 0\n", + " name = \"\"\n", + " father = \"\"\n", + " mother = \"\"\n", + " next = None\n", + " prev = None\n", + "\n", + "first = None\n", + "previous = None\n", + "\n", + "while(True):\n", + " print \"Do you want to enter details of a horse (Y or N)? \",\n", + " choice = raw_input()\n", + " if(choice.lower() == 'n'):\n", + " break\n", + " current = Horse()\n", + " if(first == None):\n", + " first = current\n", + " if(previous != None):\n", + " previous.next = current\n", + " print \"Enter the name of the horse: \",\n", + " current.name = raw_input()\n", + " print \"How old is %s? \" %current.name,\n", + " current.age = int(raw_input())\n", + " print \"How high is %s ( in hands )? \" %(current.name),\n", + " current.height = int(raw_input())\n", + " print \"Who is %s's father? \" %current.name,\n", + " current.father = raw_input()\n", + " print \"Who is %s's mother? \" %current.name,\n", + " current.mother = raw_input()\n", + " current.next = None\n", + " previous = current\n", + "\n", + "current = first\n", + "while (current != None):\n", + " print \"%s is %d years old, %d hands high,\" %(current.name, current.age, current.height),\n", + " print \"and has %s and %s as parents.\" %(current.father, current.mother)\n", + " previous = current\n", + " current = current.next\n", + " previous = None\n", + " first = None" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Do you want to enter details of a horse (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the name of the horse: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Neddy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How old is Neddy? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How high is Neddy ( in hands )? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's father? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Brtie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Neddy's mother? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Nellie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter details of a horse (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Neddy is 12 years old, 14 hands high, and has Brtie and Nellie as parents.\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.5, page no. 450" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Daisy chaining the horses both ways\n", + "\"\"\"\n", + "\n", + "first = None\n", + "current = None\n", + "last = None\n", + "\n", + "class Horse:\n", + " age = 0\n", + " height = 0\n", + " name = \"\"\n", + " father = \"\"\n", + " mother = \"\"\n", + " next = None\n", + " prev = None\n", + "\n", + "\n", + "while(True):\n", + " print \"Do you want to enter details of a horse (Y or N)? \",\n", + " choice = raw_input()\n", + " if(choice.lower() == 'n'):\n", + " break \n", + "\n", + " current = Horse()\n", + " if(first == None):\n", + " first = current\n", + " last = current\n", + " else:\n", + " last.next = current\n", + " current.previous = last\n", + " last = current\n", + " print \"Enter the name of the horse: \",\n", + " current.name = raw_input()\n", + " print \"How old is %s? \" %current.name,\n", + " current.age = int(raw_input())\n", + " print \"How high is %s ( in hands )? \" %(current.name),\n", + " current.height = int(raw_input())\n", + " print \"Who is %s's father? \" %current.name,\n", + " current.father = raw_input()\n", + " print \"Who is %s's mother? \" %current.name,\n", + " current.mother = raw_input()\n", + "\n", + "current = first\n", + "while(current):\n", + " print \"%s is %d years old, %d hands h1igh,\" %(current.name, current.age, current.height),\n", + " print \"and has %s and %s as parents.\" %(current.father, current.mother)\n", + " current = current.next" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Do you want to enter details of a horse (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the name of the horse: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "neddy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How old is neddy? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "13\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How high is neddy ( in hands )? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is neddy's father? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "bertie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is neddy's mother? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "nellie\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter details of a horse (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the name of the horse: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "stallion\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How old is stallion? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " How high is stallion ( in hands )? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "18\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is stallion's father? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "father stallio\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is stallion's mother? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "mother stallion\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter details of a horse (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " neddy is 13 years old, 14 hands h1igh, and has bertie and nellie as parents.\n", + "stallion is 14 years old, 18 hands h1igh, and has father stallio and mother stallion as parents.\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.6, page no. 456" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program 11.6, page no. 456\n", + "\n", + "\"\"\"\n", + "Basics of a family tree\n", + "\"\"\"\n", + "\n", + "class Date:\n", + " day = 0\n", + " month = 0\n", + " year = 0\n", + " \n", + "class Family:\n", + " dob = Date()\n", + " name = \"\"\n", + " father = \"\"\n", + " mother = \"\"\n", + " next = None\n", + " previous = None\n", + " \n", + "def get_person():\n", + " temp = Family()\n", + " print \"Enter the name of the person: \",\n", + " temp.name = raw_input()\n", + " print \"Enter %s's date of birth (day month year) \" %temp.name,\n", + " temp.dob.day = int(raw_input(\"Day: \"))\n", + " temp.dob.month = int(raw_input(\"Month: \"))\n", + " temp.dob.year = int(raw_input(\"Year: \"))\n", + " print \"Who is %s's father? \" %temp.name\n", + " temp.father = raw_input()\n", + " print \"Who is %s's mother? \" %temp.name\n", + " temp.mother = raw_input()\n", + " temp.next = None\n", + " temp.previous = None\n", + " return temp\n", + " \n", + "def show_people(pfirst):\n", + " current = pfirst\n", + " while(current):\n", + " print \"%s was born %d/%d/%d and has %s and %s as parents. \" %(current.name, current.dob.day, current.dob.month, current.dob.year, current.father, current.mother)\n", + " current = current.next\n", + "\n", + "first = None\n", + "current = None\n", + "last = None\n", + " \n", + "while(True):\n", + " print \"Do you want to enter details of a person (Y or N)? \",\n", + " choice = raw_input()\n", + " if(choice.lower() == 'n'):\n", + " break\n", + " current = get_person()\n", + " if(first == None):\n", + " first = current\n", + " current.previous = None\n", + " last = current\n", + " else:\n", + " last.next = current\n", + " current.previous = last\n", + " last = current\n", + "show_people(first)\n", + "first = None\n", + "last = None" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter details of a person (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the name of the person: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ricky\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter Ricky's date of birth (day month year) " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 1970\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Ricky's father? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Michael\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Who is Ricky's mother? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rachel\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Do you want to enter details of a person (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the name of the person: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mitchel\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter Mitchel's date of birth (day month year) " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 1967\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Who is Mitchel's father? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Martin\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Who is Mitchel's mother? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hingies\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Do you want to enter details of a person (Y or N)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Ricky was born 5/9/1967 and has Michael and Rachel as parents. \n", + "Mitchel was born 5/9/1967 and has Martin and Hingies as parents. \n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.7, page no. 466" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program 11.7, page no. 466\n", + "\n", + "\"\"\"\n", + "binary tree to sort integers\n", + "\"\"\"\n", + "class Node:\n", + " item = 0.0\n", + " count = 0\n", + " pLeft = None\n", + " pRight = None\n", + " \n", + "def create_node(value):\n", + " pNode = Node()\n", + " pNode.item = value\n", + " pNode.count = 1\n", + " return pNode\n", + "\n", + "def add_node(value, pNode):\n", + " if(pNode is None):\n", + " return create_node(value)\n", + " if(value == pNode.item):\n", + " pNode.count += 1\n", + " return pNode\n", + " if(value < pNode.item):\n", + " if(pNode.pLeft is None):\n", + " pNode.pLeft = create_node(value);\n", + " return pNode.pLeft\n", + " else:\n", + " return add_node(value, pNode.pLeft)\n", + " else:\n", + " if(pNode.pRight is None):\n", + " pNode. pRight = create_node(value)\n", + " return pNode.pRight\n", + " else:\n", + " return add_node(value, pNode.pRight)\n", + "\n", + "\n", + "def list_nodes(pNode):\n", + " if(pNode.pLeft):\n", + " list_nodes(pNode.pLeft)\n", + " print \"%10d x %10ld\" %(pNode.count, pNode.item)\n", + " if(pNode.pRight):\n", + " list_nodes(pNode.pRight)\n", + "\n", + "\n", + "pRoot = None\n", + "answer = 'n'\n", + "while(True):\n", + " print \"Enter the node value: \",\n", + " newvalue = float(raw_input())\n", + " if(pRoot is None):\n", + " pRoot = create_node(newvalue)\n", + " else:\n", + " add_node(newvalue, pRoot)\n", + " print \"Do you want to enter another (y or n)? \",\n", + " answer = raw_input()\n", + " if answer == \"n\":\n", + " break\n", + "print \"The values in ascending sequence are: \"\n", + "list_nodes(pRoot)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the node value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "56\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another (y or n)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the node value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another (y or n)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the node value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "77\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another (y or n)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the node value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another (y or n)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the node value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "200\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another (y or n)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the node value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another (y or n)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the node value: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Do you want to enter another (y or n)? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The values in ascending sequence are: \n", + " 2 x -10\n", + " 1 x -5\n", + " 1 x 33\n", + " 1 x 56\n", + " 1 x 77\n", + " 1 x 200\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.8, page no. 470" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "The operation of a union\n", + "\"\"\"\n", + "\n", + "def print_date(date):\n", + " if date.format == 0:\n", + " print \"The date is %d/%d/%d.\" %(date.date.nDate.day, date.date.nDate.month, date.date.nDate.year)\n", + " elif date.format == 1:\n", + " print \"The date is %s. \" %date.date.date_str\n", + " elif date.format == 2:\n", + " print \"The date is %s %s %d. \" %(date.date.day_date.day, date.date.day_date.date, date.date.day_date.year)\n", + " else:\n", + " print \"Invalid date format.\"\n", + "\n", + "class Date_Format:\n", + " numeric = 0\n", + " text = 1\n", + " mixed = 2\n", + "\n", + "class MixedDate:\n", + " def __init__(self, d=\"\", dt=\"\", yr=0):\n", + " self.day = d\n", + " self.date = dt\n", + " self.year = yr\n", + "\n", + "class NumericDate:\n", + " def __init__(self, d=0, mnt=0, yr=0):\n", + " self.day = d\n", + " self.month = mnt\n", + " self.year = yr\n", + "\n", + "class UDate:\n", + " date_str = \"\"\n", + " day_date = MixedDate()\n", + " nDate = NumericDate()\n", + "\n", + "class Date:\n", + " format = Date_Format()\n", + " date = UDate()\n", + "\n", + "form = Date_Format()\n", + "yesterday = NumericDate(11, 11, 2012)\n", + "today = MixedDate(\"Monday\", \"12th November\", 2012)\n", + "tomorrow = UDate()\n", + "tomorrow.date_str = \"Tues 13th Nov 2012\"\n", + "\n", + "udate = tomorrow\n", + "the_date = Date()\n", + "the_date.date = udate\n", + "the_date.format = form.text\n", + "print_date(the_date)\n", + "\n", + "the_date.date.nDate = yesterday\n", + "the_date.format = form.numeric\n", + "print_date(the_date)\n", + " \n", + "the_date.date.day_date = today\n", + "the_date.format = form.mixed\n", + "print_date(the_date)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The date is Tues 13th Nov 2012. \n", + "The date is 11/11/2012.\n", + "The date is Monday 12th November 2012. \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 11.9, page no. 482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program 11.9, page no. 482\n", + "\n", + "\"\"\"\n", + "Generating a Bar chart\n", + "\"\"\"\n", + "\n", + "import numpy\n", + "import sys\n", + "\n", + "\n", + "PAGE_HEIGHT = 41\n", + "PAGE_WIDTH = 75\n", + " \n", + "class Bar:\n", + " value = 0\n", + " pNext = None\n", + " \n", + "def create_bar_list():\n", + " pFirst = None\n", + " pBar = None\n", + " pCurrent = None\n", + " pBar = new_bar()\n", + " while(pBar != None):\n", + " if(pCurrent):\n", + " pCurrent.pNext = pBar\n", + " pCurrent = pBar\n", + " else:\n", + " pFirst = pCurrent = pBar\n", + " #pCurrent = pBar\n", + " pBar = new_bar()\n", + " return pFirst\n", + " \n", + "def new_bar():\n", + " print \"Enter the value of the Bar, or Enter quit to end: \",\n", + " value = raw_input()\n", + " if(value == \"quit\"):\n", + " return None\n", + " pBar = Bar()\n", + " if(pBar is None):\n", + " print \"Oops! Couldn't allocate memory for a bar.\"\n", + " sys.exit()\n", + " pBar.value = float(value)\n", + " pBar.pNext = None\n", + " return pBar\n", + "\n", + "def bar_chart(pFirst, page_width, page_height, title):\n", + " pLast = pFirst;\n", + " max = pFirst.value\n", + " min = pFirst.value\n", + " vert_step = 0.0\n", + " bar_count = 1\n", + " bar_width = 0\n", + " space = 2\n", + " column = None\n", + " blank = None\n", + " position = 0.0\n", + " axis = False\n", + " while(pLast.pNext):\n", + " bar_count += 1\n", + " max = pLast.value if (max < pLast.value) else max\n", + " min = pLast.value if (min > pLast.value) else min\n", + " pLast = pLast.pNext\n", + " if(max < 0.0):\n", + " max = 0.0\n", + " if(min > 0.0):\n", + " min = 0.0\n", + " vert_step = (max - min)/page_height\n", + " bar_width = page_width/bar_count - space\n", + " if(bar_width < 1):\n", + " print \"Page width too narrow. \"\n", + " sys.exit()\n", + " column = chart_string(space, bar_width, '#')\n", + " if(not (column)):\n", + " print \"Failed to allocate memory in bar_chart() - terminating program.\"\n", + " sys.exit()\n", + " blank = chart_string(space, bar_width, ' ')\n", + " if(not (blank)):\n", + " print \"Failed to allocate memory in bar_chart() - terminating program.\"\n", + " sys.exit(1)\n", + " print \"\\n^ %s \" %title\n", + " position = max\n", + " for i in range(page_height):\n", + " if(position <= 0.0 and not(axis)):\n", + " draw_x_axis(bar_count*(bar_width + space))\n", + " axis = True\n", + " print \"|\",\n", + " pLast = pFirst\n", + " for bars in range(1, bar_count+1):\n", + " print \"%s\" %(column if((position <= pLast.value and position > 0.0) or (position >= pLast.value and position <= 0.0)) else blank),\n", + " pLast = pLast.pNext\n", + " print \"\"\n", + " position -= vert_step\n", + " if(not axis):\n", + " draw_x_axis(bar_count*(bar_width + space))\n", + " else:\n", + " print \"v\\n\",\n", + "\n", + "\n", + "def chart_string(space, bar_width, ch):\n", + " mystr = []\n", + " for i in range(space+bar_width):\n", + " mystr.append(\"\")\n", + " for i in range(space):\n", + " mystr[i] =' '\n", + " for j in range(space,space+bar_width):\n", + " mystr[j] = ch\n", + " return ''.join(mystr)\n", + "\n", + "def draw_x_axis(length):\n", + " print \"+\",\n", + " for x in range(length):\n", + " print \"-\",\n", + " print \">\"\n", + "\n", + "pFirst = None\n", + "print \"Enter the chart title: \",\n", + "title = raw_input()\n", + "pFirst = create_bar_list()\n", + "p = pFirst\n", + "while (p):\n", + " print p.value\n", + " p = p.pNext\n", + "bar_chart(pFirst, PAGE_WIDTH, PAGE_HEIGHT, title)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the chart title: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average Temperatures\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-15\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "13\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "26\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "23\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "17\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the value of the Bar, or Enter quit to end: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "quit\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " -12.0\n", + "-15.0\n", + "2.0\n", + "5.0\n", + "13.0\n", + "20.0\n", + "26.0\n", + "32.0\n", + "23.0\n", + "17.0\n", + "-1.0\n", + "-4.0\n", + "\n", + "^ Average Temperatures \n", + "| #### \n", + "| #### \n", + "| #### \n", + "| #### \n", + "| #### \n", + "| #### \n", + "| #### #### \n", + "| #### #### \n", + "| #### #### #### \n", + "| #### #### #### \n", + "| #### #### #### \n", + "| #### #### #### #### \n", + "| #### #### #### #### \n", + "| #### #### #### #### \n", + "| #### #### #### #### #### \n", + "| #### #### #### #### #### \n", + "| #### #### #### #### #### \n", + "| #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### #### \n", + "| #### #### #### #### #### #### #### #### \n", + "+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >\n", + "| #### #### #### #### \n", + "| #### #### #### \n", + "| #### #### #### \n", + "| #### #### #### \n", + "| #### #### \n", + "| #### #### \n", + "| #### #### \n", + "| #### #### \n", + "| #### #### \n", + "| #### #### \n", + "| #### #### \n", + "| #### \n", + "| #### \n", + "v\n" + ] + } + ], + "prompt_number": 2 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |