Menu
CoddyTech

Port a C++ Inventory

Lesson 47 of 48 in Coddy's Lua for C++ Developers course.

challenge icon

Challenge

Medium

Port the reference C++ program into Lua modules. Each input line is a lowercase ASCII name, a colon, and a signed integer delta. Aggregate by name, retain only positive final totals, sort names ascending and return name=total entries joined by commas. Return empty when none remain. Implement inventory.aggregate(lines) in inventory.lua; app formats the result. All input lines are well-formed.

Edit app.lua, inventory.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.

Public API checks: inventory.aggregate(lines) returns a fresh table of summed signed deltas, retaining zero totals. Empty input yields an empty table. Formatting and filtering happen in app. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.

C++17 reference

#include <iostream>
#include <map>
#include <string>
// Input: lowercase ASCII name, colon, decimal delta with optional minus sign.
// Values and totals are small enough to be represented exactly in Lua 5.1.
std::map<std::string, long long> aggregate(std::istream& input) {
    std::map<std::string, long long> counts;
    std::string line;
    while (std::getline(input, line)) {
        const auto colon = line.find(':');
        counts[line.substr(0, colon)] += std::stoll(line.substr(colon + 1));
    }
    return counts;
}
int main() {
    const auto counts = aggregate(std::cin);
    bool first = true;
    for (const auto& entry : counts) {
        if (entry.second <= 0) continue;
        if (!first) std::cout << ',';
        std::cout << entry.first << '=' << entry.second;
        first = false;
    }
    if (first) std::cout << "empty";
    std::cout << '\n';
}

The C++ map inserts a zero count on first access and iterates in key order. In Lua, distinguish a missing key from a stored zero, accumulate signed deltas, then sort the report keys explicitly. A signed integer here has optional - followed by digits; no plus sign is supplied. Keep aggregation separate from filtering so the public module still returns zero and negative totals.

Try it yourself

-- Public API checks
do
local inventory=require("inventory")
local counts=inventory.aggregate({"pen:3","pen:-1","book:0"})
assert(counts.pen==2 and counts.book==0,"aggregate must sum signed deltas by name")
local empty=inventory.aggregate({})
assert(next(empty)==nil,"aggregate empty input returns an empty table")
assert(counts.pen==2,"aggregation calls retain independent result state")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))

All lessons in Lua for C++ Developers

Practice on your own: Online Lua compiler