""" Ex. 3.28 from "A Primer on...". Write functions min and max, to find the minimum and maximum value in a list. The exercise does not ask us to write test functions, but these are added examples. """ def max(a): max_elem = a[0] for e in a[1:]: if e > max_elem: max_elem = e return max_elem def min(a): min_elem = a[0] for e in a[1:]: if e < min_elem: min_elem = e return min_elem def test_max(): l = [1,3,5,2,1,-1,-2] expected = 5 computed = max(l) success = expected == computed msg = f'Expected {expected}, got {computed}' assert success, msg def test_min(): l = [1,3,5,2,1,-1,-2] expected = -2 computed = min(l) success = expected == computed msg = f'Expected {expected}, got {computed}' assert success, msg """ The example below shows how to use pytest to run both tests automatically, but you can also insert calls to test_min() and test_max() in this file and run it in the usual way. """ """ Terminal> pytest maxmin_list.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_9sep plugins: anyio-2.2.0, mock-3.10.0, cov-3.0.0 collected 2 items maxmin_list.py .. [100%] ====================================== 2 passed in 0.01s ======================================= """