移植:策略成绩册
Coddy 面向 C# 开发者的 Swift 课程的第 48 课,共 48 课。
挑战
中等移植 C# 参考实现。实现 ScoreRule,要求包含 accepts(_ score: Int) -> Bool,并实现 PassRule(minimum:),接受大于或等于 minimum 的分数。实现值类型 GradeBook(),包含变异方法 record(_ name: String, score: Int) -> Bool 和 report(using rule: any ScoreRule) -> String。接受 0 到 100 的分数;同名记录会覆盖之前的分数。其他分数一律拒绝,且不做更改。报告只包含通过所提供规则的记录,按名称排序,并以空格分隔,格式为 name:score。自定义规则和相互独立的副本都必须正常工作。输入内容为 0 到 100 的最低分,后接 0 到 30 组名称和分数。名称为最多 10 个字母的小写 ASCII 单词;分数范围为 -10 到 110。输出成功记录的操作次数,只有报告非空时才在后面加一个空格和报告内容。
实现所需的 API 和 solve(_ input: String) -> String。返回不带末尾换行符的输出文本;不要在可编辑代码中打印内容。锁定的驱动程序会使用其他有效输入独立检查这些 API,然后在返回的文本后恰好打印一个换行符。所有数值运算都在 Int 的范围内。
C# 结构体会在每次成功写入前替换其私有字典,这样复制出来的簿册就能保持独立。单纯的结构体赋值只会浅拷贝字典引用。在 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)!
// 公开 API 检查开始
let apiOK = apiChecks(input)
// 公开 API 检查结束
if apiOK { print(solve(input)) } else { print("API mismatch") }
面向 C# 开发者的 Swift中的所有课
自己练习: Swift 在线编辑器