نقل مخزون Lua
الدرس 34 من 35 في دورة Luau لمطوري Lua على Coddy.
التحدي
متوسطقم بنقل برنامج Lua المرجعي إلى وحدات Luau. يتكون كل سطر مدخل من اسم بأحرف ASCII صغيرة، ونقطتين رأسيتين، وفرق عددي صحيح يحمل إشارة. قم بالتجميع حسب الاسم، واحتفظ فقط بالمجاميع النهائية الموجبة، ورتب الأسماء تصاعدياً وأرجع مدخلات name=total مفصولة بفواصل. أرجع empty عندما لا يتبقى شيء. قم بتنفيذ inventory.aggregate(lines) في inventory.luau؛ ويقوم app بتنسيق النتيجة. جميع أسطر المدخلات صالحة ومشكلة بشكل صحيح.
مرجع Lua 5.1
-- Lua 5.1 reference with explicit module registration.
package.preload["app"]=function()
local M = {}
function M.run(lines)
local inventory=require("inventory")
local counts=inventory.aggregate(lines)
local names={}
for name,n in pairs(counts) do if n>0 then names[#names+1]=name end end
table.sort(names)
local out={}
for _,name in ipairs(names) do out[#out+1]=name .. "=" .. counts[name] end
return #out>0 and table.concat(out,",") or "empty"
end
return M
end
package.preload["inventory"]=function()
local M={}
function M.aggregate(lines)
local counts={}
for _,s in ipairs(lines) do
local name,n=s:match("^([a-z]+):(%-?%d+)$")
counts[name]=(counts[name] or 0)+tonumber(n)
end
return counts
end
return M
end
-- 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))قم بتعديل app.luau، وinventory.luau. يقرأ ملف main.luau المغلق والمرئي الإدخال القياسي (stdin) في تسلسل من الأسطر، ويستدعي app.run(lines)، ويطبع قيمته المرجعة مع سطر جديد نهائي واحد. لا تطبع تشخيصات ولا تغير أسماء الوحدات. يعطي الإدخال الفارغ تسلسلاً فارغاً. جميع المدخلات الرقمية صغيرة بما يكفي لتجنب التجاوز.
فحوصات API العامة: يرجع inventory.aggregate(lines) جدولاً جديداً من الفروق المجمعة ذات الإشارة، مع الاحتفاظ بالمجاميع الصفريّة. ينتج عن الإدخال الفارغ جدول فارغ. يتم التنسيق والتصفية في app. يتأكد المشغل (driver) المغلق من هذا السلوك قبل استدعاء app.run. لا تطبع هذه الفحوصات أي شيء عند نجاحها.
جرّب بنفسك
-- فحوصات API العامة
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))
جميع دروس Luau لمطوري Lua
4الدوّال والإغلاقات
الدوّال وقيم الإرجاعالقيم الافتراضية وجداول الخياراتالنتائج المتعددة و Varargsدوال Callbacks والحالة الملتقطةمراجعة: دوّال التسعير7الكائنات والحالة المملوكة
الجداول كنسخself واستدعاءات النقطتين الراسيتينجداول النسخ المستقلةمراجعة: كائنات الحساب6الترتيب، الوحدات، والأخطاء
من الاستيراد إلى requireمن الاستثناءات إلى pcallمراجعة: التحقق من صحة دفعةتدرّب بنفسك: مترجم Luau عبر الإنترنت