Homework 3 (S1 2019)
This is the third homework assignment. Your goal in this assignment is
to write functions that perform a (slightly) more complex calculation.
You will need to use some conditional branching (if statements) and
looping.
For this homework, like the last one, we provide you with a testing
framework, which will run tests of your functions. 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
exams.
Your solution to this homework will also be marked on code quality. This means some part of the marks will be given for good functional decomposition, 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 well structured and readable.
Practical information#
The assignment is due on Sunday the 24th 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 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 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 3 before starting on the assignment. The assignment should not take more than one or two hours to complete.
The problem#
In mathematics, b is a factor (also called a divisor) of a if and only if there exists an integer m such that: b * m = a.
For example:
- 4 is a factor of 12, because 4 * 3 = 12
- 11 is a factor of 99 because 11 * 9 = 99
- 5 is not a factor of 12 because there is no integer m such that 5 * m = 12
Every integer is a factor of itself, and 1 is a factor of every integer.
One definition of a prime number p, is a number that has exactly 2 distinct factors i.e. 1 and p. (Note by this definition, 1 is not a prime number since it only has one distinct factor.)
- 2, 3, 17 and 97 are all prime numbers.
- 1, 4 (also has 2 as a factor), 44 (also has 2, 4, 11 and 22 as factors) and 119 (also has 7 and 17 as factors) are not prime numbers.
An integer n is the square of a prime if there exists an integer p such that p is a prime number and p ** 2 = n.
- 25 is the square of a prime (5 ** 2).
- 289 is the square of a prime (17 ** 2).
- 90 is not the square of a prime (the square root of 90 is not an integer).
- 100 is not the square of a prime (10 ** 2 = 100, but 10 is not a prime number).
Your task is to write three functions, is_factor(a, b),
is_prime(n) and is_prime_squared(n). Each of your functions should take
one or more positive integers as arguments and return either True or False.
is_factor(a, b)should returnTrueif b is a factor of a andFalseotherwise.is_prime(n)should returnTrueif n is a prime number andFalseotherwise.is_prime_squared(n)should returnTrueif n is the square of a prime andFalseotherwise.
You can (and should) make use of the earlier functions when implementing the later functions.
Assumptions and restrictions:
- You can assume that the argument(s) to each of the functions are positive integers (i.e. they are greater than 0).
- For
is_primeandis_prime_squaredyou can assume the arguments are greater than 1 (so you don’t have to write n = 1 as a special case). - Each of your functions must always return a boolean
(a value of type
bool).
As a starting point, we provide you with a skeleton code file: is_prime.py. Download this file and write in it your implementation of the three functions.
Hint:
The modulo (%) operator is useful for
solving this problem. For example, number % 5 will give you the
remainder when number is divided by 5.
Using the testing program#
To use the testing program, you must first download the file:
Save it in the same directory as the file is_prime.py.
To run the testing program, you just need to run homework_three_tests.py.
The testing program will read the file is_prime.py and test the
functions is_factor, is_prime and is_prime_squared
defined in that file, and print out results of the tests.
If any of your functions 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 is_prime.py
which is located in the same directory. If you change the name of the
file with your implementation, the tests won’t work.
Marking#
What to submit
You should edit the skeleton file is_prime.py, then upload only
this file with your implementations of the three functions 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 syntatically correct python code.
- It must contain only function definitions and comments (module and function docstrings are accepted).
- It must only import the
mathmodule. - Your function definitions may contain docstrings (as shown in the week 2 lecture), but it is not permitted to use strings as comments anywhere other than on the first line inside a function suite.
As mentioned above, you must also attend the 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
importstatement, 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
printfunction and thereturnstatement? - Does your function always return a value of the correct type?
- Did you use the earlier functions to help solve the later 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 adequatly 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?
In marking this assignment we will consider the following:
- Does your submitted file satisfy the requirements specified above?
- Submissions that do not meet the syntactic requirements will not be marked.
- Does your implementation of the three functions
is_factor,is_primeandis_prime_squaredfunction compute the correct value for all argument values? - 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 5% of your final mark. 3 marks are based on the functionality of your submission (1 mark per function); 2 of the 5 are based on the readability and quality of your code.