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


  1. Check 4.2
    Write the function copyFrontToBack(lst) that takes a list, lst. If the list has at least two elements, it changes the last element to be the same as the first element; otherwise, it does nothing.
    def copyFrontToBack(lst): pass def testCopyFrontToBack(): print("Testing copyFrontToBack...", end="") a = [1, 2, 3, 4] copyFrontToBack(a) assert(a == [1, 2, 3, 1]) b = [] copyFrontToBack(b) assert(b == []) print("Passed.") testCopyFrontToBack() 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): try: student_code = get_student_code(student_code_div_id) certificate = [] capture = captureIO() sys.stdout = capture sys.stderr = capture exec(student_code) for test in [[19, 5, 24, 18, 8, 2, 29, 27, 13, 26, 24, 27, 0, 27, 9, 8], [0, 28, 19, 5, 8, 29, 21, 4, 0, 2, 25, 12, 12, 2, 29, 18, 19, 17, 25, 24], [2, 6, 25, 23, 19, 8, 21, 7, 4, 21, 13, 10, 3, 26, 21, 22, 17, 24], [21, 4, 16, 18, 10, 15, 18, 13, 16, 27, 13, 14, 0, 9, 25, 17, 19, 23], [4, 0, 16, 20, 6, 5, 0, 29, 1, 12, 24, 1, 2, 13, 11, 7, 27, 12, 0, 10], [16, 7, 20, 26, 3, 28, 7, 16, 28, 6, 22, 7, 20], [], [27, 22, 14, 0], [0, 14, 8, 18, 11, 11, 19, 26, 20, 25, 15, 7, 28, 17, 8], [14, 10, 10, 17, 26, 19, 22, 8, 16, 25, 18, 20], [21, 30, 5, 1, 17, 1, 29, 22, 26, 30, 2, 0, 28], [26, 5, 17, 14, 10, 10, 19, 19, 5, 11, 4, 30], [12, 5, 20, 15, 21, 26, 16, 4, 0, 29, 30, 2, 21, 19, 7, 27, 24, 19, 18, 24], [22, 8, 15, 13, 15, 14, 2, 18, 12, 19, 2, 11, 23, 12, 10, 2], [25, 3, 22, 26, 23, 28, 0, 1, 22, 30, 24], [21, 30, 22], [11, 15, 25, 26, 19, 0, 24, 6, 10, 21, 8, 28, 13, 2], [8, 22, 3, 26, 8, 17, 19, 19, 6, 20, 21], [5, 1, 16, 6, 13, 11, 19, 26, 23, 3, 1, 24, 11, 28, 10, 11, 28], [18, 22, 28, 27, 16, 8, 18, 13, 29, 1, 14, 0, 7, 17], [22, 21, 6, 28, 30, 10, 8, 20, 27, 29, 9, 0, 6, 11], [14], [20, 17, 25, 27, 13, 20, 12, 20, 15, 9, 8, 19, 18, 6, 25, 24], [3, 0, 27], [28, 4, 16, 10, 0, 3, 9, 27, 7, 4, 24, 0, 2, 11, 21, 4, 22, 28], [3, 29, 5, 0, 10, 30, 6, 17, 5, 10, 12, 6, 12, 21, 3], [6, 21, 17, 9, 9, 28, 2, 2, 13, 8], [24, 15, 5, 22, 22, 13, 18, 28], [24, 5, 27, 29, 2, 30, 18, 8, 16, 6, 10], [8, 10, 7, 30, 13, 28, 27, 11, 9, 22, 26, 18, 20, 23, 24, 8, 3, 3, 29, 6], [], [1], [2]]: copyFrontToBack(test) certificate.append((test, type(test))) set_certificate(certificate_div_id, str(certificate)) except: set_certificate(certificate_div_id, "error")

  2. Back to notes