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


  1. Check 7.3
    Write a function itemCount which takes a list, lst, and returns a dictionary that maps each element in lst to the number of times it occurs. For example, itemCount([1, 2, 1, 3]) would return { 1: 2, 2: 1, 3: 1 }.
    def itemCount(lst): return 42 def testItemCount(): print("Testing itemCount...", end="") assert(itemCount([1, 2, 1, 3]) == { 1 : 2, 2 : 1, 3 : 1 }) assert(itemCount(["a", "a", "a"]) == { "a" : 3 }) assert(itemCount([]) == { }) print("Done!") testItemCount() 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 multi_ord(s): out = 0 for c in s: out *= 256 out += ord(c) return out def int_or_ord(o): if type(o) == int: return o else: return multi_ord(o) def make_certificate(student_code_div_id, certificate_div_id): student_code = get_student_code(student_code_div_id) certificate = [] try: execCapture = captureIO() sys.stdout = execCapture sys.stderr = execCapture exec(student_code) test_list = [[1, 1, 1, 2, 3], [2, 3, 1, 4], [], ['a', 'a', 'a'], [1, 2, 1, 3, 4, 1, 2, 5, 0], [1, 'a', 2, 1, 3, 4, 1, 'b', 'ab'], [99, 0, 2, 0, 99, 2], ['n', 'J', 'c', 'H', 'q', 'T', 'z', 'B', 'N', 27, 'Y', 'B', 'v', 'q', 'R', 'h', 'Q', 'h', 10], [28], [14, 25, 'c', 'q'], ['o', 'B', 'P', 17, 'd', 17, 'u', 15, 'h', 'K', 'f', 'G', 'V', 'O', 'i', 24, 'f', 'g', 23, 'k', 23, 'w', 'k', 21, 'L', 26, 'T', 'N', 'X'], [19, 'u', 'u', 'A', 'U', 24, 'A', 'q', 'd', 'T', 'b', 'z', 'h', 11, 'J', 'n', 'U', 'S', 'S', 'E', 'g', 'B', 'j', 'y', 'x', 'Y', 'e'], ['n', 'n', 'V', 'f', 'm', 23, 29, 24, 'Z', 'b', 'r', 17, 'd', 'o', 'c', 'Z', 'm', 20, 'O'], ['D', 'u', 14, 14, 12, 18, 'W', 'm', 'y', 17, 'K', 'r', 29, 28, 'X', 'N', 23, 27, 'u', 23, 23, 'G', 'z', 'U', 18, 'C', 25, 'g', 10, 'j'], ['s', 'r', 'e', 'U', 20, 14, 'm', 'c', 'o', 'a', 20, 'H', 'a', 10, 19, 'F', 'f', 'W', 'g', 24, 'T', 't', 'u', 'l', 'S'], [29, 'N', 'W', 'o', 'b', 'i', 's', 12, 16, 26, 'o', 29, 'y', 'i', 'X', 28, 'T', 'w', 25], ['J', 'j', 22, 'D', 'f', 'w', 'i', 'a', 29, 'H', 'Z', 'T', 'A', 'V', 'l', 'q', 'i'], ['B', 25, 27, 'l', 23, 'z', 'J', 'v', 'F', 'V', 'k', 'O', 'K', 'c', 'o', 18, 'H', 'T', 21, 'p', 'O', 'C', 'l', 'E', 12, 21, 'Z', 'i'], ['g', 27, 'Z', 'c', 20, 'Q', 'O', 11, 'l', 'e', 14, 'o', 23, 'T', 'E', 'X', 'a', 11, 'n', 'f', 12], ['T', 23, 'L', 29, 'g', 26, 21, 20, 'G', 'a', 'U', 'p', 'C']] try: for test in test_list: output = itemCount(test) certificate.append((sorted(output.items(), key=lambda e: (int_or_ord(e[0]), e[1])), type(output))) except: window.alert("wtf") window.alert(repr(test)) except: set_certificate(certificate_div_id, "error") set_certificate(certificate_div_id, str(certificate))

  2. Back to notes