"""Exercises 20.1 and 20.2: leap-year rules and tests (constructed teaching example). 20.1 Four candidate rules for "is this year a leap year?" are run on the same three tests. For each rule we print its test results and every year in 1600-2400 where it disagrees with the Gregorian rule, then check which extra test years would expose each wrong rule. 20.2 The reference solution for the experimental route: three tests, a wrong implementation that passes them, and the tests that expose it. Python's calendar.isleap is used as an independent reference implementation. """ import calendar def gregorian(y): # the specification return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0) rules = { "A: divisible by 4": lambda y: y % 4 == 0, "B: divisible by 4 and not by 100": lambda y: y % 4 == 0 and y % 100 != 0, "C: divisible by 400; else divisible by 4 and not by 100": lambda y: ( True if y % 400 == 0 else (y % 4 == 0 and y % 100 != 0)), "D: divisible by 400; else divisible by 4 and not by 10": lambda y: ( True if y % 400 == 0 else (y % 4 == 0 and y % 10 != 0)), } tests = [(2024, True), (2023, False), (2000, True)] years = range(1600, 2401) def wrong_years(f): return [y for y in years if f(y) != gregorian(y)] print("Spec check against calendar.isleap on 1600-2400:", all(gregorian(y) == calendar.isleap(y) for y in years)) print() for name, f in rules.items(): res = ["pass" if f(y) == want else "FAIL" for y, want in tests] wy = wrong_years(f) print(name) print(" tests (2024 leap, 2023 common, 2000 leap):", ", ".join(res)) print(f" wrong in {len(wy)} of {len(years)} years (1600-2400); first ones: {wy[:8]}") if wy: kinds = { "div by 100 not 400": sum(1 for y in wy if y % 100 == 0 and y % 400 != 0), "div by 400": sum(1 for y in wy if y % 400 == 0), "div by 4, not by 100": sum(1 for y in wy if y % 4 == 0 and y % 100 != 0), "other": sum(1 for y in wy if y % 4 != 0), } print(" wrong years by category:", {k: v for k, v in kinds.items() if v}) print() # Which additional test years expose which rules? extra = [1900, 2100, 2020, 1996, 2400] for y in extra: caught = [n[0] for n, f in rules.items() if f(y) != gregorian(y)] print(f"test {y} ({'leap' if gregorian(y) else 'common'}) exposes rules: {caught or 'none'}") print() # One representative per category of the specification: does it catch every wrong rule? category_reps = [2023, 2024, 1900, 2000] for name, f in rules.items(): ok = all(f(y) == gregorian(y) for y in category_reps) print(f"category tests {category_reps} on {name[0]}: {'all pass' if ok else 'caught'}") # Rule D's extra branch "divisible by 10": 1900 goes through it but D still answers correctly. d = rules["D: divisible by 400; else divisible by 4 and not by 10"] print("Rule D on 1900:", d(1900), "spec:", gregorian(1900), "| on 2020:", d(2020), "spec:", gregorian(2020)) print() # ---- Exercise 20.2 reference solution ------------------------------------------------------- def is_leap(year): return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) def is_leap_wrong(year): # passes the three tests, but only by remembering them return year in (2024, 2000) def is_leap_wrong2(year): # passes the three tests, wrong on 1900, 2100, ... return year % 4 == 0 def run(f, cases): return [(y, want, f(y)) for y, want in cases if f(y) != want] three = [(2024, True), (2023, False), (2000, True)] more = [(1900, False), (2100, False), (2020, True), (1996, True), (2400, True), (2023, False)] for f in (is_leap, is_leap_wrong, is_leap_wrong2): print(f.__name__, "fails on first three:", run(f, three), "| fails on added:", run(f, more)) diff = [y for y in years if is_leap(y) != calendar.isleap(y)] print("is_leap vs calendar.isleap, 1600-2400, disagreements:", diff) print("is_leap_wrong vs calendar.isleap, disagreements:", len([y for y in years if is_leap_wrong(y) != calendar.isleap(y)]))