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


  1. Check 10.7
    Recall the pythagorean theorem, which lets us calculate the third side length of a right triangle if we know the other two side lengths. If the triangle's side lengths are a, b, and c, where c is the longest side, we know that a**2 + b**2 == c**2
    Write a lambda function that takes a and b as inputs and returns c. This lambda function should be assigned to a variable called getHypotenuse.
    Note: The autograder for this problem will only consider the first line of your submission.
    getHypotenuse = #<----your answer goes here! def almostEqual(d1, d2): epsilon = 10**-8 return abs(d1 - d2) < epsilon def testGetHypotenuse(): print("Testing getHypotenuse...", end="") assert(almostEqual(getHypotenuse(3,4), 5)) assert(almostEqual(getHypotenuse(1,1), 2**0.5)) assert(almostEqual(getHypotenuse(0.4, 0.3), 0.5)) print("Passed!") testGetHypotenuse() 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): certificate = [] student_code = get_student_code(student_code_div_id) student_code = student_code.splitlines()[0] try: execCapture = captureIO() sys.stdout = execCapture sys.stderr = execCapture exec(student_code) tests = [(1, 8), (20, 11), (18, 10), (16, 15), (0, 14), (1, 9), (5, 8), (12, 14), (18, 13), (17, 11), (19, 20), (4, 7), (14, 3), (1, 7), (18, 13), (11, 16), (4, 11), (2, 17), (10, 11), (18, 3)] for (a,b) in tests: output = getHypotenuse(a,b) certificate.append((output, type(output))) set_certificate(certificate_div_id, str(certificate)) except: set_certificate(certificate_div_id, "error")

  2. Back to notes