最终:库存端口
Coddy 面向 C# 开发者的 GDScript 课程的第 47 课,共 48 课。
挑战
简单移植 C# 参考实现:输入单词,返回按字母顺序排序、以逗号连接的 word=count 对,或者 none。单词均为小写 ASCII,重复单词要分别计数。不要更改调用方的输入。
输入是 stdin 上的一个 JSON 数组。锁定的 main 会解析它,调用公开 API,并打印 result=,后接返回的文本和一个换行符。任何辅助函数都不应打印内容。在处理输入用例之前,独立的固定探测会验证必需的 API 行为;它们不会限制实现风格。
完整的 C# 参考实现。将列出的文件与 System.Web.Extensions 框架程序集一起编译。Program.Main 会读取相同的 JSON 数组,并按照相同的结果协议打印内容。这是独立的 C# 参考实现,不是 Godot C# 集成。
C# 文件 Inventory.cs:
using System;
using System.Collections.Generic;
using System.Globalization;
public static class Inventory {
public static string Summarize(IEnumerable<string> words) {
var counts = new SortedDictionary<string,int>(StringComparer.Ordinal);
foreach (string word in words) {
int count;
counts.TryGetValue(word, out count);
counts[word] = count + 1;
}
var parts = new List<string>();
foreach (var pair in counts)
parts.Add(pair.Key + "=" + pair.Value.ToString(CultureInfo.InvariantCulture));
return parts.Count == 0 ? "none" : String.Join(",", parts);
}
}C# 文件 Program.cs:
using System;
using System.Web.Script.Serialization;
public static class Program {
public static void Main() {
var words = new JavaScriptSerializer().Deserialize<string[]>(Console.ReadLine());
Console.WriteLine("result=" + Inventory.Summarize(words));
}
}将你的代码写在 exercise.gd 中。main.gd 包含提供的输入/输出代码,不能编辑。每个 class_name 类型都要放在以其名称命名的文件中。
亲手试试
extends Node
func contract_ok(data: Array) -> bool:
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
面向 C# 开发者的 GDScript中的所有课
自己练习: GDScript 在线编译器