Menu
CoddyTech

مراجعة: حجز غرفة

الدرس 37 من 48 في دورة C# لمطوّري Kotlin على Coddy.

challenge icon

التحدي

سهل

نفّذ Room بسعة غير سالبة، وحالة remaining خاصة، مع إمكانية قراءة Remaining واستدعاء Reserve(int). اقبل الطلبات الإيجابية فقط إذا كانت ضمن السعة المتبقية. يجب ألا تغيّر الطلبات الفاشلة الحالة. أعد النتائج yes/no مفصولة بفواصل، ثم نقطتين، ثم السعة المتبقية. يختبر برنامج التشغيل الصنف بشكل مستقل.

عدّل ملفات C# غير المقفلة. يقرأ الملف المقفل الظاهر Main.cs الأسطر الموصوفة، ويستدعي Exercise.Solve، ويتحقق من عدم تغيير المدخلات، ثم يطبع النتيجة مع محرف سطر جديد أخير. أبقِ واجهات API المعلنة كما هي. لا تضف Main ثانية، ولا تطبع مخرجات تشخيصية.

يختبر المشغّل المقفل أيضًا واجهة API المساعدة أو واجهة الصنف المصدّرة بشكل مستقل، وليس غلاف Solve فقط.

جرّب بنفسك

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = Ints(input[1]); string before1=Snapshot(a1);
var r1 = new Room(3); var r2 = new Room(8); if (r1.Reserve(0) || r1.Reserve(-1) || r1.Reserve(4) || r1.Remaining != 3 || !r1.Reserve(2) || r1.Remaining != 1 || r2.Remaining != 8) throw new Exception("Room API");
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}

جميع دروس C# لمطوّري Kotlin

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