Menu
CoddyTech

移植:库存命令

Coddy 面向 C# 开发者的 Swift 课程的第 47 课,共 48 课。

challenge icon

挑战

中等

将 C# 参考实现移植到 Swift。实现 Inventory(),并提供 add(_ name: String, count: Int) -> Bool、take(_ name: String, count: Int) -> Bool、quantity(of name: String) -> Int 和 report() -> String。Add 只接受正数。Take 只接受不超过当前库存的正数。拒绝操作时保留所有状态;未知物品的数量为零。成功取出物品后,仍保留库存为零的条目。按名称升序报告所有已存储的物品,并以空格分隔,格式为 name:quantity。输入包含 0 到 40 组命令,每组由三个部分组成:add name count 或 take name count。名称是最多 10 个字母的小写 ASCII 单词,数量范围为 -10 到 100。输出成功执行的命令数量;仅当报告非空时,才在后面加一个空格和报告内容。

实现所需的 API 和 solve(_ input: String) -> String。返回不带末尾换行符的输出文本;不要在可编辑代码中打印内容。锁定的驱动程序会使用其他有效输入单独检查这些 API,然后将返回的文本打印出来,并恰好添加一个换行符。所有数值运算均在 Int 的范围内。

C# 参考实现的类拥有一个已排序的字典;多个别名共享其状态。在 Swift 实现中,保留拒绝操作时不修改状态以及库存为零的条目。

以下是符合所述输入/输出约定的完整 C# 参考实现。请将此行为移植到所需的 Swift 文件中:

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

public sealed class Inventory
{
    private readonly SortedDictionary<string, int> quantities =
        new SortedDictionary<string, int>(StringComparer.Ordinal);

    public int Quantity(string name)
    {
        int value;
        return quantities.TryGetValue(name, out value) ? value : 0;
    }
    public bool Add(string name, int count)
    {
        if (count <= 0) return false;
        quantities[name] = Quantity(name) + count;
        return true;
    }
    public bool Take(string name, int count)
    {
        if (count <= 0 || count > Quantity(name)) return false;
        quantities[name] = Quantity(name) - count;
        return true;
    }
    public string Report()
    {
        return string.Join(" ", quantities.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 inventory = new Inventory();
        int accepted = 0;
        for (int i = 0; i < tokens.Length; i += 3)
        {
            string name = tokens[i + 1];
            int count = int.Parse(tokens[i + 2]);
            bool ok = tokens[i] == "add"
                ? inventory.Add(name, count) : inventory.Take(name, count);
            if (ok) accepted += 1;
        }
        string report = inventory.Report();
        Console.WriteLine(accepted + (report.Length == 0 ? "" : " " + report));
    }
}

你的代码应写在 Inventory.swift 和 Functions.swift 中。main.swift 存放提供的输入/输出代码,不能编辑。

亲手试试

import Foundation
func apiChecks(_ input: String) -> Bool {
let x = Inventory(); guard x.quantity(of: "missing") == 0 && !x.take("missing", count: 1) && x.report().isEmpty else { return false }; guard x.add("oak", count: 4) && !x.take("oak", count: 5) && !x.add("oak", count: 0) && x.quantity(of: "oak") == 4 else { return false }; return x.take("oak", count: 4) && x.report() == "oak:0" && !x.take("oak", count: -1) && Inventory().report().isEmpty
}
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 在线编辑器