This is the fourth homework assignment. Your goal in this assignment is to write a function that performs a calculation on sequence data and returns a value.
Your solution to this homework will also be marked on code quality. This means some part of the marks will be given for good code organisation, variable/function naming, and commenting. The marks for code quality are distinct from those for functionality; to gain full marks, your submission must be both functional and readable.
Practical information#
The assignment is due Monday the 18th of April at 9.00am (Canberra time). This is the Monday in semester week 7, that is, the first week after the teaching break. 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 7). 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 fail to show up for the discussion with the tutor, you will receive zero marks for this assignment. If you do not submit a solution, you may still get partial marks if you are able to show the tutor that you have made some attempt to solve the homework.
The homework is individual. You must write your own solution, and you are expected to be able to explain every aspect of it.
As usual, you should have followed last week’s lectures and worked through the exercises in lab 4 and lab 5 before starting on the assignment. The assignment should not take more than one or two hours to complete.
The problem#
The graph below shows the evolution of the price of one bitcoin over 30 days:
As you would expect, the price fluctuates over time. To make money in a speculative market, one should of course buy when the price is low, and sell when it is high. With the benefit of hindsight (or perfect foresight), i.e., knowing the price evolution over an interval of time, we can calculate what is the biggest profit that could be made (proportionally to the amount invested) by finding the two points with the biggest increase. However, these can not be any two points: the low point must come before the high point. For example, in the graph above, the highest price ($8235.70) occurred on the 28th of July, and the lowest point ($6139.99) occurred on the 10th of August, so it would not be possible to buy at the lowest price and sell at the highest. The maximum increase that we can find, in this example, is from $6139.99 on August 10th to $6729.44 on August 25th, an increase of $589.45.
Of course, we can do the same calculation on a series that does not represent money. For example, the enrolments in COMP1730/6730 from 2013 to the current year are
226, 264, 357, 364, 485, 529, 483, 489, 563
Here, the biggest increase is from the first element to the last, a total of 337.
Your task in this homework is to write a function max_increase(seq)
which takes as argument a sequence of numbers and returns the maximum
increase from one element in the sequence to an element at a higher index.
Assumptions and restrictions:
- The function must return a number.
- If there is no increasing pair in the sequence, the function should return 0. This may happen for example if the sequence is decreasing, or if it contains fewer than 2 elements.
- You can assume that the argument is a sequence, and that its elements are numbers (integer or decimal), but other than that you should not make any assumptions. In particular, you should not assume that it is any particular type of sequence (list, array, etc) and use only operations that are applicable to all sequence types.
As a starting point, we provide you with a skeleton code file: max_increase.py. Download this file and write in it your implementation of the function.
Testing#
The skeleton file has a testing function: test_max_increase()
. It runs
some tests on max_increase
function, and will raise an error if
any of the tests fail. If all tests pass, the testing function prints
the message “all tests passed” at the end.
Remember that testing only checks a small number of predefined cases; it can never prove that your function works correctly for all valid arguments. You should examine the test cases that are provided, and think about whether there are any important ones that are missing.
Note that you can define additional functions, if you think it helps you decompose the problem or write a better solution. Your function definitions should contain docstrings, but you may not use strings as comments anywhere other than on the first line inside a function, or at the beginning of the file.
Marking#
Code quality
In this homework (like in the last one) we will also be marking your submission for its code quality. This includes aspects such as:
- Using good function, parameter and variable names. The names of some functions in the homework are fixed, but if you define additional functions (to decompose the problem) then they should be given descriptive names.
-
Appropriate use of comments and docstrings.
This means not too little comments but also not too much. Comments should be accurate, relevant, and readable. A docstring should appear as the first statement in every function definition.
-
Good code organisation.
This includes appropriate use of functions to decompose a problem and avoid code repetition. Also, do not import modules that you do not use.
What to submit
You should edit the skeleton file max_increase.py
, then upload only
this file with your implementation of the function using the
assignment submission link on wattle.
Remember that you must upload a single python code file. Do NOT zip it or convert it to another format.
The file that you submit must meet the following requirements:
- It must be syntatically correct python code.
- Like the file you downloaded, it should contain only function definitions, and, optionally, import statements. However, it is not necessary to use any module to solve the problem, and you should only import modules that you actually use. Comments, including docstrings (if they are used appropriately) are of course ok to include. Anything that is not a function definition or import statement will be ignored when we test your submission. Importantly, this means you must not use any global variables.
As mentioned above, you must also attend the following lab 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)
- 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? If so, can you point it out?
- Does your implementation pass all the tests run by the unmodified testing function?
- Is your implementation of the function correct for any valid argument sequence?
- Do your functions always return a value of the correct type?
- Did you think of any other test cases that should be used to test your function, in addition to or in place of those provided?
- What is the difference between the
print
function and thereturn
statement?
In marking this assignment we will consider the following:
- Does your submitted file satisfy the requirements specified above?
- Does your implementation compute the correct value for all valid arguments?
- The quality of your submitted python code, including its organisation, naming and documentation (with docstrings and comments).
- Your ability to use the tools (e.g., the IDE or python interpreter), your understanding of python’s error messages, and your understanding of the solution, as demonstrated in your discussion with the tutor.
The assignment is worth 4% of your final mark. 2 marks are based on the functionality of your submission, and 2 mark on the quality and readability of your code.