!636 !quickmain !auto-import !standard functions interface Learner { void processInOut(String in, String out); String processIn(String in); } // only "learns" the "id" function class LId implements Learner { public void processInOut(String in, String out) { } public String processIn(String in) { return in; } } main { static List<String[]> fullExamples = new ArrayList<String[]>(); static List<String> halfExamples = new ArrayList<String>(); static List<String[]> examples1, examples2; psvm { parse(args.length != 0 ? args[0] : null); calculate(); } static void parse(String arg) tex { String text; if (arg != null) text = loadSnippet(arg); else { text = loadTextFile("input/input.txt", null); if (text == null) text = loadSnippet("#2000377"); } System.out.println(text); String in = null, out = null; for (String line : toLines(text)) { if (line.startsWith("I")) { // "In: " or "I: " if (in != null) halfExamples.add(in); in = unquote(line.substring(line.indexOf(':')+1).trim()); out = null; } else if (line.startsWith("O")) { // "Out: " or "O: " out = unquote(line.substring(line.indexOf(':')+1).trim()); System.out.println(quote(in) + " => " + quote(out)); fullExamples.add(new String[] {in, out}); in = out = null; } } if (in != null) halfExamples.add(in); } static void calculate() tex { if (fullExamples.size() < 2) throw new RuntimeException("Too few examples (" + fullExamples.size() + ")"); int splitPoint = fullExamples.size()-1; System.out.println("Full examples: " + fullExamples.size() + ", splitPoint: " + splitPoint); examples1 = fullExamples.subList(0, splitPoint); examples2 = fullExamples.subList(splitPoint, fullExamples.size()); Learner learner = findOKLearner(); if (learner == null) print "\nProblem not solved" else { print "\nSolved!\n" for (String in : halfExamples) { String out = learner.processIn(in); System.out.println(quote(in) + " =>! " + quote(out)); } } } static Learner findOKLearner() { for (Learner learner : makeLearners()) try { if (learnerOK(learner)) return learner; } catch (Throwable e) { e.printStackTrace(); } return null; } static boolean learnerOK(Learner learner) { for (String[] e : examples1) { learner.processInOut(e[0], e[1]); } for (String[] e : fullExamples) { String out = learner.processIn(e[0]); if (!e[1].equals(out)) { System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - " + quote(out) + " vs " + quote(e[1])); return false; } } return true; // all test examples passed } static Iterable<Learner> makeLearners() { List<Learner> list = new ArrayList<Learner>(); list.add(new LId()); list.add(new LPrefixSuffix()); return list; } public static String unquote(String s) { if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\\\", "\\"); // SHOULD work... else return s; // Return SOMETHING } public static String quote(String s) { if (s == null) return "null"; return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\""; } // learns to exchange common prefixes and suffixes static class LPrefixSuffix implements Learner { String prefixIn, suffixIn, prefixOut, suffixOut; public void processInOut(String in, String out) { updateIn(in); prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out); suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out); } void updateIn(String in) { prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in); suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in); } public String processIn(String in) { //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); //System.out.println("[last info] " + quote(in)); // use latest information String p = prefixIn, s = suffixIn; updateIn(in); prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length())); suffixOut = suffixOut.substring(s.length()-suffixIn.length()); //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); String core = in.substring(prefixIn.length(), in.length()-suffixIn.length()); return prefixOut + core + suffixOut; } } }
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #639 |
Snippet name: | IOIOI Processor (v1, does prefixes and suffixes) |
Eternal ID of this version: | #639/1 |
Text MD5: | 721155e40b6f047d3c353012a45c101f |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-07-24 20:02:58 |
Source code size: | 4983 bytes / 155 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 891 / 743 |
Referenced in: | [show references] |