# How Lighthouses Work 5, Exercise 1: classify a light from timed observations # Each observation is a list of (state, seconds) for one full repeat of the pattern: L = light, D = dark. def classify(segs): period = sum(d for _, d in segs) lights = [d for s, d in segs if s == "L"] darks = [d for s, d in segs if s == "D"] n = len(lights) if len(set(lights)) > 1: kind = "Mo" # two clearly different durations of light elif n == 1: light, dark = lights[0], darks[0] if light == dark: kind = "Iso" elif light > dark: kind = "Oc" elif light >= 2: kind = "LFl" else: kind = "Fl" else: kind = "Fl(%d)" % n return kind, period OBS = { "A": [("L", 1), ("D", 4)], "B": [("L", 3), ("D", 1)], "C": [("L", 2), ("D", 2)], "D": [("L", 1), ("D", 1), ("L", 1), ("D", 1), ("L", 1), ("D", 6)], "E": [("L", 1), ("D", 1), ("L", 3), ("D", 7)], "F": [("L", 3), ("D", 9)], } for name, segs in OBS.items(): kind, period = classify(segs) light_share = sum(d for s, d in segs if s == "L") / period print(name, kind, period, "s", round(light_share, 2)) # Check against the rule for a single flash (darkness at least 3 times the flash) flash, dark = 1, 4 print("A dark/flash ratio:", dark / flash, "ok" if dark >= 3 * flash else "too short") # Output: # A Fl 5 s 0.2 # B Oc 4 s 0.75 # C Iso 4 s 0.5 # D Fl(3) 11 s 0.27 # E Mo 12 s 0.33 # F LFl 12 s 0.25 # A dark/flash ratio: 4.0 ok # # The classifier uses the definitions in the Wikipedia article on light characteristics: equal light and dark # is isophase, light longer than dark is occulting, a single light of 2 seconds or more is long flashing, several # lights per period is a group flash, and two clearly different durations of light is Morse. A is a plain flash # (Fl) every 5 s with 4 s of darkness, four times the flash, so it passes the "at least three times" rule. B is # occulting (3 s of light, 1 s dark), C is isophase (2 s and 2 s), D is a group of three flashes every 11 s, # E is Morse (a 1 s and a 3 s light, the pattern of the letter A) and F is a long flash. The classifier is crude: # it ignores colour, sectors, the quick bands and the rules on darkness inside a group, and the timings are # illustrative values chosen for this exercise, not those of any real light.