-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionGenerator.java
More file actions
52 lines (39 loc) · 1.61 KB
/
ExpressionGenerator.java
File metadata and controls
52 lines (39 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package fourEqualsTen;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
public class ExpressionGenerator {
public static List<String> generateExpressions(List<String> operatorPermutations, List<String> numberPermutations) {
List<String> expressions = new ArrayList<>();
for (String permutationNum : numberPermutations)
{
for(String ops : operatorPermutations )
{
expressions.add(generateExpressionsHelper(ops,permutationNum));
}
}
return expressions;
}
public static String generateExpressionsHelper(String operatorPermutation, String numberPermutations) {
//1(23)4
StringBuilder res = new StringBuilder();
StringBuilder ops = new StringBuilder(operatorPermutation);
for (int i =0; i<numberPermutations.length();i++)
{
char c =numberPermutations.charAt(i);
// Check conditions for appending the character
if ((Character.isDigit(c) && (i != numberPermutations.length() -1 && (Character.isDigit(numberPermutations.charAt(i + 1)) ||
numberPermutations.charAt(i + 1) == '(' ) ) ) || c == ')' && i != numberPermutations.length() -1) {
res.append(c);
res.append(ops.charAt(0));
ops.deleteCharAt(0);
continue;
}
res.append(c);
}
return res.toString();
}
}