CMU 15-112 Spring 2019: Fundamentals of Programming and Computer Science
Check 2.3
-
Check 2.3
Write a functionprintMultiplicationTable(n)
that takes a number, n, and prints out the result of the n x n multiplication table (starting at 0x0 and ending at nxn). For example, a 4x4 multiplication table would look like:0 0 0 0 0 0 1 2 3 4 0 2 4 6 8 0 3 6 9 12 0 4 8 12 16You do not need to do any special formatting to make the numbers line up nicely, and you should not print anything besides the multiplication results.def printMultiplicationTable(n): return printMultiplicationTable(4)
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 strip_all_but_numbers(line): output = "" for c in line: if c.isdigit(): output += c return output def make_certificate(student_code_div_id, certificate_div_id): try: student_code = get_student_code(student_code_div_id) certificate = [] execCapture = captureIO() sys.stdout = execCapture sys.stderr = execCapture exec(student_code) for i in range(6): capture = captureIO() sys.stdout = capture sys.stderr = capture printMultiplicationTable(i) raw_result = capture.get_output() for raw_line in raw_result.splitlines(): line = strip_all_but_numbers(raw_line) if line != "": certificate.append((line, type(line))) set_certificate(certificate_div_id, str(certificate)) except: set_certificate(certificate_div_id, "error")
Back to notes