This mini test consists of three Python functions to be written, one for each problem. The task is to prepare a Python script using the below code template and complete the functions with the appropriate code. Submit your Python script via upload on Blackboard until Friday, 22 October 2021 at 1pm the latest. You can resubmit as often as you like and only the last submission before the deadline will be marked. Aim to submit an hour earlier as the deadline is strict and late submissions will not be accepted.
Important: Blackboard will send you a confirmation email after successful submission. If you did not receive this email, you might have forgotten to confirm your submission.
You need to complete this mini test independently on your own, but you are allowed to use online resources and all lecture notes and exercise solutions. The lecture notes from weeks 1 to 3 contain all that is required to solve the below problems. You are not allowed to ask other humans for help. In particular, you are not allowed to send, give, or receive Python code to/from classmates and others.
The University Guidelines for Academic Malpractice apply: http://documents.manchester.ac.uk/display.aspx?DocID=2870
Important: Even if you are the originator of the work (and not the one who copied), the University Guidelines require that you will be equally responsible for this case of academic malpractice and may lose all coursework marks (or even be assigned 0 marks for the whole course).
Use the below template to complete the assignment. You can include any other functions in your script, but please make sure that all code is within a function. Your script shouldn't do anything when run, except possibly calling the main() function for testing. All testing should be done from within the main()
function.
You can also add any other functions which you might find useful, like the prime
function already included in the template. Only the three required functions lucasprime
, twinprime
, and credit
will be marked, not the testing code.
It is good practice to include docstrings for each function, but in this mini test they won't be marked.
Important: Do not alter the names of the three required functions lucasprime
, twinprime
, and credit
. Ensure they can be called with the parameters precisely as specified, and that they return (not print!) values of the correct data types. The three functions will be marked automatically and failure to follow the problem description will result in loss of marks.
You can also download the template as a .py file from https://personalpages.manchester.ac.uk/staff/stefan.guettel/py/minitest1.py
"""
MATH20621 - Mini test 1
Student name: add your name
Student id: add your id number
Student mail: your.name123@student.manchester.ac.uk
"""
# Feel free to other functions you find useful.
def prime(n):
"""
Returns Boolean `True` if the integer `n` is a prime number,
and `False` otherwise. See problem 3 of the week 3 exercises.
"""
if n < 2:
return False
for k in range(2, n):
if n % k == 0:
return False
return True
# Problem 1
def lucasprime(n):
"""
add a docstring
"""
# TODO: add your code
return
# Problem 2
def twinprime(n):
"""
add a docstring
"""
# TODO: add your code
return
# Problem 3
def credit(n, mode):
"""
add a docstring
"""
# TODO: add your code
return
# main() function for all the testing
def main():
print("should return True: ", lucasprime(2207))
print("should return False: ", lucasprime(-7))
print("should return 29: ", twinprime(29))
print("should return True: ", credit(4578423013769219, 'verify'))
print("should return False: ", credit(4578423023769219, 'verify'))
print("should return 9: ", credit(457842301376921, 'calculate'))
main() # call main() function to run all tests
As this is part of the course assessment, we will not be able to help you solving these problems in the lab classes or elsewhere. However, if you provided a valid email address in the docstring at the beginning of your .py file, you will receive a feedback report with a score for each function.
Please read the problem descriptions carefully! The problems are formulated in a way that there shouldn't be any doubts. In case there remains a question about a problem formulation, please post it to the Blackboard Discussion Forum and it will be answered.
Write a function lucasprime(n)
that accepts an integer argument n
. You can assume without checking that n
is an integer. The function returns (not prints!) the Boolean value True
if n
is a Lucas prime number, and the Boolean value False
otherwise.
Definition: The Lucas numbers are defined as $L_0=2,$ $L_1=1,$ and $L_j = L_{j-1} + L_{j-2}$ for $j\geq 2$.
Definition: A Lucas prime is a Lucas number that is prime.
Hint: You might find it useful to use the prime(n)
function we discussed in week 3. It has already been added to the above script template. You can add any additional functions you might need.
Further reading: See https://en.wikipedia.org/wiki/Lucas_number, where you can also find examples for testing. Lucas numbers are the second most common pattern in sunflowers after Fibonacci numbers, when clockwise and counter-clockwise spirals are counted, according to an analysis of 657 sunflowers in 2016 (https://royalsocietypublishing.org/doi/10.1098/rsos.160091).
Write a function twinprime(n)
that accepts an integer argument n
. You can assume without checking that n
is an integer. The function returns the largest number $p\leq n$ that is a twin prime. The returned number $p$ should be an integer. If no such number exists for the provided value of n
, the function returns None
(of the NoneValue
data type).
Definition: A twin prime $p$ is a prime number that is either 2 less or 2 more than another prime number.
Further reading: See https://en.wikipedia.org/wiki/Twin_prime, where you can also find examples for testing.
Write a function credit(n, mode)
that takes as input an integer n
and an optional string argument mode
. You can assume without checking that n
is an integer and mode
is a valid string (if provided). The function is intended to help with the validation and generation of 16-digit credit card numbers. The first 15 digits of a typical credit card number identify the institution that issued the card and the individual account, and the final (right-most) digit is a check digit.
The validation of a credit card number, explained at an example, goes as follows:
4578423013769219
.4-5-7-8-4-2-3-0-1-3-7-6-9-2-1
.8-5-14-8-8-2-6-0-2-3-14-6-18-2-2
.8-5-5-8-8-2-6-0-2-3-5-6-9-2-2
.8 + 5 + 5 + 8 + 8 + 2 + 6 + 0 + 2 + 3 + 5 + 6 + 9 + 2 + 2 = 71
.71 + 9 = 80
is divisible by 10. Hence this is a valid credit card number.The function credit
can do two things depending on the value of mode
.
If mode
has the string value verify
, the function checks whether n
corresponds to a valid credit card number by verifying that the check digit is correct, using the algorithm explained above. The function then returns the Boolean value True
if the integer n
corresponds to a valid credit card number, and otherwise False
.
If mode
has the string value calculate
, n
should be a positive 15-digit integer and the function returns the single check digit as an integer. If n
is not a positive 15-digit integer, then the function should return None
.
If mode
is not specified, the value verify
should be assumed by default.
Hint: Throughout this problem you can assume that a credit card number starts with a nonzero digit on the left. For example, 1234567890121234 is considered a 16-digit integer, but 0001234567891234 is not.
Some test data: Following the above example,
credit(4578423013769219, 'verify')
and credit(4578423013769219)
should return the Boolean value True
credit(457842301376921, 'calculate')
should return the integer 9
Optional further reading: The above is a variant of Luhn's algorithm (https://en.wikipedia.org/wiki/Luhn_algorithm).