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


  1. Check 9.1
    Write the function combineStrings(lst), which takes a list of strings, lst, and returns a single string with all of lst's strings combined in order. You must solve this problem recursively- do not use loops, comprehensions, or join.
    def combineStrings(lst): pass def testCombineStrings(): print("Testing combineStrings()...", end="") assert(combineStrings(["a", "b", "c"]) == "abc") assert(combineStrings(["foo"]) == "foo") assert(combineStrings([]) == "") print("Done.") testCombineStrings() 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() combineStrings(["a", "b", "c"]) 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 = [['Y', 'L', 'W', 'I', '=%p~)W', 'n'], ['c:', ''], ['\\7', '8#Y\x0bapC'], ['', 'mJ#yHf[UU', ',e_s'], ['K(+Jqi'], ['=g', 'C4lq', 'N', '', 'J'], ['wts', '7vXGU\x0b', '', '1', '', ''], ['', '', '', 'Q=RVRg;}.', 'w-', "'\\:", 'f&S4&', 'ds'], ['i&b2', '', 'x', 'u{qAW', 'a', '70K\t'], ['nMc%', '', 'f', '', '-k', 'kb=tGO&D', ''], ['x\\', 'vtz>'], ['_', '', 'Co6#%y>e', '$q', '%{', '20k\t,']] test1 = ["a", "b", "c"] test2, test3 = ['foo'], [] tests.extend([test1, test2, test3]) for test in tests: output = combineStrings(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