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


  1. Check 2.1
    If you loop over range(4, 20, 4), what numbers will be visited by the loop variable? Modify the function below to return a string of the numbers with commas separating each number.
    def answer(): # Insert your answer below, replacing the comma-separated numbers # currently being returned return "1,2,3" 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();'); def clean_output(s): allowed_chars = "1234567890," output = "" for c in s: if c in allowed_chars: output += c return output 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 exec(student_code) raw_output = answer() output = clean_output(raw_output) certificate.append((output, type(output))) except: pass set_certificate(certificate_div_id, str(certificate))

  2. Back to notes