1 | // math operations. |
2 | // input: L<S> (list of numbers) |
3 | // output: S (number) |
4 | static class LMath extends LearnerImpl { |
5 | static boolean debug; |
6 | String allOperations = "+ - * /"; |
7 | new (Tree)Set<String> possibleOperations; |
8 | |
9 | LMath() { |
10 | possibleOperations.addAll(Arrays.asList(allOperations.split(" +"))); |
11 | } |
12 | |
13 | public void processInOut(Object _in, Object _out) { |
14 | List in = (List) _in; |
15 | String out = (String) _out; |
16 | BigInteger[] inNumbers = makeBigIntArray(in); |
17 | BigInteger[] outNumbers = new BigInteger[] {bigint(out)}; |
18 | findOperation(inNumbers, outNumbers); |
19 | if (debug) |
20 | System.out.println("Operations: " + possibleOperations); |
21 | } |
22 | |
23 | public void findOperation(BigInteger[] in, BigInteger[] out) { |
24 | filterOperations(in, out); |
25 | if (possibleOperations.isEmpty()) |
26 | fail("tilt"); |
27 | } |
28 | |
29 | public void filterOperations(BigInteger[] in, BigInteger[] out) { |
30 | for (Iterator<String> i = possibleOperations.iterator(); i.hasNext(); ) { |
31 | String op = i.next(); |
32 | BigInteger[] out2 = doOperation(op, in); |
33 | if (out2 == null || !arraysEqual(out, out2)) |
34 | i.remove(); // keep only matching operations |
35 | } |
36 | } |
37 | |
38 | public BigInteger[] doOperation(String op, BigInteger[] in) { |
39 | op = op.intern(); |
40 | try { |
41 | if (in.length == 2) { |
42 | BigInteger a = in[0], b = in[1], x = null; |
43 | if (op == "+") |
44 | x = a.add(b); |
45 | else if (op == "-") |
46 | x = a.subtract(b); |
47 | else if (op == "*") |
48 | x = a.multiply(b); |
49 | else if (op == "/") |
50 | x = a.divide(b); |
51 | return x != null ? new BigInteger[] {x} : null; |
52 | } |
53 | return null; |
54 | } catch (Throwable e) { |
55 | return null; |
56 | } |
57 | } |
58 | |
59 | public String processIn(Object _in) { |
60 | L<S> in = cast _in; |
61 | String op = possibleOperations.iterator().next(); |
62 | if (debug) |
63 | System.out.println("op: " + op); |
64 | BigInteger[] inNumbers = makeBigIntArray(in); |
65 | BigInteger[] outNumbers = doOperation(op, inNumbers); |
66 | return outNumbers[0].toString(); |
67 | } |
68 | |
69 | String BI = BigInteger.class.getName(); |
70 | |
71 | public void toJava(Code code) { |
72 | String op = possibleOperations.iterator().next(); |
73 | String a = "new " + BI + "((String) " + code.list() + ".get(0))"; |
74 | String b = "new " + BI + "((String) " + code.list() + ".get(1))"; |
75 | if (op.equals("+")) |
76 | code.assign(a + ".add(" + b + ").toString()"); |
77 | else |
78 | todo(); |
79 | } |
80 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1005157 |
Snippet name: | LMath (learner) |
Eternal ID of this version: | #1005157/1 |
Text MD5: | 72df02fc94077c63162800b4ca6cace1 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-10-17 02:06:06 |
Source code size: | 2484 bytes / 80 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 536 / 916 |
Referenced in: | [show references] |