Menu
CoddyTech

Final: Port Inventaris

Pelajaran 47 dari 48 dalam kursus GDScript untuk Developer C++ di Coddy.

challenge icon

Tantangan

Mudah

Porting referensi C++: terima kata sebagai input, lalu kembalikan pasangan kata=jumlah yang diurutkan secara alfabetis dan dipisahkan dengan koma, atau none. Kata menggunakan huruf kecil ASCII, duplikat dihitung secara terpisah. Jangan ubah input pemanggil.

Input berupa satu array JSON di stdin. main yang dikunci menguraikannya, memanggil API publik, lalu mencetak result= diikuti teks yang dikembalikan dan karakter baris baru. Tidak ada helper yang boleh mencetak. Probe tetap independen memverifikasi perilaku API yang diwajibkan sebelum kasus input, tetapi tidak menentukan gaya implementasi.

Referensi C++ lengkap. Simpan semua file ini bersama-sama, lalu compile main.cpp dengan C++20. Header menyediakan API helper; main membaca array JSON yang sama dan mencetak protokol hasil yang sama. Hanya library standar yang digunakan.

File 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;
}

File 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';
}

Tuliskan kodemu di exercise.gd. main.gd berisi kode input/output yang disediakan dan tidak boleh diedit. Simpan setiap tipe class_name di file dengan nama yang sesuai.

Coba sendiri

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))

Semua pelajaran di GDScript untuk Developer C++

Latihan sendiri: Compiler GDScript online