···11+from math import sqrt
22+33+44+def almostEqual(x, y):
55+ return abs(x - y) < 10**-9
66+77+88+def distance(a: tuple[float, float], b: tuple[float, float]) -> float:
99+ """Calculate the distance between two points using the Euclidean distance formula."""
1010+ return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
1111+1212+1313+def triangleArea(a: float, b: float, c: float) -> float:
1414+ """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."""
1515+ semiperimeter = 0.5 * (a + b + c)
1616+ return sqrt(
1717+ semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c)
1818+ )
1919+2020+2121+def intersect(m1: float, b1: float, m2: float, b2: float) -> tuple[float, float]:
2222+ """Calculate the intersection point of two lines."""
2323+ x = (b2 - b1) / (m1 - m2)
2424+ y = m1 * x + b1
2525+ return x, y
2626+2727+2828+def areaWithinThreeLines(
2929+ m1: float, b1: float, m2: float, b2: float, m3: float, b3: float
3030+) -> float | None:
3131+ """Calculate the area of a triangle formed by three lines."""
3232+ if m1 == m2 or m2 == m3 or m3 == m1:
3333+ return None
3434+ a = intersect(m1, b1, m2, b2)
3535+ b = intersect(m2, b2, m3, b3)
3636+ c = intersect(m3, b3, m1, b1)
3737+ return triangleArea(distance(a, b), distance(b, c), distance(c, a))
3838+3939+4040+print("Testing areaWithinThreeLines()...", end="")
4141+assert almostEqual(areaWithinThreeLines(0, 7, 1, 0, -1, 2), 36)
4242+assert almostEqual(areaWithinThreeLines(1, -5, 0, -2, 2, 2), 25)
4343+assert almostEqual(areaWithinThreeLines(0, -9.75, -6, 2.25, 1, -4.75), 21)
4444+assert almostEqual(areaWithinThreeLines(1, -5, 0, -2, 2, 25), 272.25)
4545+assert almostEqual(areaWithinThreeLines(1, 2, 3, 4, 5, 6), 0)
4646+4747+# The first two lines are parallel:
4848+assert areaWithinThreeLines(1, 2, 1, 4, 0, 7) == None
4949+5050+# Here, the second and third lines are parallel
5151+assert areaWithinThreeLines(5, 2, 1, 4, 1, 6) == None
5252+print("Passed!")
+24
python/oct1/level3/dayOfWeek.py
···11+def dayOfWeek(month: int, day: int, year: int) -> int:
22+ """Return the day of the week for a given date."""
33+ m = month if month > 2 else month + 12
44+ y = year - 1 if month <= 2 else year
55+ n = (day + 2 * m + (3 * (m + 1)) // 5 + y + y // 4 - y // 100 + y // 400 + 2) % 7
66+ return n if n != 0 else 7
77+88+99+print("Testing dayOfWeek()...")
1010+# On 6/15/1215, the Magna Carta was signed on a Monday!
1111+assert dayOfWeek(6, 15, 1215) == 2
1212+# On 3/11/1952, the author Douglas Adams was born on a Tuesday!
1313+assert dayOfWeek(3, 11, 1952) == 3
1414+# on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday!
1515+assert dayOfWeek(4, 12, 1961) == 4
1616+# On 7/4/1776, the Declaration of Independence was signed on a Thursday!
1717+assert dayOfWeek(7, 4, 1776) == 5
1818+# on 1/2/1920, Isaac Asimov was born on a Friday!
1919+assert dayOfWeek(1, 2, 1920) == 6
2020+# on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)!
2121+assert dayOfWeek(10, 11, 1975) == 7
2222+# On 2/5/2006, the Steelers won Super Bowl XL on a Sunday!
2323+assert dayOfWeek(2, 5, 2006) == 1
2424+print("Passed!")