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


  1. Check 7.1
    Write a function that takes two sets, s and t, and returns a new set that contains elements that only occur in one of the two sets. Do not use the built-in symmetric_difference or ^ methods (it's okay if you don't know what these are).

    def oneOrTheOther(s, t): return def testOneOrTheOther(): print("Testing oneOrTheOther...", end="") assert(oneOrTheOther({2, 3, 4, 5}, {1, 2, 3, 4}) == {1, 5}) assert(oneOrTheOther({"a", "b", "c", "d"}, {"b", "d"}) == {"a", "c"}) assert(oneOrTheOther(set(), {10, 20}) == {10, 20}) print("Done!") testOneOrTheOther() 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): student_code = get_student_code(student_code_div_id) certificate = [] try: capture = captureIO() sys.stdout = capture sys.stderr = capture if ('symmetric_difference' in student_code) or ('^' in student_code): raise Exception("Using builtins for solution") exec(student_code) cases = [[{1, 2, 3, 4, 5, 6, 7, 8, 10}, {1, 2, 3, 5, 6, 7, 8, 10}], [{1, 2, 3, 4, 6, 8, 9, 10}, {1, 2, 4, 5, 6, 7, 8, 10}], [{1, 2, 4, 5, 6, 7, 8, 10}, {1, 2, 4, 5, 6, 7, 8, 9, 10}], [{1, 2, 3, 4, 5, 6, 7, 10}, {2, 3, 4, 5, 6, 7, 9, 10}], [{2, 3, 4, 5, 6, 7, 8, 10}, {2, 3, 4, 5, 9, 10}], [{1, 3, 4, 5, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}], [{1, 3, 5, 6, 7, 9, 10}, {3, 4, 6, 7, 8, 9}], [{1, 2, 3, 4, 5, 6, 7, 8, 10}, {2, 3, 4, 5, 6, 7, 8, 10}], [{1, 2, 4,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{1, 2, 4, 5, 6, 7, 9, 10}], [{2, 4, 5, 6, 7, 9, 10}, {1, 2, 3, 4, 5, 7, 8, 9}]] for (s, t) in cases: output = oneOrTheOther(s, t) certificate.append((sorted(output), type(output))) set_certificate(certificate_div_id, str(certificate)) except: set_certificate(certificate_div_id, "error")

  2. Back to notes