{ "metadata": { "name": "Chapter IX" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9: Working with structures" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 9.1, Page number: 167" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", "\n", " #Class Declaration\n", " class date:\n", " 'python classes are equivalent to C structures'\n", " def __init__(self): #Class Constructor\n", " #Set default values\n", " self.month=0\n", " self.day=0\n", " self.year=0\n", "\n", "\n", " #Creating instance\n", " today=date() \n", " \n", " #Modifying values\n", " today.month=9 \n", " today.day=25\n", " today.year=2004\n", "\n", " #Result\n", " print(\"Today's date is {0}/{1}/{2}\".format(today.month,today.day,today.year%100));\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Today's date is 9/25/4\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 9.2, Page number: 169" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", " #Class Declaration\n", " class date:\n", " def __init__(self): #Class Constructor\n", " #Default values\n", " month=0\n", " day=0\n", " year=0\n", " #creating instances\n", " today=date()\n", " tomorrow=date()\n", "\n", " #List Declaration\n", " daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]\n", " \n", " print(\"Enter today's date (mm/dd/yyyy):\")\n", " today.month,today.day,today.year=map(int,\"12/17/2004\".split('/'))\n", " #today.month,today.day,today.year=map(int,raw_input().split('/'))\n", "\n", " #Calculations\n", " if( today.day != daysPerMonth[today.month - 1] ):\n", " tomorrow.day = today.day + 1;\n", " tomorrow.month = today.month;\n", " tomorrow.year = today.year;\n", " elif( today.month == 12 ):\n", " tomorrow.day = 1;\n", " tomorrow.month = 1;\n", " tomorrow.year = today.year + 1;\n", " else:\n", " tomorrow.day = 1;\n", " tomorrow.month = today.month + 1;\n", " tomorrow.year = today.year;\n", "\n", " #Result\n", " print(\"Tomorrow's date is {0}/{1}/{2}\\n\".format(tomorrow.month,tomorrow.day,tomorrow.year));\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter today's date (mm/dd/yyyy):\n", "Tomorrow's date is 12/18/2004\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 9.3, Page number: 171" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class date:\n", " def __init__(self): #Class Constructor\n", " #Default values\n", " month=0\n", " day=0\n", " year=0\n", " \n", "\n", "def main():\n", " #creating instances\n", " today=date()\n", " tomorrow=date()\n", "\n", " print(\"Enter today's date (mm/dd/yyyy):\")\n", " today.month,today.day,today.year=map(int,\"2/28/2004\".split('/'))\n", " #today.month,today.day,today.year=map(int,raw_input().split('/'))\n", "\n", " #Calculations\n", " if( today.day != numberOfDays(today) ):\n", " tomorrow.day = today.day + 1;\n", " tomorrow.month = today.month;\n", " tomorrow.year = today.year;\n", " elif( today.month == 12 ):\n", " tomorrow.day = 1;\n", " tomorrow.month = 1;\n", " tomorrow.year = today.year + 1;\n", " else:\n", " tomorrow.day = 1;\n", " tomorrow.month = today.month + 1;\n", " tomorrow.year = today.year;\n", "\n", " #Result\n", " print(\"Tomorrow's date is {0}/{1}/{2}\\n\".format(tomorrow.month,tomorrow.day,tomorrow.year));\n", "\n", "\n", "def numberOfDays(d):\n", " \n", " #List Declaration\n", " daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]\n", " if(isLeapYear(d)==True and d.month==2):\n", " days=29\n", " else:\n", " days = daysPerMonth[d.month - 1];\n", "\n", " return days\n", "\n", "\n", "def isLeapYear(d):\n", " if ( (d.year % 4 == 0 and d.year % 100 != 0) or d.year % 400 == 0 ):\n", " leapYearFlag = True # Its a leap year\n", " else:\n", " leapYearFlag = False # Not a leap year\n", " \n", " return leapYearFlag;\n", "\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter today's date (mm/dd/yyyy):\n", "Tomorrow's date is 2/29/2004\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 9.4, Page number: 174" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class date:\n", " def __init__(self): #Class Constructor\n", " #Default values\n", " month=0\n", " day=0\n", " year=0\n", " \n", " def dateUpdate(self,today):\n", " #Calculations \n", " tomorrow=date()\n", " if( today.day != numberOfDays(today) ):\n", " tomorrow.day = today.day + 1;\n", " tomorrow.month = today.month;\n", " tomorrow.year = today.year;\n", " elif( today.month == 12 ):\n", " tomorrow.day = 1;\n", " tomorrow.month = 1;\n", " tomorrow.year = today.year + 1;\n", " else:\n", " tomorrow.day = 1;\n", " tomorrow.month = today.month + 1;\n", " tomorrow.year = today.year;\n", " \n", " return tomorrow\n", "\n", "\n", "\n", "def main():\n", " #creating instances\n", " thisDay=date()\n", " nextDay=date()\n", " \n", " print(\"Enter today's date (mm/dd/yyyy):\")\n", " thisDay.month,thisDay.day,thisDay.year=map(int,\"2/22/2004\".split('/'))\n", " #thisDay.month,thisDay.day,thisDay.year=map(int,raw_input().split('/'))\n", " nextDay=thisDay.dateUpdate(thisDay)\n", "\n", " #Result\n", " print(\"Tomorrow's date is {0}/{1}/{2}\\n\".format(nextDay.month,nextDay.day,nextDay.year));\n", "\n", "\n", "\n", "\n", "def numberOfDays(d):\n", " \n", " #List Declaration\n", " daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]\n", " if(isLeapYear(d)==True and d.month==2):\n", " days=29\n", " else:\n", " days = daysPerMonth[d.month - 1];\n", "\n", " return days\n", "\n", "\n", "\n", "def isLeapYear(d):\n", " if ( (d.year % 4 == 0 and d.year % 100 != 0) or d.year % 400 == 0 ):\n", " leapYearFlag = True # Its a leap year\n", " else:\n", " leapYearFlag = False # Not a leap year\n", " \n", " return leapYearFlag;\n", "\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter today's date (mm/dd/yyyy):\n", "Tomorrow's date is 2/23/2004\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 9.5, Page number: 178" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class time:\n", " def __init__(self):\n", " hour=0\n", " minutes=0\n", " seconds=0\n", " def timeUpdate(self,now):\n", " #Calculation\n", " now.seconds+=1\n", " if ( now.seconds == 60): #next minute\n", " now.seconds = 0\n", " now.minutes+=1 \n", " if ( now.minutes == 60 ): #next hour\n", " now.minutes = 0\n", " now.hour+=1;\n", " if ( now.hour == 24 ): # midnight\n", " now.hour = 0\n", " \n", "\n", " return now\n", "\n", "def main():\n", " #Creating instances\n", " currentTime=time()\n", " nextTime=time()\n", " #User Input\n", " print(\"Enter the time (hh:mm:ss):\")\n", " currentTime.hour,currentTime.minutes,currentTime.seconds=map(int,\"16:14:59\".split(':'))\n", " #currentTime.hour,currentTime.minutes,currentTime.seconds=map(int,raw_input().split(':'))\n", " \n", " nextTime =currentTime.timeUpdate (currentTime);\n", " \n", " #Result\n", " print(\"Updated time is {0}:{1}:{2}\\n\".format(nextTime.hour,nextTime.minutes,nextTime.seconds))\n", "\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the time (hh:mm:ss):\n", "Updated time is 16:15:0\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 9.6, Page number: 183" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "class time:\n", " def __init__(self,h,m,s):\n", " #Variable Declarations\n", " self.hour=h\n", " self.minutes=m\n", " self.seconds=s\n", " def timeUpdate(self,now):\n", " #Calculation\n", " now.seconds+=1\n", " if ( now.seconds == 60): #next minute\n", " now.seconds = 0\n", " now.minutes+=1 \n", " if ( now.minutes == 60 ): #next hour\n", " now.minutes = 0\n", " now.hour+=1;\n", " if ( now.hour == 24 ): # midnight\n", " now.hour = 0\n", " \n", "\n", " return now\n", "\n", "def main():\n", " #Creating instances\n", " testTimes = []\n", " testTimes.append(time(11,59,59))\n", " testTimes.append(time(12,0,0))\n", " testTimes.append(time(1,29,59))\n", " testTimes.append(time(23,59,59))\n", " testTimes.append(time(19,12,27))\n", "\n", " #Result\n", " for i in range(0,5):\n", " \n", " sys.stdout.write(\"Time is {0:2}:{1:2}:{2:2} \".format(testTimes[i].hour,testTimes[i].minutes,testTimes[i].seconds))\n", "\n", " nextTime =testTimes[i].timeUpdate(testTimes[i])\n", " \n", " \n", " sys.stdout.write(\"...one second later it's {0:2}:{1:2}:{2:2}\\n\".format(testTimes[i].hour,testTimes[i].minutes,testTimes[i].seconds))\n", "\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Time is 11:59:59 ...one second later it's 12: 0: 0\n", "Time is 12: 0: 0 ...one second later it's 12: 0: 1\n", "Time is 1:29:59 ...one second later it's 1:30: 0\n", "Time is 23:59:59 ...one second later it's 0: 0: 0\n", "Time is 19:12:27 ...one second later it's 19:12:28\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 9.7, Page number: 188" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def main():\n", " #Class Declaration\n", " class month:\n", " def __init__(self,n,na): #Class Constructor\n", " self.numberOfDays=n\n", " self.name=na\n", "\n", " #List Declaration\n", " months=[]\n", " months.append(month(31,['j','a','n']))\n", " months.append(month(28,['f','e','b']))\n", " months.append(month(31,['m','a','r']))\n", " months.append(month(30,['a','p','r']))\n", " months.append(month(31,['m','a','y']))\n", " months.append(month(30,['j','u','n']))\n", " months.append(month(31,['j','u','l']))\n", " months.append(month(31,['a','u','g']))\n", " months.append(month(30,['s','e','p']))\n", " months.append(month(31,['o','c','t']))\n", " months.append(month(30,['n','o','v']))\n", " months.append(month(31,['d','e','c']))\n", " \n", " \n", " #Result\n", " print(\"Month NumberOfDays\")\n", " print(\"----- ------------\")\n", " for i in range (0,12):\n", " print(\"{0}{1}{2} {3}\\n\".format(months[i].name[0],months[i].name[1],months[i].name[2], months[i].numberOfDays))\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Month NumberOfDays\n", "----- ------------\n", "jan 31\n", "\n", "feb 28\n", "\n", "mar 31\n", "\n", "apr 30\n", "\n", "may 31\n", "\n", "jun 30\n", "\n", "jul 31\n", "\n", "aug 31\n", "\n", "sep 30\n", "\n", "oct 31\n", "\n", "nov 30\n", "\n", "dec 31\n", "\n" ] } ], "prompt_number": 3 } ], "metadata": {} } ] }