Debugging and testing · 10 / 14 Exercise Medium

Hunt the bug

Objective

This function has a hidden bug. Create a program that finds it by comparing its result with the correct one in several cases.

Instructions

  • Copy this function exactly as it is (even though it has the bug): def maximum(items): with largest = 0, a loop for n in items: and, inside, if n > largest: largest = n; at the end return largest.
  • Read an integer n and then n lines, each with integers separated by spaces (at least one).
  • For each line, compare maximum(items) with the correct result, max(items). Show Line K: OK if they match or Line K: FAIL (expected E, got G) otherwise. K starts at 1.
  • At the end show Failures: F.
Input
3
3 8 1
-5 -2 -9
0 0
Output
Line 1: OK
Line 2: FAIL (expected -2, got 0)
Line 3: OK
Failures: 1

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise