最終:インベントリの移植
Coddyの「C++開発者のためのGDScript」コースのレッスン 47/48。
チャレンジ
簡単C++の参照実装を移植してください。入力された単語を受け取り、アルファベット順に並べた単語=countのペアをカンマでつないで返します。単語がない場合はnoneを返します。単語は小文字のASCIIで、重複は個別に数えます。呼び出し元の入力を変更しないでください。
入力は標準入力のJSON配列1つです。編集不可の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オンラインコンパイラ