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, 19 November 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: TurnitIn 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 7 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 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, only after calling a function. In particular, all testing should be be done from within a main() function.
You can also add any other functions which you might find useful. Only the three required functions cross, magic, and benford 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 cross, magic, benford. 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/minitest2.py
"""
MATH20621 - Mini test 2
Student name: add your name
Student id: add your id number
Student mail: your.name.123@student.manchester.ac.uk
"""
# Feel free to add other functions you find useful.
# Problem 1
def cross(X, Y):
"""
add a docstring
"""
# TODO: add your code
return
# Problem 2
def magic(L):
"""
add a docstring
"""
# TODO: add your code
return
# Problem 3
def benford(s):
"""
add a docstring
"""
# TODO: add your code
return
# main() function for all the testing
def main():
# do your testing here
return
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 should (hopefully) be formulated in a way that there are no doubts. In case there remains a question on a problem formulation, please post it to the Blackboard Discussion Forum and it will be answered.
Write a function cross(X, Y) that computes the cross-product of two vectors X, Y. The vectors X, Y are provided as lists with integer (int) or floating point (float) elements, and the function returns (not prints!) a list with three floating point elements. Recall that the cross-product between two three-dimensional vectors is defined as
Each of the lists X and Ycan contain up to three integer or floating point numbers (or be an empty list). If the number of elements in a list is strictly less than three, the remaining elements are assumed to be zero.
If any of the lists X or Y has strictly more than three elements, the function should raise a ValueError exception.
If any of the list elements is not an integer or float, the function should raise a TypeError exception.
Note: Exceptions should just be raised and should not be treated within the function. They should be available when calling the function. It is not necessary nor advised to use the numpy module for this problem.
Examples:
try:
L = cross([1, 3.2], [-1.9, 3.7, 2.1, 9])
except ValueError:
print("at least one of the vectors contains more than three elements")
except TypeError:
print("at least one of the vectors contains a non-int or non-float")
L = cross([1, 3], [-1.2, 3.2, 2]) should return the list L = [6.0, -2.0, 6.8].Write a function magic(L) that takes as argument a list L. The list L is a list with $n$ elements, each of which is itself a list with $n$ integer elements. (You can assume without checking that L is of the correct format.)
The function checks whether L corresponds to a valid magic square in the following sense:
The function returns the Boolean value True if these conditions are satisfied, and the Boolean value False otherwise.
Example: Consider the list
L = [[2, 7, 6],
[9, 5, 1],
[4, 3, 8]]
The call magic(L) should return True since L is a valid magic square, with all the following sums equal 15:
Further reading: https://en.wikipedia.org/wiki/Magic_square
Definition: A number in a string s is defined as the longest possible consecutive sequence of digits (between 0 and 9), containing at least one digit and at most one decimal point (.).
Task: Write a function benford(s) that takes a string argument s and then returns a list L of tuples (d, f). Here, d is a nonzero digit between 1 and 9 and f is the frequency of that digit appearing first (left-most) in the numbers contained in s. Both d and f should be integers. The string s is simply read from beginning to end to identify one number at a time.
The returned list L should be sorted by number of occurences of each nonzero digit, with the most frequent digit appearing first. If two nonzero digits appear the same number of times in s, they should be listed in naturally sorted order, that is, with the smaller digit appearing first.
Example: Consider the string is s = "test 123.456.78.003... test 32-16,2.3 .00 test.". The contained numbers are:
123.456 .78 .003 32 16 2.3 .00
The first (left-most) nonzero digits in these numbers (if they exist) are
1 7 3 3 1 2
Hence,
L = benford("test 123.456.78.003... test 32-16 2.3,0 test.")
should return the list
L = [(1, 2), (3, 2), (2, 1), (7,1)]
Note: It is up to you whether you want to use one of Python's in-built sorting functions for this, or perhaps a variant of the my_sort function from the week 4 exercises. Also, don't worry too much if you struggle to get the natural sorting of digits with the same frequency right and just manage to sort by number of occurences; you'll get partial credits for that.
Further reading: You can use your function to test Benford's Law on some real-world data. This law has been used for fraud detection in accounting or electoral data; see https://www.youtube.com/watch?v=vIsDjbhbADY and also Wikipedia.