Final: Inventory Port
Lesson 47 of 48 in Coddy's GDScript for C++ Developers course.
Challenge
EasyPort the C++ reference: input words, return alphabetically sorted word=count pairs joined by commas, or none. Words are lowercase ASCII, duplicates count separately. Do not change caller input.
Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.
Complete C++ reference. Save these files together and compile main.cpp with C++20. The headers provide the helper APIs; main reads the same JSON array and prints the same result protocol. Only the standard library is used.
C++ file 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++ file 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';
}
Your code goes in exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.
Try it yourself
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))
All lessons in GDScript for C++ Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command CountPractice on your own: Online GDScript compiler