""" Exercise 6.9 from "A primer on..." Change the input to a given function from a list to a dictionary, with integer keys 1, 2, 3. Solution: 1. First change the test function to use the new input type, which makes the test fail 2. The change the actual function so that the test passes. """ def triangle_area(v): [x1,y1] = v[1]; [x2,y2] = v[2]; [x3,y3] = v[3] #or, more compact version: #[x1,y1], [x2,y2], [x3,y3] = v A = abs(0.5*((x2*y3)-(x3*y2)-(x1*y3)+(x3*y1)+(x1*y2)-(x2*y1))) return A # Test function from book def test_triangle_area(): """ Verify the area of a triangle with vertex coordinates (0,0), (1,0), and (0,2). """ v1 = (0,0); v2 = (1,0); v3 = (0,2) #vertices = [v1, v2, v3] vertices = {1: (0,0), 2: (1,0), 3: (0,2)} expected = 1 computed = triangle_area(vertices) tol = 1E-14 success = abs(expected - computed) < tol msg = "computed area={computed:g} != {expected:g} (expected)" assert success, msg """ Terminal> pytest area_triangle_dict.py ============================= test session starts ============================== platform darwin -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 rootdir: /Users/sundnes/Desktop/IN1900_13_okt plugins: anyio-2.2.0, mock-3.10.0, cov-3.0.0 collected 1 item area_triangle_dict.py . [100%] ============================== 1 passed in 0.01s =============================== """