Start Your Journey with Linux Command Line
| Day of the year: writing and using your own functions |
LAB 4.3.1.6
LAB 4.3.1.7
Familiarize the student with:
Your task is to write and test a function which takes three arguments (a year, a month, and a day of the month) and returns the corresponding day of the year, or returns None if any of the arguments is invalid.
Use the previously written and tested functions. Add some test cases to the code. This test is only a beginning.
Stoooooooop .. we are here to real the REAL PYTHON, not this SILLY COURSE
Dear learner .. if you reached here, that means you'd have had enough of this boring course already😏. Let's learn python the right way, not the old school way.
=================================================================================
This lab focuses on projecting and writing parameterized functions and combining them into a small set of utilities that work together. It builds directly on two earlier labs: is_leap_year(year) from LAB 4.3.1.6 and days_in_month(year, month) from LAB 4.3.1.7. Each function takes arguments through its parameters, performs a focused job, and hands a result back to the caller through the return statement.
The heart of the exercise is day_of_year(year, month, day), which returns the corresponding day of the year, or None if any argument is invalid. The function works by accumulating the total number of days from every complete month before the requested one. It loops over range(1, month), and for each month m it calls days_in_month(year, m) to discover how many days that month has, adding them to a running total.
The correctness of this code depends on careful validation at every level. First, days_in_month returns None if the year is below one or the month falls outside 1..12. Because February can have either 28 or 29 days, it consults the leap-year function through the test month == 2 and is_leap_year(year) before returning a value from the days list. Second, day_of_year checks that the requested day is between 1 and the number of days in that actual month; if the day is out of range, both the month total and the day are rejected and None is returned. In the verification output, 2021-11-30 maps to day 30 of November, and 2000-02-29 produces day 60 because February 2000 is a leap year.
return statement to send computed values back to the calling code.None to signal invalid input such as a bad year, month, or day.days_in_month(year, month) adjusts February to 29 only when is_leap_year returns True.The list stores days per month by index, so days_in_month uses month - 1 to convert a 1-based month number into the correct 0-based position in the list.
None accomplish?It signals that the inputs are invalid, allowing the caller to distinguish a real day-of-year value from a failed lookup.
Because February has 29 days in leap years and 28 otherwise, so its result depends on the outcome of is_leap_year(year).
After summing all previous months, the function validates the requested day and, if valid, returns the accumulated total plus that day.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.