# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("7_python_tools_q", "week_3", "readings", assignment_points = 15.0, assignment_tag = 'week3-readings')
# Initialize Otter
import otter
grader = otter.Notebook("7_python_tools_q.ipynb")
โ๐ Python Tools for Engineering ๐ ๏ธ#
# Run this block of code by pressing Shift + Enter to display the question
from questions._7_python_tools_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._7_python_tools_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._7_python_tools_q import Question3
Question3().show()
NumPy Matrix Determinant#
Instructions:#
Write a Python function using NumPy that computes the determinant of a 2x2 matrix. The function should take a 2D list as input, convert it to a NumPy array, and return the determinant.
We have provided you with the structure for the function matrix_determinant(matrix)
since you do not know that yet. You need to complete the function by adding the necessary code to calculate the determinant.
Your solution must include:
A function
matrix_determinant(matrix)
that:Accepts a 2D list (e.g.,
[[1, 2], [3, 4]]
).Converts it into a NumPy array. Using the
np.array()
function. You can save this as the variablenp_matrix
.Calculates and returns the determinant. Using the
np.linalg.det()
function. You must save this as the variabledeterminant
.
Tip
If you do not know how any of the functions mentioned above work, you can look them up in the documentation help(function_name). This is a good test taking strategy as you will have access to the documentation during the quizzes and tests.
Test cases to validate the correctness of your implementation.
# Import numpy here, make sure to use the common alias np.
...
def matrix_determinant(matrix):
# Convert the matrix to a numpy array and return the determinant
# make sure to save it to the variable `np_matrix`
...
# Compute the determinant of the matrix
# make sure to save it to the variable `determinant`
...
return determinant
# Example test case
result = matrix_determinant([[1, 2], [3, 4]])
print(f"Determinant: {result}")
grader.check("numpy-matrix-determinant")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week3-readings", "7_python_tools_q")