Skip navigation
PROGRAMMING FOR SCIENTISTS S2 2019
ANU College of Engineering & Computer Science
COMP1730/6730
Course content & schedule
Labs
Assessment
Archive
1.Home COMP1730/6730: Assessment Homework 3 (S2 2019)
Homework 3 (S2 2019)
Homework 3 (S2 2019)
This is the third homework assignment. Your goal in this assignment is to write functions that perform a (slightly) more complex calculation and return a value. You will need to use some conditional branching (if statements) and looping.
Practical information
The assignment is due on Monday the 26th of August, at 11.55pm. To submit your solution, 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 6). 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 for 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 weeks 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 is a number that has exactly 2 distinct factors: 1 and the number itself. (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.
Your task is to write two functions: count_factors(n) and count_prime_factors(n). Both functions take a single argument, which is a positive integer.
count_factors(n) should return the number of distinct factors in n (counting also 1 and the number itself).
count_prime_factors(n) should return the number of factors in n that are also prime numbers (again, including the number itself). This means that if n is a prime number, count_prime_factors(n) must always return 1, because n has only two factors (1 and n) and only one of those (n) is prime.
We provide you with a skeleton code file: factors.py. Download this file and write your implementation in it.
In the skeleton file you will also find a function called is_prime(n), which tests if a number is prime. The test implemented in this function is the one described above, meaning that it uses count_factors to check if the number of distinct factors is exactly 2. Therefore, is_prime will not work until you have correctly written count_factors.
Hint:
The remainder (%) operator is useful for solving this problem. For example, the expression number % 5 evaluates to the remainder when number is divided by 5.
Assumptions and restrictions:
You can assume that the argument to each of the functions is a positive integer (i.e. greater than 0).
The two counting functions must return an integer value.
Testing
The skeleton file has several testing functions: test_count_factors(), test_is_prime() and test_count_prime_factors(). Each of them runs some tests on the corresponding function, and will raise an error if any of the tests fail. If all tests pass, each 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.
Marking
Code quality
In this homework (and in the following two) 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. Docstrings should be used only as the first statement in a function definition or in a file.
Good code organisation.This includes appropriate use of functions to decompose a problem and avoid code repetition.
What to submit
You should edit the skeleton file factors.py that you downloaded, then upload only this file with your solution using the assignment submission link on wattle.
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; comments, including docstrings (if they are used appropriately) are of course ok to include. Anything that is not a function definition will be ignored when we check your submission.
As mentioned above, you must also attend the following lab (in week 6) and answer your tutors 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? If so, can you point it out?
Does your implementation pass all the tests run by the unmodified testing functions?
Is your implementation of the two functions correct for any valid arguments (non-negative integers)?
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 the three functions, in addition to or in place of those provided?
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?
Does your implementation of the two functions count_factors, and count_prime_factors 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 pythons 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. 2 marks are based on the functionality of your submission, and 1 mark on the quality and readability of your code.
Updated:13 Aug 2019
Responsible Officer:Head of School
Page Contact:Patrik Haslum
Contact ANU
Copyright
Disclaimer
Privacy
Freedom of Information
+61 2 6125 5111The Australian National University, CanberraCRICOS Provider : 00120CABN : 52 234 063 906
Reviews
There are no reviews yet.