"""Exercise 9.1: answer key for the ten constructed worked solutions, and a scorer for your record. Each problem has a worked solution that states a final answer. Some solutions contain an error. This script recomputes every answer from the problem statement, compares it with the answer the worked solution states, and prints which solutions are correct. Fill in RECORD below with your own judgments (True = "the solution is correct") and confidences (1-5) to get your accuracy per round. To keep the demonstration valid, the answer key is printed only after both rounds are completely filled in (or when the script is run with --show-key). Only the Python standard library is used. """ import sys from fractions import Fraction from itertools import product def p1(): # +20% then -20%, final price as a fraction of the original return Fraction(12, 10) * Fraction(8, 10) def p2(): # opening balance 500, income 120 and 80, spending 45, 190, 35 return 500 + (120 + 80) - (45 + 190 + 35) def p3(): # two fair coins, probability of at least one head outcomes = list(product("HT", repeat=2)) return Fraction(sum(1 for o in outcomes if "H" in o), len(outcomes)) def p4(): # 3(x - 2) = 2x + 5 -> x = 11; check by substitution x = 11 assert 3 * (x - 2) == 2 * x + 5 return x def p5(): # 40 students, 45% boys, half of the girls wear glasses girls = 40 * Fraction(55, 100) return girls / 2 def p6(): # arithmetic sequence, first term 3, difference 4, 10th term return 3 + (10 - 1) * 4 def p7(): # 1000 at 5% for a year, then principal plus interest for another year return Fraction(1000) * Fraction(105, 100) ** 2 def p8(): # 60 km/h for 1.5 h, then 80 km/h for 0.5 h: average speed distance = 60 * Fraction(3, 2) + 80 * Fraction(1, 2) return distance / 2 def p9(): # rectangle, perimeter 30, length = width + 3: area for w in range(1, 15): l = w + 3 if 2 * (l + w) == 30: return l * w def p10(): # integers 1..100 divisible by 3 or 5 return sum(1 for n in range(1, 101) if n % 3 == 0 or n % 5 == 0) # the final answer each worked solution states stated = {1: Fraction(96, 100), 2: 420, 3: Fraction(2, 3), 4: 11, 5: 11, 6: 43, 7: Fraction(11025, 10), 8: 70, 9: 54, 10: 53} truth = {1: p1(), 2: p2(), 3: p3(), 4: p4(), 5: p5(), 6: p6(), 7: p7(), 8: p8(), 9: p9(), 10: p10()} key = {i: truth[i] == stated[i] for i in range(1, 11)} # Your record: for each problem, (judged correct?, confidence 1-5). None = not filled in yet. RECORD = { "round 1 (1 min each)": {i: None for i in range(1, 11)}, "round 2 (5 min each)": {i: None for i in range(1, 11)}, } for name, rec in RECORD.items(): filled = {i: v for i, v in rec.items() if v is not None} if not filled: print(f"{name}: not filled in") continue hits = sum(1 for i, (judged, _) in filled.items() if judged == key[i]) conf = sum(c for _, c in filled.values()) / len(filled) conf_wrong = [c for i, (judged, c) in filled.items() if judged != key[i]] print(f"{name}: accuracy {hits}/{len(filled)} = {hits/len(filled):.0%}, mean confidence {conf:.2f}, " f"mean confidence on your mistakes {sum(conf_wrong)/len(conf_wrong) if conf_wrong else float('nan'):.2f}") all_filled = all(v is not None for rec in RECORD.values() for v in rec.values()) if all_filled or "--show-key" in sys.argv: for i in range(1, 11): print(f"problem {i:>2}: correct answer = {truth[i]!s:>8} ({float(truth[i]):g}), " f"solution states {stated[i]!s:>8} -> solution is {'CORRECT' if key[i] else 'WRONG'}") print("correct solutions:", [i for i in key if key[i]], " wrong solutions:", [i for i in key if not key[i]]) else: print("answer key hidden until both rounds are filled in (run with --show-key to see it anyway)")