Menu
CoddyTech

نقل كائنات الغرف في Dart

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

challenge icon

التحدي

متوسط

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

مرجع Dart

import 'dart:io';
class Room {
  final int capacity;
  int used=0;
  Room(this.capacity);
  bool reserve(int seats){
    if(seats<=0 || seats>capacity-used) return false;
    used+=seats;
    return true;
  }
}
void main(){
  final room=Room(int.parse(stdin.readLineSync()!));
  var accepted=0;
  String? line;
  while((line=stdin.readLineSync())!=null){
    if(room.reserve(int.parse(line!))) accepted++;
  }
  print('${room.capacity-room.used}:$accepted');
}

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

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

جرّب بنفسك

-- فحوصات واجهة البرمجة العامة
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))

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

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