Libraryless. Click here for Pure Java version (6993L/50K/155K).
!7 !include once #1003474 // class E abstract sclass Learner { abstract void processInOut(S in, S out); abstract S processIn(S in); } static LMath learner; p { testPoem("#1003072"); // Addition Poem testPoem("#1003073"); // Division Poem print("OK!"); } static void testPoem(S snippetID) { learner = new LMath; L<Text> texts = loadPoems(snippetID); L<E> test1 = parsePoem(texts.get(0).lines); L<E> test2 = parsePoem(texts.get(1).lines); learner.processInOut(test1.get(0).q, test1.get(1).a); print("Test: " + assertTrue(runPoemTest1(test2))); } static void untest(L<E> test) { for (E e : test) e.test = false; } // used by runPoemTest1 static L<E> predict(L<E> input) { if (l(input) != 1) ret null; new E e; S q = input.get(0).q; e.a = learner.processIn(q); print(q + " >> " + e.a + " " + structure(learner.possibleOperations)); ret litlist(e); } sclass LMath extends Learner { String allOperations = "+ - * /"; Set<String> possibleOperations = new TreeSet<String>(); String outPattern = ""; LMath() { restart(); } void restart() { possibleOperations.addAll(Arrays.asList(allOperations.split(" +"))); } void processInOut(String in, String out) { outPattern = out.replaceAll("-?\\d+", "*"); BigInteger[] inNumbers = extractIntsFromString(in); BigInteger[] outNumbers = extractIntsFromString(out); try { findOperation(inNumbers, outNumbers); } catch (Throwable e) { e.printStackTrace(); } } public void findOperation(BigInteger[] in, BigInteger[] out) { filterOperations(in, out); if (possibleOperations.isEmpty()) { System.out.println("TILT"); restart(); filterOperations(in, out); } } public void filterOperations(BigInteger[] in, BigInteger[] out) { for (Iterator<String> i = possibleOperations.iterator(); i.hasNext(); ) { String op = i.next(); BigInteger[] out2 = doOperation(op, in); if (out2 == null || !arraysEqual(out, out2)) i.remove(); // keep only matching operations } } public BigInteger[] doOperation(String op, BigInteger[] in) { op = op.intern(); try { if (in.length == 2) { BigInteger a = in[0], b = in[1], x = null; if (op == "+") x = a.add(b); else if (op == "-") x = a.subtract(b); else if (op == "*") x = a.multiply(b); else if (op == "/") x = a.divide(b); return x != null ? new BigInteger[] {x} : null; } return null; } catch (Throwable e) { return null; } } String processIn(String in) { if (possibleOperations.isEmpty()) return ""; String op = possibleOperations.iterator().next(); BigInteger[] inNumbers = extractIntsFromString(in); BigInteger[] outNumbers = doOperation(op, inNumbers); String s = outPattern; if (outNumbers != null) for (BigInteger num : outNumbers) s = outPattern.replaceFirst("\\*", num.toString()); return s; } }
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003067 |
Snippet name: | Solve 2 Math Poems (works!) |
Eternal ID of this version: | #1003067/3 |
Text MD5: | 5b6174888e3eb7da7328657b02f9f91a |
Transpilation MD5: | f9d9da463f11252d498900d6135b3005 |
Author: | stefan |
Category: | eleu / nl |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-09-26 00:16:31 |
Source code size: | 3180 bytes / 121 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 647 / 746 |
Version history: | 2 change(s) |
Referenced in: | [show references] |