CMU 15-112 Spring 2019: Fundamentals of Programming and Computer Science
Check 1.10
-
Check 1.10
Write the functioncanLegallyDrive(country, age)
which takes in a country and an age, and returns whether or not a person of the given age can legally drive in the given country. We'll restrict the countries to "US" or "Rwanda". The legal driving age in the US is 16. In Rwanda it's 18. SocanLegallyDrive("US", 16)
should return True.canLegallyDrive("Rwanda", 16)
should return False.def canLegallyDrive(country, age): return 42 def testCanLegallyDrive(): print("testing canLegallyDrive...", end="") assert(canLegallyDrive("US", 15.5) == False) assert(canLegallyDrive("US", 15) == False) assert(canLegallyDrive("US", 16) == True) assert(canLegallyDrive("US", 17) == True) assert(canLegallyDrive("US", 18.5) == True) assert(canLegallyDrive("Rwanda", 14.5) == False) assert(canLegallyDrive("Rwanda", 17) == False) assert(canLegallyDrive("Rwanda", 18) == True) assert(canLegallyDrive("Rwanda", 19) == True) assert(canLegallyDrive("Rwanda", 20.5) == True) print("passed!") testCanLegallyDrive()
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 (country, age) in [("US", 13.5), ("US", 15), ("US", 16), ("US", 17), ("US", 18.5), ("Rwanda", 14.5), ("Rwanda", 16), ("Rwanda", 17), ("Rwanda", 18), ("Rwanda", 19.5), ("Rwanda", 21)]: result = canLegallyDrive(country, age) certificate.append((result, type(result))) set_certificate(certificate_div_id, str(certificate)) except: set_certificate(certificate_div_id, "error")
Back to notes