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


  1. Check 4.5
    Write a function that takes a list, lst, and destructively removes all the even numbers inside of it.

    def removeEvens(lst): pass def testRemoveEvens(): print("Testing removeEvens()...", end="") lst = [2, 4, 3, 1, 5, 6, 8, 10, 10, 9] removeEvens(lst) assert(lst == [3, 1, 5, 9]) lst = [2, 4, 6, 8, 10, 12] removeEvens(lst) assert(lst == []) lst = [3, 5, 7, 9, 11] removeEvens(lst) assert(lst == [3, 5, 7, 9, 11]) print("Passed.") testRemoveEvens() 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 test(student_code): exec(student_code) result = [] cases = [[22, 30, 29, 3, 0, 25, 7, 12, 27, 25, 30, 7, 3, 24], [22, 12, 26, 18, 8, 13, 23, 9, 8, 22, 27, 16, 8, 4, 1, 2, 19], [17, 8, 15, 2, 20, 20, 23, 14, 10, 4, 9, 29, 25, 0, 0], [15, 24, 20, 12, 10, 29, 3, 29, 24, 12, 17, 9, 24, 0, 16, 8, 23, 11], [8, 10, 7, 7, 21, 13, 30, 23, 6, 13, 7, 26, 3, 29, 8], [17, 6, 12, 5, 16, 16, 19, 4, 3, 29, 18, 25, 5, 18, 28, 0, 6], [0, 13, 18, 1, 19, 10, 29, 13, 9, 28], [23, 2, 9, 2, 22, 22, 27, 10, 29, 23, 17], [15, 29, 16, 12, 3, 8, 15, 25, 2, 23, 13, 27, 7, 27, 0, 10], [7, 29, 6, 28, 6, 12, 17, 29, 27, 18, 6, 3, 7], [16, 12, 2, 7, 12, 21, 13, 4, 3, 25, 12, 14, 0, 27, 22, 7, 30, 22], [26, 29, 29, 2, 18, 15, 12, 22, 18, 18, 26, 2, 6, 16, 9], [4, 2, 29, 16, 12, 27, 11, 24, 18, 2, 6], [28, 30, 16, 13, 25, 19, 13, 4, 8, 15], [1, 4, 23, 14, 11, 26, 28, 13, 21, 26, 13, 29, 4, 9], [4, 1, 15, 17, 28, 13, 29, 16, 9, 17, 20, 29, 21, 19, 21, 1], [15, 16, 13, 27, 17, 12, 21, 23, 30, 11, 16, 0, 12, 13, 18, 9, 1], [5, 1, 21, 17, 13, 7, 10, 1, 5, 25, 4, 2, 27, 16, 24, 26, 3, 30, 13], [18, 25, 14, 25, 16, 2, 9, 21, 17, 6], [20, 16, 20, 13, 20, 17, 6, 26, 0, 5, 2, 10], [18, 16, 15, 24, 4, 7, 5, 18, 11, 8, 17, 10], [26, 14, 14, 16, 7, 25, 2, 30, 3, 5, 11, 11, 4, 2, 24, 8], [14, 8, 12, 29, 30, 11, 13, 9, 27, 19, 20, 27, 10, 6, 29, 0], [14, 4, 8, 24, 4, 17, 28, 17, 24, 13, 19, 11], [21, 21, 27, 11, 4, 13, 9, 10, 28, 3, 17, 15, 11, 15], [13, 16, 18, 27, 9, 26, 27, 11, 0, 9, 18, 26, 29], [5, 1, 17, 11, 30, 6, 0, 9, 19, 11, 5], [21, 12, 24, 8, 21, 16, 0, 9, 23, 28, 29, 20, 20, 3, 12, 26, 14, 26, 21], [15, 9, 7, 2, 6, 4, 9, 13, 16, 20, 26, 12, 12, 3, 18, 22, 10, 3, 27], [29, 13, 15, 18, 5, 27, 25, 21, 3, 7, 19, 12, 9, 15, 16, 28, 22, 15, 8]] for lst in cases: removeEvens(lst) result.append(lst) return result 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 result = test(student_code) certificate.append((result, type(result))) set_certificate(certificate_div_id, str(certificate)) except: set_certificate(certificate_div_id, "error")

  2. Back to notes