Count failures against the specification

An inventory function restock_needed(stock, target) should return how many items must be added to make stock at least the target. Both inputs are nonnegative integers. If stock already meets or exceeds the target, the required addition is zero.


A student writes:


Python
def restock_needed(stock: int, target: int) -> int:
    return abs(target - stock)


The built-in abs turns a negative difference into its positive magnitude. Count a test failure whenever the returned value differs from the value required by the specification.


The proposed tests use (stock, target) values (2, 7), (7, 7), (9, 4) and (0, 3). How many tests fail? Give an exact integer count.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More The Design Recipe Questions: