Programming Assignment 1 (Date Arithmetic)
- Complete the specification of the Date class as defined below.
You are not allowed to use any library packages within Python except in
implementing the today() class method.
from datetime import date import math class Date: # Constructor def __init__(self, m, d, y): self.month = m self.day = d self.year = y def leap_year(self): return ((self.year%4 == 0) and not (self.year%100 == 0 and self.year%400 != 0)) # To implement this class method, you can use Python's datetime package @classmethod def today(cls): pass # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week # Gauss - disparate variation # returns day of week as a String ("Sunday", "Monday", ...) def day_of_weekS(self): pass # returns day of week as a number (0 for "Sunday", 1 for "Monday", ...) def day_of_weekN(self): pass # Returns the Date object one day after self def tomorrow(self): pass # Returns the Date object obtained by adding ndays to self def add(self,ndays): pass # Returns True if self is after d def after(self,d): pass # Returns True if self is same as d def equals(self,d): pass # Returns True if self is before d def before(self,d): pass # Returns the number of days between self and d def days_between(self,d): pass # Please look at part 2 of assignment for string format def __str__(self): pass
The above template for Date class is available in Date.py. - Write a program, called DateArithmetic.py,
which reads data from an input file
(file name provided as command line input)
containing information about celebrities including their date of birth
and if no longer alive their date of death.
A sample input (available in text file p1.dat) is given below:
mac-mini:p1-date raj$ more p1.dat Albert:Einstein:3/14/1879:4/18/1955 Donald:Knuth:1/10/1938: Srinivasa:Ramanujan:12/22/1887:4/26/1920 Isaac:Newton:12/25/1642:3/20/1726 Tim:Berners-Lee:6/8/1955: Alonzo:Church:8/11/1995:6/14/1903 Alan:Turing:6/23/1912:6/7/1954 Leonhard:Euler:4/15/1707:9/18/1783 Jimi:Hendrix:11/27/1942:9/18/1970 Bob:Marley:2/6/1945:5/11/1981
Each line in the file contains the first name, last name, date of birth, and date of death (if alive this part will be blank) of celebrities. It is possible for error in data entry in this file in that the date of birth may be AFTER date of death. Such an error should be flagged by your program and the entry discarded from further calculations. We can assume that besides this error the input data does not contain any other error such as invalid day, month, or year in the dates.The program should process this information and produce a listing of celebrity information as shown in the sample run (sorted by NUM_DAYS in increasing order).
A sample run is shown below:
mac-mini:p1-date raj$ python3 P1.py p1.dat BAD DATA: Alonzo:Church:8/11/1995:6/14/1903 FNAME LNAME DOB DAY DOD DAY NUM_DAYS --------------------------------------------------------------------------------------------------- Jimi Hendrix 27 November, 1942 Friday 18 September, 1970 Friday 10157 Srinivasa Ramanujan 22 December, 1887 Thursday 26 April, 1920 Monday 11813 Bob Marley 6 February, 1945 Tuesday 11 May, 1981 Monday 13243 Alan Turing 23 June, 1912 Sunday 7 June, 1954 Monday 15324 Tim Berners-Lee 8 June, 1955 Wednesday None ALIVE 25052 Albert Einstein 14 March, 1879 Friday 18 April, 1955 Monday 27793 Leonhard Euler 15 April, 1707 Friday 18 September, 1783 Thursday 27915 Isaac Newton 25 December, 1642 Thursday 20 March, 1726 Wednesday 30400 Donald Knuth 10 January, 1938 Monday None ALIVE 31410 ---------------------------------------------------------------------------------------------------
The program should use the Date class developed earlier to do any Date calculations. The formatting should be exactly as shown. You will notice the output contains the date of birth and date of death (if applicable) in a different format. Also there are three new columns in the output: day of week for DOB, day of week for DOD, and number of days alive. For celebrities alive as of today, the number of days alive will be from DOB till today!
Submit source files (Date.py and DateArithmetic.py).