Swift-Raumobjekte portieren
Lektion 48 von 48 im Kurs Lua für Swift-Entwickler von Coddy.
Aufgabe
MittelPortiere la versione di riferimento in Swift. La prima riga di input è la capacità non negativa della stanza; le righe rimanenti sono richieste di prenotazione con segno. Accetta solo richieste strettamente positive che rientrano nella capacità. Le richieste non riuscite non modificano i posti occupati. Restituisci remaining:accepted. Implementa Model.new(capacity) e Model:reserve(seats) in model.lua; reserve restituisce un valore booleano. Usa un nuovo oggetto a ogni esecuzione.
Versione di riferimento in Swift
final class Room {
let capacity: Int
private(set) var used = 0
init(capacity: Int) { self.capacity = capacity }
func reserve(_ seats: Int) -> Bool {
guard seats > 0 && seats <= capacity - used else { return false }
used += seats
return true
}
}
let room = Room(capacity: Int(readLine()!)!)
var accepted = 0
while let line = readLine() {
if room.reserve(Int(line)!) { accepted += 1 }
}
print("\(room.capacity - room.used):\(accepted)")
Modifica app.lua, model.lua. Il file visibile e bloccato main.lua legge stdin in una sequenza di righe, chiama app.run(lines) e stampa il valore restituito con un solo newline finale. Non stampare messaggi diagnostici e non modificare i nomi dei moduli. Un input vuoto produce una sequenza vuota. Tutti gli input numerici sono abbastanza piccoli da evitare overflow.
Controlli dell’API pubblica: Model.new(capacity) espone capacity e used, inizialmente pari a zero. reserve restituisce un valore booleano e aggiorna used solo per una richiesta positiva che rientra nella capacità. Le stanze sono indipendenti. Il driver bloccato verifica questo comportamento prima di chiamare app.run. Se hanno esito positivo, questi controlli non stampano nulla.
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 Lua für Swift-Entwickler
1Erste Lua-Programme
Lua-Module ausführenAusgeben und kommentierenNamen und lokaler GültigkeitsbereichZahlen und KonvertierungZeichenketten und Byte-IndizesRückblick: Eine Quittung formatieren4Funktionen und Closures
Funktionen und RückgabewerteStandardwerte und Options-TabellenMehrere Ergebnisse und VarargsCallbacks und erfasster ZustandZusammenfassung: Preisfunktionen2Werte und Entscheidungen
Nil und fehlende FelderWahrheit und StandardwerteVerzweigungen und GrenzenGleichheit und IdentitätRückblick: Zulassungsregeln3Iteration und Ablaufsteuerung
Von Wertebereichen zur numerischen for-Schleifewhile- und repeat-SchleifenVon Array-Indizes zu ipairsFiltern und vorzeitiger AbbruchRückblick: Statistiken lesenÜbe selbstständig: Online-Lua-Compiler