Menu
CoddyTech

最终:库存端口

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

challenge icon

挑战

简单

移植 C++ 参考实现:输入单词,返回按字母顺序排序、以逗号连接的 word=count 对;如果没有单词,则返回 none。单词均为小写 ASCII,重复项分别计数。不要更改调用方的输入。

输入是 stdin 上的一个 JSON 数组。锁定的 main 会解析数组、调用公开 API,并输出 result=,后面跟着返回的文本和一个换行符。任何辅助函数都不应输出内容。在处理输入用例之前,会通过独立的固定探测验证必需的 API 行为;这些探测不会限制实现风格。

完整的 C++ 参考实现。将这些文件保存在一起,并使用 C++20 编译 main.cpp。头文件提供辅助 API;main 会读取同一个 JSON 数组,并按照相同的结果协议输出。这里只使用标准库。

C++ 文件 inventory.hpp:

#pragma once
#include <map>
#include <string>
#include <vector>
inline std::string summarize(const std::vector<std::string> &words) {
    std::map<std::string,std::size_t> counts;
    for (const auto &word:words) ++counts[word];
    std::string out;
    for (const auto &[word,count]:counts) {
        if (!out.empty()) out+=',';
        out+=word+'='+std::to_string(count);
    }
    return out.empty()?"none":out;
}

C++ 文件 main.cpp:

#include <iostream>
#include <iterator>
#include <stdexcept>
#include "inventory.hpp"
class WordsJson {
    const std::string &text;
    std::size_t at=0;
    char peek() const { return at<text.size()?text[at]:'\0'; }
    char take() { if(at==text.size()) throw std::runtime_error("unexpected end"); return text[at++]; }
    void space() { while(peek()==' ' || peek()=='\n' || peek()=='\r' || peek()=='\t') ++at; }
    static int hex(char c) {
        if(c>='0' && c<='9') return c-'0';
        if(c>='a' && c<='f') return c-'a'+10;
        if(c>='A' && c<='F') return c-'A'+10;
        throw std::runtime_error("invalid escape");
    }
    std::string word() {
        if(take()!='"') throw std::runtime_error("expected string");
        std::string out;
        while(peek()!='"') {
            unsigned value=static_cast<unsigned char>(take());
            if(value=='\\') {
                if(take()!='u') throw std::runtime_error("expected Unicode escape");
                value=0;
                for(int i=0;i<4;++i) value=value*16+hex(take());
            }
            if(value<'a' || value>'z') throw std::runtime_error("expected lowercase ASCII word");
            out+=static_cast<char>(value);
        }
        take(); return out;
    }
public:
    explicit WordsJson(const std::string &input):text(input) {}
    std::vector<std::string> parse() {
        space(); if(take()!='[') throw std::runtime_error("expected array");
        space(); std::vector<std::string> words;
        if(peek()!=']') {
            for(;;) {
                words.push_back(word()); space();
                if(peek()==']') break;
                if(take()!=',') throw std::runtime_error("expected comma");
                space();
            }
        }
        take(); space();
        if(at!=text.size()) throw std::runtime_error("trailing input");
        return words;
    }
};
int main() {
    std::string text{std::istreambuf_iterator<char>(std::cin),std::istreambuf_iterator<char>()};
    auto words=WordsJson(text).parse();
    std::cout << "result=" << summarize(words) << '\n';
}

将你的代码写在 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 在线编译器