Menu
CoddyTech

انقل مخزونًا مكتوبًا بـ Lua

الدرس 47 من 48 في دورة Python لمطوّري Lua على Coddy.

challenge icon

التحدي

مبتدئ

انقل برنامج Lua الكامل لإدارة المخزون أدناه. نفّذ inventory(lines) في inventory.py. تتلقى الدالة سلاسل name amount سليمة التنسيق، وتُرجع نص الناتج من دون محرف سطر جديد في نهايته. الأسماء أحرف ASCII صغيرة غير فارغة؛ والكميات أعداد صحيحة موقّعة بالنظام العشري. اجمع كل التحديثات لكل اسم، بما في ذلك الإجماليات الوسيطة السالبة. بعد التجميع، استبعد الإجماليات التي تساوي صفرًا أو تقل عنه. أرجع إدخالات name:total الموجبة مرتبة تصاعديًا حسب الاسم، ومفصولة بمحارف سطر جديد، أو empty إذا لم يتبقَّ أي إدخال. لا تقرأ المدخلات ولا تطبع في هذه الدالة. يقرأ driver.py المقفل عددًا غير سالب وهذا العدد من السجلات، ثم يطبع النتيجة مع محرف سطر جديد في النهاية.

يجمع مرجع Lua الأسماء في جدول، ويحصل على ترتيب حتمي باستخدام table.sort، ويستخدم أدوات مساعدة للتعامل مع النصوص العشرية الموقّعة لتجنب حدود النطاق العددي الأصلي ومشكلات الدقة. تستبدل Python تلك الأدوات المساعدة بـ int. يحافظ التكرار على قاموس Python على ترتيب الإدراج، لكن هذه المهمة تتطلب ترتيب الأسماء صراحةً. يجمع كلا الإصدارين القيم قبل التصفية. بالنسبة إلى pen 3 وbook 2 وpen -1، أرجع book:2 متبوعًا بـ pen:2.

-- Decimal text arithmetic avoids Lua integer overflow and float rounding.
local function parse(text)
    local negative = text:sub(1, 1) == "-"
    local digits = text:gsub("^[+-]", ""):gsub("^0+", "")
    if digits == "" then return false, "0" end
    return negative, digits
end
local function compare(a, b)
    if #a ~= #b then return #a < #b and -1 or 1 end
    if a == b then return 0 end
    return a < b and -1 or 1
end
local function add_magnitude(a, b)
    local i, j, carry, reversed = #a, #b, 0, {}
    while i > 0 or j > 0 or carry > 0 do
        local x = i > 0 and tonumber(a:sub(i, i)) or 0
        local y = j > 0 and tonumber(b:sub(j, j)) or 0
        local total = x + y + carry
        reversed[#reversed + 1] = string.char(48 + total % 10)
        carry = math.floor(total / 10)
        i, j = i - 1, j - 1
    end
    return table.concat(reversed):reverse()
end
-- Precondition: a >= b in magnitude.
local function subtract_magnitude(a, b)
    local j, borrow, reversed = #b, 0, {}
    for i = #a, 1, -1 do
        local x = tonumber(a:sub(i, i))
        local y = j > 0 and tonumber(b:sub(j, j)) or 0
        local difference = x - y - borrow
        borrow = difference < 0 and 1 or 0
        if difference < 0 then difference = difference + 10 end
        reversed[#reversed + 1] = string.char(48 + difference)
        j = j - 1
    end
    local result = table.concat(reversed):reverse():gsub("^0+", "")
    return result == "" and "0" or result
end
local function add(left, right)
    local ln, a = parse(left)
    local rn, b = parse(right)
    local negative, result
    if ln == rn then
        negative, result = ln, add_magnitude(a, b)
    elseif compare(a, b) >= 0 then
        negative, result = ln, subtract_magnitude(a, b)
    else
        negative, result = rn, subtract_magnitude(b, a)
    end
    return negative and result ~= "0" and "-" .. result or result
end
local count = assert(tonumber(io.read("*l")))
local stock = {}
for _ = 1, count do
    local name, amount = io.read("*l"):match("^(%l+)%s+([+-]?%d+)$")
    stock[name] = add(stock[name] or "0", amount)
end
local names = {}
for name, total in pairs(stock) do
    if total ~= "0" and total:sub(1, 1) ~= "-" then names[#names + 1] = name end
end
table.sort(names)
local output = {}
for _, name in ipairs(names) do output[#output + 1] = name .. ":" .. stock[name] end
print(#output == 0 and "empty" or table.concat(output, "\n"))

ضع شيفرتك في inventory.py. يتولى driver.py المقفل توفير المدخلات والمخرجات، ولا يمكن تعديله.

جرّب بنفسك

from inventory import inventory

lines = [input() for _ in range(int(input()))]
print(inventory(lines))

جميع دروس Python لمطوّري Lua

تدرّب بنفسك: مترجم Python عبر الإنترنت