Homework 3
This is the third homework assignment. Your goal in this assignment is
to write a function that performs a more complex calculation and returns
a value. You will need to use some conditional branching (if
statements)
and probably also a loop to do counting.
For this homework, like the last one, we provide you with a testing
framework, which will run tests of your function. It is important that
you learn to use the testing program effectively, since we will be using
this kind of automated testing for the following homeworks and for the
programming exam at the end of the course.
Practical information#
The assignment is due on Sunday the 18th of March, at 6pm. To submit your solution, you will upload a single python file via wattle. Here is the assignment submission link.
In addition to submitting your solution, you must attend the following lab (in week 5). In the lab, your tutor will ask you some questions about your solution, and give you feedback if there is anything you need to improve. This discussion with the tutor is also part of the assessment. If you do not show up for the discussion with the tutor, you will not receive any marks for this assignment. If you do not submit a solution, you may still get some marks following the discussion with the tutor.
The assignment can be done together in pairs, but not in a group of more than two students. You may, of course, also do the assignment on your own if you prefer.
If you work in a pair, both students must submit solution files, and both students must attend the following lab and answer the tutors questions. In your solution file, write a comment (using python comment syntax) to say who you worked together with - you should write their ANU id. Both of you must be able to explain every part of your submitted solution. The tutor will choose who to address each question to, and only the student addressed may answer. It is not acceptable to divide the assignment up so that one student does half and the other student the other half.
As usual, you should have followed last week’s lectures and worked through the exercises in lab 3 before starting on the assignment. The assignment should not take more than one or two hours to complete.
The problem#
A standard year in the Gregorian calendar is 365 days. To compensate for the fact that the Earth’s orbit is slightly longer, the calendar inserts one extra day, called the leap day, into some years. Such years, which are called leap years, have 366 days.
You may think that every fourth year is a leap year, but that is not exactly true. Adding a leap day every fourth year is slightly too much. Therefore, the Gregorian calendar removes 3 leap days every 400 years. The exact rule for which years are leap years is:
A year X is a leap year iff X is a multiple of 4 but not a multiple of 100, or if X is a multiple of 400.
Your task is to write a function that computes the total number of days in a range of years. The function definition should be like this:
def total_days(start_year, end_year):
# ...your code here...
The function should return the number of days from the 1st of January
of start_year
to (but not including) the 1st of January end_year
.
In other words, it should return the sum of the number of days in each
year from start_year
to, but not including, end_year
.
Examples:
-
If
start_year
is 2016 andend_year
is 2017, the function should return 366. This is the number of days in 2016, since 2016 was a leap year. -
If
start_year
is 2000 andend_year
is 2004, the function should return 1461. This is the sum of the number of days in 2000 (which is a leap year), 2001, 2002 and 2003 (which are not leap years). -
If
start_year
is -3 andend_year
is 0, the function should return 1095. This is the sum of the number of days in years -3, -2 and -1 (none which are leap years).
Assumptions and restrictions:
-
You can assume that the arguments are integers, and that
start_year
is less thanend_year
. -
Years can be negative (year zero is just an arbitrary reference point).
-
Your function must return an integer (a value of type
int
).
As a starting point, we provide you with a skeleton code file: homework3.py. Download this file and write in it your implementation of the function.
Using the testing program#
To use the testing program, you must first download these two files:
Save both of them in the same directory as the file homework3.py
.
To run the testing program, you just need to run test_hw3.py
.
The testing program will read the file homework3.py
and test the
function total_days
defined in that file, and print out results
of the tests. If your function fails any of the tests, the program
will print a detailed error message and stop.
Remember that the testing program will test the file named homework3.py
which is located in the same directory. If you change the name of the
file with your implementation, you must also change the file name on the
last line in the testing program to test the right file.
For the testing program to work, your code file must meet certain requirements. These requirements also apply to the file that you submit.
- It must be syntatically correct python code.
- It must contain only function definitions and comments.
- It must define a function called
total_days
that takes two arguments.
Your function definitions may contain docstrings (as shown in the lectures), but it is not permitted to use strings as comments anywhere other than on the first line inside a function suite.
If your submitted program does not satisfy these requirements, you will not receive any marks.
Marking#
Upload the file homework3.py
with your implementation.
As mentioned above, you must also attend the following lab (in week 5) and answer your tutor’s questions about your solution. This discussion is part of the assessment. You should be prepared to answer or demonstrate to the following questions:
- Can you download the file that you submitted from wattle?
- Can you run that file in the python interpreter (using an IDE of your choice) on the CSIT lab computer?
- If the file has syntax errors, can you use the error messages from the interpreter or IDE to identify where the syntax errors are?
- Does your submitted file meet the requirements stated above? Does
it contain anything that is not a function definition, an
import
statement, or a comment? If so, can you point it out? - Can you download and run the testing program?
- Does your implementation pass all the tests run by the testing program?
- What is the difference between the
print
function and thereturn
statement? - Does your function always return a value of the correct type?
- You may have found some python modules provided functionality that was helpful to solve the problem, but it is also possible to solve without using any extra module. Can you describe how you would implement a solution that does not rely on any imported module?
- Did you implement your solution with just one function, or divide it into several functions? Does the functional abstraction make your code easier to read and understand?
- Are the functions in your submitted file documented? (with docstrings and/or comments). Does the function documentation adequately describe what the function does and what its assumptions and limitations are? Are the names of variables, parameters and auxiliary functions descriptive of their purpose?
The marking scale for this assignment is as follows:
- The submitted file is syntactically correct and meets the submission requirements: 0.5 marks.
- The implementation of the
total_days
function computes and return correct values for all valid arguments: 1 mark. - The submitted code has the right level of documentation and commenting: 1 mark.
- Your ability to use the tools (including the testing program) and your understanding of possible solutions to the problem, as demonstrated in your discussion with the tutor: up to 2.5 marks.
The assignment is worth 5% of your final mark.