Prize37 Chapter 8, Exercise 2 Ranking restoration jobs Task A trust has 600 thousand pounds for a fictional disused canal. The jobs are A (cost 200, benefit 8), B (cost 500, benefit 9), C (cost 100, benefit 3) and D (cost 250, benefit 6). Rank them by benefit per cost, fund them in that order while they fit, and say what is funded and what is left over. Answer Code run: jobs = [("A", 200, 8), ("B", 500, 9), ("C", 100, 3), ("D", 250, 6)] budget = 600 ranked = sorted(jobs, key=lambda j: j[2] / j[1], reverse=True) spent = 0 for n, c, b in ranked: if spent + c <= budget: spent += c print(n, "funded") else: print(n, "waits") print(spent) Output: A funded C funded D funded B waits 550 Benefit per cost: A 0.04, C 0.03, D 0.024, B 0.018, so the order is A, C, D, B. A (200), C (100) and D (250) are funded, 550 in all, leaving 50. B (500) does not fit and waits.