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


  1. Check 9.2
    Modify the given listSum function so that it only sums elements which are integers and ignores any non-int elements. You'll need to add either another base case or another recursive case to do this.
    def listSum(lst): if (len(lst) == 0): return 0 else: return lst[0] + listSum(lst[1:]) def testListSum(): print("Testing listSum()...", end="") assert(listSum([2, 3, 5, 7, 11]) == 28) assert(listSum([2, "foo", 5, 7.45, 6, "blah"]) == 13) assert(listSum([]) == 0) print("Done.") testListSum() 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 test_rec = """ certificate = [] def inject_counters(): def callCounter(f): f.calls = 0 f.is_recursive = False def g(*args): f.calls += 1 initial_calls = f.calls out = f(*args) if (f.calls > initial_calls): f.is_recursive = True return out return g, f g = globals() current_objects = list(g.items()) originals = [] for name, obj in current_objects: try: if callable(obj): g[name], original = callCounter(obj) originals.append(original) except: pass return originals def did_recur(originals): return any(map(lambda f: f.is_recursive, originals)) originals = inject_counters() listSum([2, "foo", 5, 7.45, 6, "blah"]) recurs = did_recur(originals)""" def make_certificate(student_code_div_id, certificate_div_id): student_code = get_student_code(student_code_div_id) try: execCapture = captureIO() sys.stdout = execCapture sys.stderr = execCapture exec(student_code+"\n"+test_rec, globals()) if recurs: tests = [[0.33719535793335975, 4.049922030841282, 2.8346364800472488], [8.288607679913653, 0.5578810793678838, 7, 3.03020190329081, 0.10992439885860306, 'JF\tpwF', 'n', 6], [7, 8, 'nTHn~/%V', 7], ['s2n.!Yq', 'Z8zyt!l&', 4, 4, 5, 'Y ^', 3.1401969696637524], ['KEA"O2b#', 2, 4.588950272172619, 'E', 4.354954854304068, 6.730889957307453], [0.9465660303232418, 3, 6.400836444740136, "9A`|7'\t}0"], ['DF?n~', 6.663596118456904], [5, 3.229447760945239, 6.7465759293229395], [8.07731922541752, '', 8, 3, 'S', 6, 'n \x0b+'], ['h,8y=Oo^m', 'oOr=b]f9A', 8.48277306850879], [8.812018579261194, "6(N'6Up", 6.382114634740901, 'aF8ze,~\t'], [0.9894418701948772, 6, 9, 1.2886390953818525, 4, 'l|aZ.', 'WGZ`x:F', 5, 7]] test1 = [2, 3, 5, 7, 11] test2, test3 = [2, "foo", 5, 7.45, 6, "blah"], [] tests.extend([test1, test2, test3]) for test in tests: output = listSum(test) certificate.append((output, type(output))) set_certificate(certificate_div_id, str(certificate)) else: set_certificate(certificate_div_id, "Not Recursive") except: set_certificate(certificate_div_id, "error")

  2. Back to notes