# prize35-3 Exercise 2: remove each species and see who loses all their food web = { "algae": [], "water plants": [], "snails": ["algae", "water plants"], "water fleas": ["algae"], "minnows": ["water fleas", "snails"], "trout": ["minnows", "snails", "water fleas"], "otter": ["trout", "minnows"], "heron": ["minnows", "trout"], } def lose(web, removed): gone = {removed} changed = True while changed: changed = False for sp, diet in web.items(): if sp in gone or not diet: continue if all(p in gone for p in diet): gone.add(sp) changed = True return gone - {removed} results = {sp: lose(web, sp) for sp in web} for sp, lost in results.items(): print(f"remove {sp:13s} -> also lost: {sorted(lost) or 'nobody'}") worst = max(results, key=lambda s: len(results[s])) print("most damaging removal:", worst, "with", len(results[worst]), "secondary losses") # Output: # remove algae -> also lost: ['water fleas'] # remove water plants -> also lost: nobody # remove snails -> also lost: nobody # remove water fleas -> also lost: nobody # remove minnows -> also lost: nobody # remove trout -> also lost: nobody # remove otter -> also lost: nobody # remove heron -> also lost: nobody # most damaging removal: algae with 1 secondary losses # Reading it: only algae matter in this test, and only because water fleas eat nothing else. Every # other species has more than one food source, so none is wiped out. The test only sees bottom-up # loss of food; it cannot see top-down effects, which is why a keystone predator can be # important even when its removal "loses" nobody here (see exercise 3).