logo CodeStepByStep logo

check_dates

Language/Type: Python file input
Author: Nick Troccoli (on 2017/07/08)

Write a function named check_dates that prints information about dates in a file. Your function accepts a string parameter representing a file name. Each line of the file will contain four integers representing a pair of calendar dates in month/day format. For example the line "6 14 9 21" represents the dates 6/14 (June 14) and 9/21 (September 21). Your function should read the contents of this file and check to see which pairs of dates are at least one month apart from each other.

Each date in the file consists of a month (1 through 12) and a day (1 through the number of days in that month [28-31]).

You should read the file's data and output to the console a message indicating whether each pair of dates are at least a month apart. To be one month apart, two dates' months must differ by at least 1. If the months differ by exactly one, their days decide whether they are apart by a month or more for example, 10/24 is a month apart from 11/25, but it is not a month apart from 11/23 because the days are too close. Another way of thinking of this is that the two dates are at least one month apart if you would need to shift one of the dates by one month or more to reach the other date.

The following dates are all considered to be at least a month apart from 9/19 (September 19):

  • 1/1, 2/14, 7/25, 8/2, 8/19 (at least one month before 9/19)
  • 10/19, 10/20, 11/5, 12/31 (at least one month after 9/19)

The following dates are not at least a month apart from 9/19:

  • 8/20, 8/31, 9/1, 9/18 (before, too close)
  • 9/20, 9/28, 10/1, 10/15, 10/18 (after, too close)

Note that the first date could come before or after (or be the same as) the second date. Assume that all parameters passed are valid (i.e., the month will be between 1 and 12, and the day will be between 1 and the number of days in that month). You may assume that the file is in a valid format and that all dates occur during the same year. Assume that this is a non-leap year and do not worry about leap years for this problem.

Suppose the input file dates.txt contains the following text:

6 14 9 21
4 5 5 15
4 15 5 15
4 16 5 15
6 14 6 8 
7 7 6 8 
7 8 6 8 
10 14 7 15 
2 14 3 15 
1 1 12 31

For the input file above, the call of check_dates("dates.txt") should produce the following console output:

6/14 and 9/21 differ by one full month or more!
4/5 and 5/15 differ by one full month or more!
4/15 and 5/15 differ by one full month or more!
4/16 and 5/15 are within one month of each other.
6/14 and 6/8 are within one month of each other.
7/7 and 6/8 are within one month of each other.
7/8 and 6/8 differ by one full month or more!
10/14 and 7/15 differ by one full month or more!
2/14 and 3/15 differ by one full month or more!
1/1 and 12/31 differ by one full month or more!

If the file does not exist, prno output.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.