C#-Raumobjekte portieren
Lektion 48 von 48 im Kurs Lua für C#-Entwickler von Coddy.
Aufgabe
MittelPortiere la soluzione di riferimento in C#. La prima riga di input è la capacità non negativa della sala; le righe successive sono richieste di prenotazione con interi 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 booleano. Usa un oggetto nuovo per ogni esecuzione.
Soluzione di riferimento in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
string first=Console.ReadLine();
var room=new Room(long.Parse(first,System.Globalization.CultureInfo.InvariantCulture));
long accepted=0;
string line;
while((line=Console.ReadLine())!=null){
if(room.Reserve(long.Parse(line,System.Globalization.CultureInfo.InvariantCulture))) accepted++;
}
Console.WriteLine((room.Capacity-room.Used).ToString(System.Globalization.CultureInfo.InvariantCulture)+":"+accepted.ToString(System.Globalization.CultureInfo.InvariantCulture));
class Room {
public long Capacity {get;}
public long Used {get;private set;}
public Room(long capacity){Capacity=capacity;}
public bool Reserve(long seats){
if(seats<=0 || seats>Capacity-Used) return false;
Used+=seats;
return true;
}
}
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 carattere di nuova riga 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 se 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 Lua für C#-Entwickler
1Erste Lua-Programme
Lua-Module ausführenAusgeben und kommentierenNamen und lokaler GültigkeitsbereichZahlen und KonvertierungStrings und Byte-IndizesRückblick: Einen Beleg formatieren4Funktionen und Closures
Funktionen und RückgabewerteStandardwerte und OptionstabellenMehrere Rückgabewerte und VarargsCallbacks und gespeicherter ZustandRückblick: Preisfunktionen2Werte und Entscheidungen
Null und fehlende FelderWahrheitswerte und StandardwerteVerzweigungen und GrenzenGleichheit und IdentitätRückblick: Zulassungsregeln3Iteration und Ablaufsteuerung
Von Ranges zu numerischen for-Schleifenwhile und repeatArray-Indizes bis ipairsFiltern und vorzeitiger AbbruchRückblick: Statistiken lesenÜbe selbstständig: Online-Lua-Compiler