CMU Coding Bootcamp

feat: oct 1 level 3

thecoded.prof ed475f62 22888541

verified
Changed files
+76
python
+52
python/oct1/level3/areaWithinThreeLines.py
··· 1 + from math import sqrt 2 + 3 + 4 + def almostEqual(x, y): 5 + return abs(x - y) < 10**-9 6 + 7 + 8 + def distance(a: tuple[float, float], b: tuple[float, float]) -> float: 9 + """Calculate the distance between two points using the Euclidean distance formula.""" 10 + return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2) 11 + 12 + 13 + def triangleArea(a: float, b: float, c: float) -> float: 14 + """Use herons formula: `sp = (a+b+c)/2` & `sqrt(sp(sp-a)(sp-b)(sp-c))` to calculate the area of a triangle using it's side lengths.""" 15 + semiperimeter = 0.5 * (a + b + c) 16 + return sqrt( 17 + semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c) 18 + ) 19 + 20 + 21 + def intersect(m1: float, b1: float, m2: float, b2: float) -> tuple[float, float]: 22 + """Calculate the intersection point of two lines.""" 23 + x = (b2 - b1) / (m1 - m2) 24 + y = m1 * x + b1 25 + return x, y 26 + 27 + 28 + def areaWithinThreeLines( 29 + m1: float, b1: float, m2: float, b2: float, m3: float, b3: float 30 + ) -> float | None: 31 + """Calculate the area of a triangle formed by three lines.""" 32 + if m1 == m2 or m2 == m3 or m3 == m1: 33 + return None 34 + a = intersect(m1, b1, m2, b2) 35 + b = intersect(m2, b2, m3, b3) 36 + c = intersect(m3, b3, m1, b1) 37 + return triangleArea(distance(a, b), distance(b, c), distance(c, a)) 38 + 39 + 40 + print("Testing areaWithinThreeLines()...", end="") 41 + assert almostEqual(areaWithinThreeLines(0, 7, 1, 0, -1, 2), 36) 42 + assert almostEqual(areaWithinThreeLines(1, -5, 0, -2, 2, 2), 25) 43 + assert almostEqual(areaWithinThreeLines(0, -9.75, -6, 2.25, 1, -4.75), 21) 44 + assert almostEqual(areaWithinThreeLines(1, -5, 0, -2, 2, 25), 272.25) 45 + assert almostEqual(areaWithinThreeLines(1, 2, 3, 4, 5, 6), 0) 46 + 47 + # The first two lines are parallel: 48 + assert areaWithinThreeLines(1, 2, 1, 4, 0, 7) == None 49 + 50 + # Here, the second and third lines are parallel 51 + assert areaWithinThreeLines(5, 2, 1, 4, 1, 6) == None 52 + print("Passed!")
+24
python/oct1/level3/dayOfWeek.py
··· 1 + def dayOfWeek(month: int, day: int, year: int) -> int: 2 + """Return the day of the week for a given date.""" 3 + m = month if month > 2 else month + 12 4 + y = year - 1 if month <= 2 else year 5 + n = (day + 2 * m + (3 * (m + 1)) // 5 + y + y // 4 - y // 100 + y // 400 + 2) % 7 6 + return n if n != 0 else 7 7 + 8 + 9 + print("Testing dayOfWeek()...") 10 + # On 6/15/1215, the Magna Carta was signed on a Monday! 11 + assert dayOfWeek(6, 15, 1215) == 2 12 + # On 3/11/1952, the author Douglas Adams was born on a Tuesday! 13 + assert dayOfWeek(3, 11, 1952) == 3 14 + # on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday! 15 + assert dayOfWeek(4, 12, 1961) == 4 16 + # On 7/4/1776, the Declaration of Independence was signed on a Thursday! 17 + assert dayOfWeek(7, 4, 1776) == 5 18 + # on 1/2/1920, Isaac Asimov was born on a Friday! 19 + assert dayOfWeek(1, 2, 1920) == 6 20 + # on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)! 21 + assert dayOfWeek(10, 11, 1975) == 7 22 + # On 2/5/2006, the Steelers won Super Bowl XL on a Sunday! 23 + assert dayOfWeek(2, 5, 2006) == 1 24 + print("Passed!")