From ed475f6230f2986066e297cf9890842afd902fda Mon Sep 17 00:00:00 2001 From: Samuel Shuert Date: Wed, 1 Oct 2025 15:07:49 +0000 Subject: [PATCH] feat: oct 1 level 3 --- python/oct1/level3/areaWithinThreeLines.py | 52 ++++++++++++++++++++++ python/oct1/level3/dayOfWeek.py | 24 ++++++++++ 2 files changed, 76 insertions(+) create mode 100644 python/oct1/level3/areaWithinThreeLines.py create mode 100644 python/oct1/level3/dayOfWeek.py diff --git a/python/oct1/level3/areaWithinThreeLines.py b/python/oct1/level3/areaWithinThreeLines.py new file mode 100644 index 0000000..0926a4b --- /dev/null +++ b/python/oct1/level3/areaWithinThreeLines.py @@ -0,0 +1,52 @@ +from math import sqrt + + +def almostEqual(x, y): + return abs(x - y) < 10**-9 + + +def distance(a: tuple[float, float], b: tuple[float, float]) -> float: + """Calculate the distance between two points using the Euclidean distance formula.""" + return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2) + + +def triangleArea(a: float, b: float, c: float) -> float: + """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.""" + semiperimeter = 0.5 * (a + b + c) + return sqrt( + semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c) + ) + + +def intersect(m1: float, b1: float, m2: float, b2: float) -> tuple[float, float]: + """Calculate the intersection point of two lines.""" + x = (b2 - b1) / (m1 - m2) + y = m1 * x + b1 + return x, y + + +def areaWithinThreeLines( + m1: float, b1: float, m2: float, b2: float, m3: float, b3: float +) -> float | None: + """Calculate the area of a triangle formed by three lines.""" + if m1 == m2 or m2 == m3 or m3 == m1: + return None + a = intersect(m1, b1, m2, b2) + b = intersect(m2, b2, m3, b3) + c = intersect(m3, b3, m1, b1) + return triangleArea(distance(a, b), distance(b, c), distance(c, a)) + + +print("Testing areaWithinThreeLines()...", end="") +assert almostEqual(areaWithinThreeLines(0, 7, 1, 0, -1, 2), 36) +assert almostEqual(areaWithinThreeLines(1, -5, 0, -2, 2, 2), 25) +assert almostEqual(areaWithinThreeLines(0, -9.75, -6, 2.25, 1, -4.75), 21) +assert almostEqual(areaWithinThreeLines(1, -5, 0, -2, 2, 25), 272.25) +assert almostEqual(areaWithinThreeLines(1, 2, 3, 4, 5, 6), 0) + +# The first two lines are parallel: +assert areaWithinThreeLines(1, 2, 1, 4, 0, 7) == None + +# Here, the second and third lines are parallel +assert areaWithinThreeLines(5, 2, 1, 4, 1, 6) == None +print("Passed!") diff --git a/python/oct1/level3/dayOfWeek.py b/python/oct1/level3/dayOfWeek.py new file mode 100644 index 0000000..a5b509b --- /dev/null +++ b/python/oct1/level3/dayOfWeek.py @@ -0,0 +1,24 @@ +def dayOfWeek(month: int, day: int, year: int) -> int: + """Return the day of the week for a given date.""" + m = month if month > 2 else month + 12 + y = year - 1 if month <= 2 else year + n = (day + 2 * m + (3 * (m + 1)) // 5 + y + y // 4 - y // 100 + y // 400 + 2) % 7 + return n if n != 0 else 7 + + +print("Testing dayOfWeek()...") +# On 6/15/1215, the Magna Carta was signed on a Monday! +assert dayOfWeek(6, 15, 1215) == 2 +# On 3/11/1952, the author Douglas Adams was born on a Tuesday! +assert dayOfWeek(3, 11, 1952) == 3 +# on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday! +assert dayOfWeek(4, 12, 1961) == 4 +# On 7/4/1776, the Declaration of Independence was signed on a Thursday! +assert dayOfWeek(7, 4, 1776) == 5 +# on 1/2/1920, Isaac Asimov was born on a Friday! +assert dayOfWeek(1, 2, 1920) == 6 +# on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)! +assert dayOfWeek(10, 11, 1975) == 7 +# On 2/5/2006, the Steelers won Super Bowl XL on a Sunday! +assert dayOfWeek(2, 5, 2006) == 1 +print("Passed!") -- 2.51.2