Check 5.2
Write the function initListVals(rows, cols, n)
which takes three non-negative integer values and returns a 2D list containing the correct number of rows and columns designated by row and col. Additionally, every item in each row should be assigned a value of n. Careful! No rows or items should be aliased to one another.
def initListVals(rows, cols, n):
pass
def testInitListVals():
print("Testing initListVals...", end="")
assert(initListVals(2,2,4)==[[4,4],[4,4]])
assert(initListVals(2,3,10)==[[10,10,10],[10,10,10]])
assert(initListVals(1,1,7)==[[7]])
assert(initListVals(0,0,3)==[])
lst=initListVals(3,3,5)
assert(lst==[[5,5,5],[5,5,5],[5,5,5]])
print("Testing for aliasing...", end="")
lst[1][1]=7
assert(lst==[[5,5,5],[5,7,5],[5,5,5]])
print("Passed.")
testInitListVals()
import sys
def set_certificate(certificate_div_id, certificate):
document[certificate_div_id].textContent = certificate
def get_student_code(student_code_div_id):
raw_student_code = document[student_code_div_id].textContent
return window.patchCodeToCheckTimeout(raw_student_code, 'check_timeout();');
class captureIO:
def __init__(self):
self.captured = []
def get_output(self):
out = ""
for c in self.captured:
out += str(c)
return out
def write(self, data):
self.captured.append(data)
def flush(self):
pass
def make_certificate(student_code_div_id, certificate_div_id):
student_code = get_student_code(student_code_div_id)
certificate = []
try:
capture = captureIO()
sys.stdout = capture
sys.stderr = capture
exec(student_code)
##starting testing
num_tests = 15
test_cap = 5
test_cases = [(5, 4, 3), (2, 3, 2), (2, 2, -1), (3, 5, 3), (3, 4, -2), (2, 3, -5), (3, 1, -2), (2, 4, 0), (4, 4, 2), (3, 4, -2), (2, 1, -3), (2, 3, 0), (4, 2, 0), (5, 3, 4), (4, 3, -5)]
test_null = initListVals(0, 0, -22)
certificate.append((test_null, type(test_null)))
test_null2 = initListVals(1, 0, 0)
certificate.append((test_null2, type(test_null2)))
test_null2[0].append(237)
certificate.append((test_null2, type(test_null2)))
test_null22 = initListVals(3, 0, 0)
certificate.append((test_null22, type(test_null22)))
test_null22[1].append(237)
certificate.append((test_null22, type(test_null22)))
test_null3 = initListVals(0, 1, 11)
certificate.append((test_null3, type(test_null3)))
for i in range(len(test_cases)):
r, c, n = test_cases[i][0], test_cases[i][1], test_cases[i][2]
student_ans = initListVals(r, c, n)
certificate.append((student_ans, type(student_ans)))
if i%3 == 0:
student_ans[0][0] = n - test_cap
#just changing n in first position
certificate.append((student_ans, type(student_ans)))
elif i%3 == 1:
#changing n in middle position
student_ans[r//2][c//2] = n - 2*test_cap
certificate.append((student_ans, type(student_ans)))
else:
#changing n in last position
student_ans[r-1][c-1] = n - 3*test_cap
certificate.append((student_ans, type(student_ans)))
except:
set_certificate(certificate_div_id, "error")
set_certificate(certificate_div_id, str(certificate))