Find the units missing from a delivery

Write codeEasy10 min

Your task

A warehouse checks each delivery against its purchase order. Codes can repeat on both lists, and quantities matter.

Write missing_items(ordered, received) that returns every unit that was ordered but not received.

Input and output:

  • ordered: the item codes on the order, one entry per unit, a list of 0 to 100,000 integers from 1 to 1,000,000,000.
  • received: the item codes delivered, one entry per unit, a list of the same kind.
  • Returns a list of codes in increasing numeric order, one entry per missing unit.

Rules:

  • Each received unit covers one ordered unit with the same code.
  • If a code was ordered x times and received y times, it appears x - y times in the result when x is more than y, and not at all otherwise.
  • Received units that were never ordered, or that arrived beyond the ordered quantity, are ignored.
  • If nothing is missing, return an empty list.

Examples

missing_items(ordered: list[int], received: list[int]) -> list[int]
Example 1
Inputmissing_items([4, 4, 7, 9], [4, 9])
Output[4, 7]
Example 2
Inputmissing_items([3, 1, 2, 3], [2, 8])
Output[1, 3, 3]