Use the command line to create a new directory called lab14 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.
One of the benefits of functions is that they can be tested independently from the rest of the program. The caveat to this is you need to know exactly how a function is supposed to behave before you even write the program. Creating the test cases before hand will allow you to make sure your function works as soon as you get done. For practice, you will write your test cases for the final lab activity today first.
Details
In a file called date_validation.py, write a set of test
cases to ensure your is_leap_year(year)
function
operates under the following specification.
A leap year occurs almost every 4 years. All leap years are
divisible by 4, but not all years divisible by 4 are leap years.
Years that are divisible by 100, but not divisible by 400 are not
leap years. Your function should return True
if the
year parameter is a leap year, and False
otherwise.
Once your test cases are checked off, you may then proceed to write
your is_leap_year
function. Make sure you get
your test cases checked before you proceed.
Make sure your code follows the course's code conventions.
Sample Test Cases
Function Parameters | Expected Output |
---|---|
2004 | True |
2014 | False |
Hint
-
A very easy way to generate test cases is through boundary testing. If there are conditions that must be satisfied, you want to test values around these conditions. For example, you know that a year has to be divisible by 4 to even have the possibility of being a leap year. Thus, you will want to check a value that is divisible by 4, and a value that is not. You need to do that for all of the possible conditions from the above specification.
-
You can use the
%
(mod) operator to test divisibility. Ifyear % 4
is 0, then year is divisible by 4. -
Dont forget that you can use your logical constructs to reduce the complexity of the condition checks.
Challenge
Another approach to testing is exhaustive enumeration. There have been 2014 years since the beginning of the common era. Write code that checks all of the years since 0 A.D. Compute the total number of leap years in that time frame.
The reason we have leap years added to our calendar is because they do not take into account the fact that the Earth actually takes (about) 365.25 days to revolve once around the sun. While it sounds like this should be an easy thing to account for, there's actually some fairly complex math involved in dealing with this inconsistency. This causes havoc when trying to validate a date, but we can simplify things by adding functions and using our conditionals wisely.
Details
In your file called date_validation.py, write an additional
function days_in_month(month, year)
, which takes a
integer parameter in the range \([0, 12)\) which specifies the
month, and an integer that specifies the year. Your function should
return the number of days in the month. Recall:
30 days hath September, April, June, and November
All the rest have 31
Except for February, the only one.
Test this function before you go on. You may want to have your test cases checked before you proceed.
Once you have tested your days_in_month
function, you
can write the validate_date(month, day, year)
function.
This function takes 3 integers: The first specifies the month, the
second specifies the day, and the final specifies the year. This
function should return True if the date specified is valid, and
False otherwise.
Make sure you test this function with various parameters before you have it checked. Make sure you also follow all of the course's style guidelines.
Sample Test Cases
Function Parameters | Expected Output |
---|---|
2, 29, 2004 | True |
2, 29, 2014 | False |
Hint
-
You need to make sure that you don't send any parameters to
days_in_month
function that are outside of the ranges. If you operate under this assumption, you can write this function using only 4 conditionals: one for months with 31 days, one for months with 30 days, one for February non-leap year (28), and one for February leap years (29). You will need to use youris_leap_year
function in this function. -
Validate date needs to check to make sure the month is a valid integer for a month and the day specified is within the ranges for the month they specified. If both of these conditions are satisfied, then the date is valid.
-
Dont forget that you can use your logical constructs to reduce the complexity of the condition checks.
Challenge
Write an additional function
called validate_date_time(month, day, year, hours,
minutes)
. This function should validate the date AND the
time specified. Assume the time is specified as two integers,
which should be in the ranges \([0, 12]\) and \([0, 60]\).
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.