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


  1. Check 3.7
    Write a function called approximateFraction(n,d) which takes in integers n and d which represent the numerator and denominator of the fraction n/d. The function should print a string in the format of "n/d is approximately f!" where n and d are replaced by the parameters and f is a float given exactly 2 decimal places. Use string formatting! For example, approximateFraction(5,9) prints "5/9 is approximately 0.56!"
    def approximateFraction(n,d): print() approximateFraction(1,12) 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 clean_line(line): output = "" for c in line: if c in '0123456789./': output += c return output 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) for (testN, testD) in [(39, 34), (3, 3), (16, 7), (30, 13), (22, 28), (32, 20), (8, 14), (9, 16), (38, 29), (0, 29), (30, 32), (15, 10), (38, 6), (37, 26), (38, 12), (9, 21), (31, 37), (19, 37), (3, 24), (35, 32)]: capture = captureIO() sys.stdout = capture sys.stderr = capture approximateFraction(testN, testD) output = clean_line(capture.get_output()) certificate.append((output, type(output))) except: pass set_certificate(certificate_div_id, str(certificate))

  2. Back to notes