Menu
CoddyTech

نقل كائنات الغرفة في Lua

الدرس 35 من 35 في دورة Luau لمطوري Lua على Coddy.

challenge icon

التحدي

متوسط

قم بنقل مرجع Lua. سطر الإدخال الأول هو سعة الغرفة غير السالبة؛ والأسطر المتبقية هي طلبات حجز بأرقام صحيحة ذات إشارة. اقبل فقط الطلبات الموجبة تمامًا التي تتسع لها الغرفة. الطلبات الفاشلة تترك المقاعد المستخدمة دون تغيير. أرجع remaining:accepted. قم بتنفيذ Model.new(capacity) و Model:reserve(seats) في model.luau؛ حيث ترجع reserve قيمة بولية (boolean). استخدم كائنًا جديدًا لكل تشغيل.

مرجع Lua 5.1

-- Lua 5.1 reference with explicit module registration.
package.preload["app"]=function()
local M = {}
function M.run(lines)
  local Model=require("model")
  local room=Model.new(tonumber(lines[1]))
  local accepted=0
  for i=2,#lines do if room:reserve(tonumber(lines[i])) then accepted=accepted+1 end end
  return (room.capacity-room.used) .. ":" .. accepted
end
return M
end
package.preload["model"]=function()
local Model={}
Model.__index=Model
function Model.new(capacity) return setmetatable({capacity=capacity,used=0},Model) end
function Model:reserve(seats)
  if seats<=0 or self.used+seats>self.capacity then return false end
  self.used=self.used+seats;return true
end
return Model
end
-- Public API checks
do
local Model=require("model")
local a,b=Model.new(5),Model.new(2)
assert(a:reserve(6)==false and a.used==0,"oversized reservation must not mutate")
assert(a:reserve(0)==false and a:reserve(-1)==false and a.used==0,"nonpositive requests are rejected")
assert(a:reserve(2)==true and a.used==2,"accepted seats update used")
assert(a:reserve(3)==true and a.used==5 and a.capacity==5,"exact fit succeeds without changing capacity")
assert(b.used==0 and b.capacity==2,"rooms must be independent")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))

قم بتعديل app.luau و model.luau. يقوم ملف main.luau المرئي والمغلق بقراءة الإدخال القياسي (stdin) إلى تسلسل من الأسطر، واستدعاء app.run(lines)، وطباعة القيمة المرجعة منه مع سطر جديد في النهاية. لا تطبع أي معلومات تشخيصية ولا تغير أسماء الوحدات. الإدخال الفارغ يعطي تسلسلًا فارغًا. جميع المدخلات الرقمية صغيرة بما يكفي لتجنب طفح البيانات (overflow).

فحوصات الـ API العامة: يكشف Model.new(capacity) عن capacity و used، وتكون قيمتهما الأولية صفرًا. ترجع reserve قيمة بولية (boolean) وتحدث used فقط للطلب الموجب الذي يتسع له المكان. الغرف مستقلة عن بعضها. يتحقق البرنامج المشغل المغلق من هذا السلوك قبل استدعاء app.run. لا تطبع هذه الفحوصات أي شيء عند نجاحها.

جرّب بنفسك

-- فحوصات API العامة
do
local Model=require("./model")
local a,b=Model.new(5),Model.new(2)
assert(a:reserve(6)==false and a.used==0,"oversized reservation must not mutate")
assert(a:reserve(0)==false and a:reserve(-1)==false and a.used==0,"nonpositive requests are rejected")
assert(a:reserve(2)==true and a.used==2,"accepted seats update used")
assert(a:reserve(3)==true and a.used==5 and a.capacity==5,"exact fit succeeds without changing capacity")
assert(b.used==0 and b.capacity==2,"rooms must be independent")
end
local app = require("./app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))

جميع دروس Luau لمطوري Lua

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