CMU 15-112 Spring 2019: Fundamentals of Programming and Computer Science
Check 1.8
-
Check 1.8
Fill in the body of the functiondivisibleByThree
so that it returns True if the input is a number and is divisible by three.def divisibleByThree(n): # We probably want to use isinstance(n, int) and a % check return 42 def testDivisibleByThree(): print("testing divisibleByThree...", end="") assert(divisibleByThree("hello") == False) assert(divisibleByThree(4.2) == False) assert(divisibleByThree(3) == True) assert(divisibleByThree(5) == False) assert(divisibleByThree(45) == True) print("passed!") testDivisibleByThree()
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): try: student_code = get_student_code(student_code_div_id) certificate = [] capture = captureIO() sys.stdout = capture sys.stderr = capture exec(student_code) for test in [1,2,3,4.2,6.7,"hello","goodbye","",9,12]: result = divisibleByThree(test) certificate.append((result, type(result))) set_certificate(certificate_div_id, str(certificate)) except: set_certificate(certificate_div_id, "error")
Back to notes