Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

121
LINES

< > BotCompany Repo | #1003067 // Solve 2 Math Poems (works!)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (6993L/50K/155K).

1  
!7
2  
3  
!include once #1003474 // class E 
4  
5  
abstract sclass Learner {
6  
  abstract void processInOut(S in, S out);
7  
  abstract S processIn(S in);
8  
}
9  
10  
static LMath learner;
11  
12  
p {
13  
  testPoem("#1003072"); // Addition Poem
14  
  testPoem("#1003073"); // Division Poem
15  
  print("OK!");
16  
}
17  
18  
static void testPoem(S snippetID) {
19  
  learner = new LMath;
20  
  L<Text> texts = loadPoems(snippetID);
21  
  
22  
  L<E> test1 = parsePoem(texts.get(0).lines);
23  
  L<E> test2 = parsePoem(texts.get(1).lines);
24  
  
25  
  learner.processInOut(test1.get(0).q, test1.get(1).a);
26  
  
27  
  print("Test: " + assertTrue(runPoemTest1(test2)));
28  
}
29  
30  
static void untest(L<E> test) {
31  
  for (E e : test)
32  
    e.test = false;
33  
}
34  
35  
// used by runPoemTest1
36  
static L<E> predict(L<E> input) {
37  
  if (l(input) != 1) ret null;
38  
  
39  
  new E e;
40  
  S q = input.get(0).q;
41  
  e.a = learner.processIn(q);
42  
  print(q + " >> " + e.a + " " + structure(learner.possibleOperations));
43  
  ret litlist(e);
44  
}
45  
46  
sclass LMath extends Learner {
47  
  String allOperations = "+ - * /";
48  
  Set<String> possibleOperations = new TreeSet<String>();
49  
  String outPattern = "";
50  
  
51  
  LMath() {
52  
    restart();
53  
  }
54  
  
55  
  void restart() {
56  
    possibleOperations.addAll(Arrays.asList(allOperations.split(" +")));
57  
  }
58  
  
59  
  void processInOut(String in, String out) {
60  
    outPattern = out.replaceAll("-?\\d+", "*");
61  
    BigInteger[] inNumbers = extractIntsFromString(in);
62  
    BigInteger[] outNumbers = extractIntsFromString(out);
63  
    try {
64  
      findOperation(inNumbers, outNumbers);
65  
    } catch (Throwable e) {
66  
      e.printStackTrace();
67  
    }
68  
  }
69  
  
70  
  public void findOperation(BigInteger[] in, BigInteger[] out) {
71  
    filterOperations(in, out);
72  
    if (possibleOperations.isEmpty()) {
73  
      System.out.println("TILT");
74  
      restart();
75  
      filterOperations(in, out);
76  
    }
77  
  }
78  
    
79  
  public void filterOperations(BigInteger[] in, BigInteger[] out) {
80  
    for (Iterator<String> i = possibleOperations.iterator(); i.hasNext(); ) {
81  
      String op = i.next();
82  
      BigInteger[] out2 = doOperation(op, in);
83  
      if (out2 == null || !arraysEqual(out, out2))
84  
        i.remove(); // keep only matching operations
85  
    }
86  
  }
87  
  
88  
  public BigInteger[] doOperation(String op, BigInteger[] in) {
89  
    op = op.intern();
90  
    try {
91  
      if (in.length == 2) {
92  
        BigInteger a = in[0], b = in[1], x = null;
93  
        if (op == "+")
94  
          x = a.add(b);
95  
        else if (op == "-")
96  
          x = a.subtract(b);
97  
        else if (op == "*")
98  
          x = a.multiply(b);
99  
        else if (op == "/")
100  
          x = a.divide(b);
101  
        return x != null ? new BigInteger[] {x} : null;
102  
      }
103  
      return null;
104  
    } catch (Throwable e) {
105  
      return null;
106  
    }
107  
  }
108  
  
109  
  String processIn(String in) {
110  
    if (possibleOperations.isEmpty()) return "";
111  
    String op = possibleOperations.iterator().next();
112  
    
113  
    BigInteger[] inNumbers = extractIntsFromString(in);
114  
    BigInteger[] outNumbers = doOperation(op, inNumbers);
115  
    String s = outPattern;
116  
    if (outNumbers != null)
117  
      for (BigInteger num : outNumbers)
118  
        s = outPattern.replaceFirst("\\*", num.toString());
119  
    return s;
120  
  }
121  
}

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: 649 / 749
Version history: 2 change(s)
Referenced in: [show references]