Final: Inventory Port
Lesson 47 of 48 in Coddy's GDScript for Java Developers course.
Challenge
EasyPort the Java 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 Java reference. Compile the listed .java files together with Java 17, then run Main. The program reads the same JSON array and prints the same result protocol using only standard-library classes.
Java file Inventory.java:
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
public class Inventory {
public static String summarize(List<String> words) {
Map<String,Integer> counts=new TreeMap<>();
for(String word:words) counts.merge(word,1,Integer::sum);
List<String> parts=new ArrayList<>();
for(var entry:counts.entrySet()) parts.add(entry.getKey()+"="+entry.getValue());
return parts.isEmpty()?"none":String.join(",",parts);
}
}
Java file Main.java:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class Main {
// Parser for the contract's JSON array of lowercase ASCII words.
private final String text;
private int at=0;
private Main(String text) { this.text=text; }
private char peek() { return at<text.length()?text.charAt(at):0; }
private char take() {
if(at==text.length()) throw new IllegalArgumentException("unexpected end");
return text.charAt(at++);
}
private void space() {
while(peek()==' ' || peek()=='\t' || peek()=='\r' || peek()=='\n') ++at;
}
private String word() {
if(take()!='"') throw new IllegalArgumentException("expected string");
StringBuilder out=new StringBuilder();
while(peek()!='"') {
int value=take();
if(value=='\\') {
if(take()!='u') throw new IllegalArgumentException("expected escape");
value=0;
for(int i=0;i<4;++i) {
int h=Character.digit(take(),16);
if(h<0) throw new IllegalArgumentException("invalid escape");
value=value*16+h;
}
}
if(value<'a' || value>'z') throw new IllegalArgumentException("expected lowercase ASCII");
out.append((char)value);
}
take();
return out.toString();
}
private List<String> parse() {
space();
if(take()!='[') throw new IllegalArgumentException("expected array");
space();
List<String> words=new ArrayList<>();
if(peek()!=']') {
for(;;) {
words.add(word()); space();
if(peek()==']') break;
if(take()!=',') throw new IllegalArgumentException("expected comma");
space();
}
}
take(); space();
if(at!=text.length()) throw new IllegalArgumentException("trailing input");
return words;
}
public static void main(String[] args) throws IOException {
String input=new String(System.in.readAllBytes(),StandardCharsets.UTF_8);
System.out.println("result="+Inventory.summarize(new Main(input).parse()));
}
}
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 Java 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