Chuyển đổi các đối tượng phòng Lua
Bài học 35 trên 35 trong khóa học Luau cho lập trình viên Lua của Coddy.
Thử thách
Trung bìnhChuyển mã tham chiếu Lua. Dòng đầu vào đầu tiên là sức chứa phòng không âm; các dòng còn lại là các yêu cầu đặt chỗ kiểu số nguyên (integer) có dấu. Chỉ chấp nhận các yêu cầu là số dương vừa đủ chỗ. Yêu cầu thất bại sẽ giữ nguyên số chỗ đã dùng. Trả về remaining:accepted. Cài đặt Model.new(capacity) và Model:reserve(seats) trong model.luau; reserve trả về giá trị boolean. Sử dụng một đối tượng (object) mới cho mỗi lần chạy.
Tham chiếu 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))Chỉnh sửa app.luau, model.luau. Tệp main.luau bị khóa đang hiển thị sẽ đọc stdin thành một dãy các dòng, gọi app.run(lines) và in giá trị trả về của nó cùng một dòng mới ở cuối. Không in thông tin chẩn đoán hay thay đổi tên module. Đầu vào rỗng sẽ cho ra một dãy rỗng. Tất cả đầu vào số đều đủ nhỏ để tránh bị tràn số.
Kiểm tra API công khai: Model.new(capacity) cung cấp capacity và used, ban đầu bằng 0. reserve trả về giá trị boolean và chỉ cập nhật used đối với yêu cầu số dương vừa đủ chỗ. Các phòng hoạt động độc lập với nhau. Trình chạy bị khóa sẽ kiểm tra hành vi này trước khi gọi app.run. Các kiểm tra này không in ra gì khi thành công.
Tự làm thử
-- Kiểm tra API public
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))
Tất cả bài học trong Luau cho lập trình viên Lua
4Hàm và closure
Hàm và giá trị trả vềGiá trị mặc định và bảng tùy chọnNhiều kết quả và varargsCallback và trạng thái lưu giữÔn tập: Hàm tính giá7Đối tượng và trạng thái sở hữu
Bảng là instanceself và lời gọi bằng dấu hai chấmBảng instance độc lậpÔn tập: Đối tượng tài khoản5Bảng dưới dạng tập hợp
Danh sách dưới dạng bảng đặcTừ điển và pairsBí danh và bản sao nôngÔn tập: Đếm kho hàng8Kết hợp và tìm kiếm đối tượng
Kết hợp và ủy quyềnChuỗi tìm kiếm prototypeĐa hình bằng protocolMetamethod so sánh bằng giá trịÔn tập: Báo cáo dispatchTự luyện tập: Trình biên dịch Luau online