Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
34 views
ubuntu2204
1
#check if function exists
2
import time
3
#import multiprocessing
4
testk=np.linspace(1,5,10)
5
6
7
8
assert "make_matrix" in dir(), 'Make sure you define your function and use the specified function name.'
9
10
11
12
try:
13
out = make_matrix(testk)
14
except TypeError:
15
16
17
18
19
20
raise AssertionError('Your function should take 2 input arguments and ignore eigenvectors.')
21
22
23
24
25
26
else:
27
pass
28
29
30
31
32
33
testM=make_matrix(testk)
34
tnb=len(testk)-1
35
36
37
38
39
40
41
42
#test output
43
assert type(testM)==np.ndarray, 'Your function output should be a 2D numpy array.'
44
45
46
47
48
49
assert np.size(testM)/tnb==tnb, 'Your matrix should have a size of a x a where a is the number of beads (NOT strings!)'
50
51
52
53
54
55
assert testM[tnb-1][tnb-1]==testk[-1]+testk[-2], 'Your matrix values are incorrect.'
56
57
58
59
60
tN=3
61
62
63
64
65
assert np.isclose(testM[tN][tN+1],-testk[tN+1]), 'Your matrix values are incorrect.'
66
67
#check if for loops used
68
testk=np.linspace(1,100,100)
69
import timeit
70
assert timeit.timeit('make_matrix(testk)','from __main__ import make_matrix, testk',number=100000)<8.5, 'Your code is inefficient for large matrices. Try to simplify it by avoiding the use of if statements.'
71
72
73
74
75
## CHANGES HERE: check if for loops used
76
77
78
79
80
81
82
# check if local variables used
83
assert not testM==make_matrix(testk*2), 'Do not use global variables - your function should work for any input.'
84
85
86
87
88
89
from IPython.display import clear_output
90
clear_output(wait=False)
91
print('************\n Well done! All test passed. You can move to the next part of the task.\n************')
92