הסבה: פקודות מלאי
שיעור 47 מתוך 48 בקורס Swift למפתחי C# של Coddy.
אתגר
בינוניהסב את מימוש הייחוס ב־C# ל־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. השמות הם מילים באותיות ASCII קטנות, באורך של עד 10 אותיות, והכמויות הן בין -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") }
כל השיעורים ביחידה Swift למפתחי C#
1תחילת הדרך עם Swift
עבודה עם קבציםהשמות ושינוי ערכיםמספרים והמרהתנאים בוליאנייםסיכום: הצעת מחיר לנסיעה4פונקציות ומערכים
פונקציות ותוויותInout וגישה בלעדיתTuples ותוצאות מרובותלמערכים יש סמנטיקה של ערךסיכום: מדרגות עולותתרגלו בעצמכם: Swift אונליין