Menu
CoddyTech

نقل: سجل درجات السياسات

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

challenge icon

التحدي

متوسط

انقل تطبيق C# المرجعي. نفّذ ScoreRule بحيث يتطلب accepts(_ score: Int) -> Bool، وPassRule(minimum:) لقبول الدرجات التي لا تقل عن الحد الأدنى. نفّذ نوع القيمة GradeBook() مع record(_ name: String, score: Int) -> Bool المعدّلة وreport(using rule: any ScoreRule) -> String. اقبل الدرجات من 0 إلى 100؛ واستبدل الدرجة السابقة للاسم نفسه. ارفض الدرجات الأخرى دون تغيير. أبلغ عن الإدخالات التي تقبلها القاعدة المقدمة فقط، مرتبة حسب الاسم، ومفصولة بمسافات على هيئة name:score. يجب أن تعمل القواعد المخصصة والنسخ المستقلة. الإدخال هو الحد الأدنى من 0 إلى 100، متبوعًا بما يصل إلى 30 زوجًا من الاسم والدرجة. الأسماء كلمات بأحرف ASCII صغيرة يصل طولها إلى 10 أحرف؛ والدرجات من -10 إلى 110. أخرج عدد عمليات تسجيل الدرجات المقبولة، متبوعًا بتقرير مسبوق بمسافة فقط عندما لا يكون فارغًا.

نفّذ واجهات API المطلوبة وsolve(_ input: String) -> String. أعد نص الإخراج دون سطر جديد في النهاية؛ ولا تطبع في الشيفرة القابلة للتحرير. يتحقق برنامج التشغيل المقفل من واجهات API بشكل مستقل باستخدام مدخلات إضافية صالحة، ثم يطبع النص المُعاد متبوعًا بسطر جديد واحد بالضبط. تبقى جميع العمليات العددية ضمن حدود Int.

يستبدل struct في C# قاموسه الخاص قبل كل كتابة ناجحة، لكي تظل الكتب المنسوخة مستقلة. إن إسناد struct عاديًا وحده ينسخ مرجع القاموس نسخًا سطحيًا. في Swift، استخدم نوع القيمة المطلوب واقبل أي قاعدة تسجيل درجات مطابقة.

تطبيق C# المرجعي الكامل لعقد الإدخال/الإخراج المذكور. انقل هذا السلوك إلى ملفات Swift المطلوبة:

using System;
using System.Collections.Generic;
using System.Linq;

public interface IScoreRule
{
    bool Accepts(int score);
}
public sealed class PassRule : IScoreRule
{
    private readonly int minimum;
    public PassRule(int minimum) { this.minimum = minimum; }
    public bool Accepts(int score) { return score >= minimum; }
}
public struct GradeBook
{
    // A struct copy initially shares this dictionary reference. Record replaces
    // it with a fresh dictionary before writing, preserving independent values.
    private SortedDictionary<string, int> scores;

    public bool Record(string name, int score)
    {
        if (score < 0 || score > 100) return false;
        var next = scores == null
            ? new SortedDictionary<string, int>(StringComparer.Ordinal)
            : new SortedDictionary<string, int>(scores, StringComparer.Ordinal);
        next[name] = score;
        scores = next;
        return true;
    }
    public string Report(IScoreRule rule)
    {
        if (scores == null) return "";
        return string.Join(" ", scores.Where(pair => rule.Accepts(pair.Value))
            .Select(pair => pair.Key + ":" + pair.Value));
    }
}
public static class Program
{
    public static void Main()
    {
        string[] tokens = Console.In.ReadToEnd().Split(
            (char[])null, StringSplitOptions.RemoveEmptyEntries);
        var rule = new PassRule(int.Parse(tokens[0]));
        var book = new GradeBook();
        int accepted = 0;
        for (int i = 1; i < tokens.Length; i += 2)
            if (book.Record(tokens[i], int.Parse(tokens[i + 1]))) accepted += 1;
        string report = book.Report(rule);
        Console.WriteLine(accepted + (report.Length == 0 ? "" : " " + report));
    }
}

ضع شيفرتك في ScoreRule.swift وPassRule.swift وGradeBook.swift وFunctions.swift. يحتوي main.swift على شيفرة الإدخال/الإخراج المقدمة، ولا يمكن تعديله.

جرّب بنفسك

import Foundation
func apiChecks(_ input: String) -> Bool {
struct ProbeRule: ScoreRule { func accepts(_ score: Int) -> Bool { score % 2 == 0 } }
var b = GradeBook(); guard b.record("a", score: 40) && b.record("b", score: 71) && !b.record("a", score: 101) else { return false }; var copy = b; _ = copy.record("a", score: 88); return b.report(using: ProbeRule()) == "a:40" && copy.report(using: ProbeRule()) == "a:88" && b.report(using: PassRule(minimum: 40)) == "a:40 b:71" && PassRule(minimum: 50).accepts(50)
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// بداية اختبارات PUBLIC API
let apiOK = apiChecks(input)
// نهاية اختبارات PUBLIC API
if apiOK { print(solve(input)) } else { print("API mismatch") }

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

تدرّب بنفسك: Playground لـ Swift