G-XR8P2XJ088

AI-Written Python: Keep Shortages in the Stock Report

An independent print shop counts stock at closing time. The stock file says there should be 12 postcards and eight notebooks. The team finds 10 postcards and 10 notebooks. An AI-written script compares the two sets of figures and reports two extra notebooks. It never mentions the missing postcards. The report is tidy and the calculation has run without errors. It is also missing half the story.

The problem can be as small as a minus sign. Python’s Counter is a useful way to store counts by item, but its subtraction operator and its subtract() method return different kinds of information. One is good for asking what remains. The other can preserve a signed difference. If your report is supposed to help someone investigate discrepancies, that distinction decides what the person at the stockroom door gets to see.

Decide what the report needs to say

In the example, take the observed count and subtract the expected count for each item. A positive number means there is more than expected; a negative number means there is less. Zero means the two counts agree. Under that convention the report needs to say postcards −2 and notebooks +2. If the team uses expected minus observed instead, both signs reverse. Either convention can work if the report labels it and everyone reads it the same way.

Keep the counts as well as the difference when handing the report to someone else. A discrepancy of two on a line expected to contain two items is different in practice from a discrepancy of two on a line expected to contain 200. The difference tells you where to look. The pair of counts gives you context for deciding how to follow up. Neither tells you why the discrepancy happened.

Read the operation before trusting the output

In the Python 3.14 documentation for collections.Counter, the subtraction operator is described as a multiset operation: it keeps only results above zero. So if observed and expected are counters for the example above, observed - expected contains the two extra notebooks. The postcard entry drops out because its result is negative. This behaviour is useful for a list of surplus stock. It makes the same expression unsuitable for a report that promises every discrepancy.

For signed differences, make a copy of the observed counter with delta = observed.copy(), then call delta.subtract(expected). The method changes the counter it is called on and keeps zero and negative results. Copying first preserves the observed counts for display or later checks. Use the updated delta in the next step; the call itself does not return a new result counter.

Make sure the export does not quietly discard the differences again. For instance, the documented elements() method ignores counts below one, so it cannot carry shortages to the CSV. Use the item and its signed count when building each row. Our guide to choosing the columns for an AI-written CSV export covers a related check: what the script holds internally and what the reader receives are separate things.

Distinguish absent data from a real zero

Counters return zero for a missing key when you look it up with square brackets. That can be convenient when you are counting individual scanned items. It needs more care if the observed file is supposed to include one row per product. A missing row may mean the shelf was skipped or a file failed to import. In that case, calculating a shortage assumes the very thing you need to check: that someone actually found zero.

Write down what an absent item means for each input. If the observed file comes from a complete scan of a defined shelf, an expected item with no scans might reasonably be shown as a shortage. If the count sheet is incomplete, flag the missing observation and keep it out of the signed comparison until someone checks it. Similarly, a new item in the observed file may need review before you treat it as a surplus against zero. An unrecognised product code and an extra item can look identical if the report treats them both as arithmetic.

Decide what one row represents before you compare counts. If the studio keeps stock at two sites, comparing only product codes could cancel out a shortage at one site and a surplus at the other. A product-and-site pair can be a useful key when the team needs to investigate each location. If an item was transferred during the count, the report also needs a shared cut-off for the two files. The most accurate subtraction in the world cannot reconcile snapshots from different points in the day.

Test a report that could otherwise look clean

  • Include one shortage, one surplus and one item that matches. Check the signed values in the CSV or screen your team actually reads.
  • Use a pair of differences that cancel out, such as −2 and +2. A net difference of zero is consistent with two lines needing attention.
  • Include a missing observation and a recorded zero. Require different outputs if the source says these situations mean different things.

Ask the assistant to state the convention alongside the proposed code and to show the rows it expects from this sample. Then inspect the finished report yourself. A surplus-only list may be perfectly useful for transferring spare stock, provided that is what the heading promises. A reconciliation report should support a different decision: whether each count agrees, differs, or still needs checking. The report is ready when it lets the next person tell those three situations apart.

Source

Leave a Reply

Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe now to keep reading and get access to the full archive.

Continue reading