Homework 2 (S1 2019)

This is the second homework assignment. Your goal in this assignment is to write a simple function that performs a calculation and returns a value. For this homework, 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 programming problems in exams.

Practical information#

The assignment is due on Sunday the 17th of March, at 11:55pm. 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 4). 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 partial marks following the discussion with the tutor.

The homework is individual. You must write your own solution, and you are expected to be able to explain every aspect of it.

If you have followed the lectures and worked through the exercises in lab 2, the assignment should not take more than one or two hours to complete.

The problem#

In mathematics, the square pyramidal number sequence is the number sequence 0, 1, 5, 14, 30, 55, … .

The n:th number in the sequence is defined as the sum of the square of each number from 0 to n, inclusive, that is, 02 + 12 + … + n2.

Note that the sequence starts from zero: The zeroth sum of squares is zero, and the first sum of squares is 1.

There is also closed-form formula for the n:th number in the sequence: S(n) = n (n + 1)(2n + 1) / 6.

Your task is to write a function sum_of_squares(n) that computes and returns the n:th number in the sequence for any non-negative integer value n. The function must take one argument (that is, have one parameter), which is the value of n, and it must return an integer value (a value of type int).

As a starting point, we provide you with a skeleton code file: sum_of_squares.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 the file:

Save it in the same directory as the file sum_of_squares.py. To run the testing program, you just need to run homework_two_tests.py. It will read the file with your implementation (sum_of_squares.py), test the function sum_of_squares(n) defined in that file, and print out results of the tests. If your function fails any of the tests, the program will print an error message and stop.

Marking#

What to submit

You should edit the skeleton file sum_of_squares.py that you downloaded, then upload only this file with your corrections using the assignment submission link on wattle. Do not edit (and do not try to upload) the testing program.

The file that you submit must meet the following requirements:

  • It must be called sum_of_squares.py
  • It must be syntatically correct python code.
  • Like the file you downloaded, it must contain only function definitions and comments (module and function docstrings are accepted). If you are not sure what is accepted, ask your tutor during the lab.
  • It must define a function called sum_of_squares that takes one argument.

As mentioned above, you must also attend the following lab (in week 4) 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 function pass all the tests run by the testing program?
  • Does your function correctly compute and return the square pyramidal number S(n) for every non-negative integer n?
  • Does your function always return a value of the correct type?
  • How did you choose to calculate the number? Which formula or method did you use, and why?
  • What is the difference between the print function and the return statement?

In marking this assignment we will consider the following:

  • Does your submitted file satisfy the requirements specified above? (Submissions that do not meet the requirements can not receive more than half the marks and may not be marked at all.)
  • Does your implementation of the sum_of_squares function compute the correct value for all argument values?
  • 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 3% of your final mark.

bars search times arrow-up