MCPcopy Index your code
hub / github.com/careercup/ctci / bruteForce

Method bruteForce

java/Chapter 9/Question9_11/Question.java:139–167  ·  view source on GitHub ↗
(String expression, HashMap<String, Boolean> completed, boolean result, boolean[] flags)

Source from the content-addressed store, hash-verified

137 }
138
139 public static int bruteForce(String expression, HashMap<String, Boolean> completed, boolean result, boolean[] flags) {
140 int count = 0;
141 boolean isDone = true;
142 if (completed.containsKey(expression)) {
143 return 0;
144 }
145
146 for (int i = 0; i < flags.length; i++) {
147 if (!flags[i]) {
148 flags[i] = true;
149 String newexpression = insertParensAround(expression, i);
150 isDone = false;
151 count += bruteForce(newexpression, completed, result, flags);
152 flags[i] = false;
153 }
154 }
155
156 if (isDone) {
157 if (evaluate(expression, 0, expression.length() - 1) == result) {
158 System.out.println(expression + " = " + result);
159 return 1;
160 } else {
161 System.out.println(expression + " = " + !result);
162 return 0;
163 }
164 }
165 completed.put(expression, true);
166 return count;
167 }
168
169 public static int countR(String exp, boolean result, int start, int end) {
170 if (start == end) {

Callers 1

mainMethod · 0.95

Calls 4

insertParensAroundMethod · 0.95
evaluateMethod · 0.95
putMethod · 0.80
lengthMethod · 0.45

Tested by

no test coverage detected