CMU 15-112 Spring 2019: Fundamentals of Programming and Computer Science
Check 5.3


  1. Check 5.3
    The function printItems(lst) uses a nested loop to print out every item in a 2D list. Without changing the number of rows and columns in myList, change the values of myList so that printItems(myList) prints the numbers 0, 3, 2, 4, 6, and 5 in that order. Remember, myList should still have 3 rows and 2 columns!
    #Change this! myList= ( [ [0,0], [0,0], [0,0] ] ) #Don't change this! def printItems(lst): for row in range(len(lst)): for col in range(len(lst[row])): print(lst[row][col],end=' ') #Should print 0 3 2 4 6 5 printItems(myList) 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 internalPrintItems(lst): for row in range(len(lst)): for col in range(len(lst[row])): print(lst[row][col],end=' ') def make_certificate(student_code_div_id, certificate_div_id): student_code = get_student_code(student_code_div_id) certificate = [] try: execCapture = captureIO() sys.stdout = execCapture sys.stderr = execCapture exec(student_code) capture = captureIO() sys.stdout = capture sys.stderr = capture internalPrintItems(myList) output = execCapture.get_output() certificate.append((output, type(output))) rows = len(myList) cols = len(myList[0]) certificate.append((rows, type(rows))) certificate.append((cols, type(cols))) except: set_certificate(certificate_div_id, "error") set_certificate(certificate_div_id, str(certificate))

  2. Back to notes