Menu
CoddyTech

移植:インベントリコマンド

Coddyの「Luau 開発者のための Swift」コースのレッスン 47/48。

challenge icon

チャレンジ

中級

LuauのリファレンスをSwiftに移植してください。Inventory() を実装し、add(_ name: String, count: Int) -> Bool、take(_ name: String, count: Int) -> Bool、quantity(of name: String) -> Int、および report() -> String を作成してください。Addは正の数のみを受け入れます。Takeは現在の在庫以下の正の数のみを受け入れます。拒否された場合はすべての状態が保持され、未知の数量はゼロとなります。成功したtakeは在庫ゼロのエントリを保持します。保存されているすべての名前を昇順でスペース区切りの name:quantity として出力してください。入力には0から40個のコマンドのトリプルが含まれます: add name count または take name count。名前は最大10文字の小文字ASCII単語で、カウントは-10から100です。成功したコマンドの数を出力し、空でない場合のみスペースに続けてレポートを出力してください。

必要なAPIおよび solve(_ input: String) -> String を実装してください。末尾に改行を付けずに出力テキストを返してください。編集可能なコード内では印刷(標準出力)を行わないでください。ロックされたドライバーは、追加の有効な入力に対してAPIを独立してチェックし、返されたテキストを正確に1つの改行を付けて印刷します。すべての数値演算はIntの範囲内に収まります。

Luauのリファレンスでは、有界整数値とASCII名を使用しています。そのコラボレータは呼び出し可能なメソッドを持つテーブルです。Swiftへの移植では、プロトコル適合性を明示的に宣言し、必要なファイル構成を維持してください。

同じ入力と出力の仕様に対する完全なLuauリファレンス:

local function new_inventory()
  local stock = {}
  local inventory = {}
  function inventory:quantity(name) return stock[name] or 0 end
  function inventory:add(name, count)
    if count <= 0 then return false end
    stock[name] = self:quantity(name) + count
    return true
  end
  function inventory:take(name, count)
    local available = self:quantity(name)
    if count <= 0 or count > available then return false end
    stock[name] = available - count
    return true
  end
  function inventory:report()
    local names, parts = {}, {}
    for name in pairs(stock) do names[#names + 1] = name end
    table.sort(names)
    for _, name in ipairs(names) do parts[#parts + 1] = name .. ":" .. stock[name] end
    return table.concat(parts, " ")
  end
  return inventory
end
local function solve(input)
  local tokens = {}
  for token in input:gmatch("%S+") do tokens[#tokens + 1] = token end
  local inventory, accepted = new_inventory(), 0
  for i = 1, #tokens, 3 do
    local name, count = tokens[i + 1], tonumber(tokens[i + 2])
    local ok
    if tokens[i] == "add" then ok = inventory:add(name, count)
    else ok = inventory:take(name, count) end
    if ok then accepted = accepted + 1 end
  end
  local report = inventory:report()
  return tostring(accepted) .. (report == "" and "" or " " .. report)
end
print(solve(io.read("*a")))

あなたのコードは 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") }

Luau 開発者のための Swiftのすべてのレッスン

自分で練習してみよう: Swiftプレイグラウンド