Subtracting Inventories with Counts

A multiset keeps the number of occurrences of each key. A count-based difference can show unmet quantity even when a key is available.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace the shortage
  2. Choose signed output if needed
  3. Compute quantities before dropping exhausted keys
  4. Choose whether zero counts are stored or omitted

Trace the shortage

A request for three pens and one pad cannot be filled from two pens and two pads. Key sets for both inventories match. Subtracting available counts gives a shortage of one pen when negative shortages are clamped to zero. See subset constraints.

Choose signed output if needed

A signed difference can express the extra pad as negative one while showing the pen shortage as positive one. Define which sign and zero convention the caller expects. Counters by key provide the stored quantities for both rules.[1]

Compute quantities before dropping exhausted keys

If inventory has five bolts and two nuts while a withdrawal requests three bolts and four nuts, remaining bolts equal two. The nut request exceeds available stock. A mathematical bag difference truncated at zero leaves no nuts, while an inventory transaction may reject the whole request or record a shortage of two. State which semantics the operation promises. See set union, intersection and difference.

Choose whether zero counts are stored or omitted

A bag mapping can omit keys whose count reached zero, with reads treating missing keys as zero. An audit may retain explicit zero to show a previously tracked item. Validate that incoming adjustments obey the allowed signs. Test exact withdrawal, overdraw and a request for an unknown item. When orders affect several items, verify whether failure on the second should keep the first unchanged. See dictionary frequency counts.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩