Ruby-Raumobjekte portieren
Lektion 48 von 48 im Kurs Luau für Ruby-Entwickler von Coddy.
Aufgabe
MittelPortiere il riferimento Ruby. La prima riga di input indica la capacità non negativa della sala; le righe successive contengono richieste di prenotazione intere con segno. Accetta solo le richieste strettamente positive che rientrano nella capacità. Le richieste non accettate non modificano i posti occupati. Restituisci remaining:accepted. Implementa Model.new(capacity) e Model:reserve(seats) in model.luau; reserve restituisce un booleano. Usa un oggetto nuovo a ogni esecuzione.
Riferimento Ruby
class Room
def initialize(capacity)
@capacity=capacity
@used=0
end
def reserve(seats)
return false if seats<=0 || seats>@capacity-@used
@used+=seats
true
end
def remaining
@capacity-@used
end
end
room=Room.new(Integer(STDIN.gets,10))
accepted=0
STDIN.each_line { |line| accepted+=1 if room.reserve(Integer(line,10)) }
puts "#{room.remaining}:#{accepted}"
Modifica app.luau, model.luau. Il file main.luau visibile e bloccato legge stdin in una sequenza di righe, chiama app.run(lines) e stampa il valore restituito con un solo a capo finale. Non stampare messaggi diagnostici né modificare i nomi dei moduli. L'input vuoto produce una sequenza vuota. Tutti gli input numerici sono abbastanza piccoli da evitare overflow.
Verifiche dell'API pubblica: Model.new(capacity) espone capacity e used, inizialmente pari a zero. reserve restituisce un booleano e aggiorna used solo per una richiesta positiva che rientra nella capacità. Le sale sono indipendenti. Il driver bloccato verifica questo comportamento prima di chiamare app.run. Queste verifiche non stampano nulla quando hanno esito positivo.
Probier es selbst
-- Prüfungen der öffentlichen 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))
Alle Lektionen in Luau für Ruby-Entwickler
1Erste Luau-Programme
Luau-Module ausführenAusgabe und KommentareNamen und lokaler GültigkeitsbereichZahlen und KonvertierungZeichenketten und Byte-IndizesWiederholung: Einen Beleg formatieren4Funktionen und Closures
Funktionen und RückgabewerteStandardwerte und Options-TabellenMehrere Rückgabewerte und VarargsCallbacks und erfasster ZustandRückblick: Preisfunktionen2Werte und Entscheidungen
None und fehlende FelderWahrheitswerte und StandardwerteVerzweigungen und GrenzenGleichheit und IdentitätRückblick: Zulassungsregeln3Iteration und Kontrollfluss
Von Ranges zu numerischen for-Schleifenwhile und repeatVon enumerate zu ipairsFiltern und vorzeitiger AbbruchRückblick: Statistiken lesenÜbe selbstständig: Online-Luau-Compiler