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

Method reduce

java/Chapter 9/Question9_11/Question.java:16–72  ·  view source on GitHub ↗
(String expression, int start, int end)

Source from the content-addressed store, hash-verified

14 };
15
16 public static String reduce(String expression, int start, int end) {
17 if (start == end) {
18 if (expression.charAt(start) == '1') {
19 return "1";
20 } else if (expression.charAt(start) == '0') {
21 return "0";
22 }
23 }
24 int count = 0;
25 int i = 0;
26 String[] reduced = new String[3];
27 int index = 0;
28 int left = start;
29 int right = start;
30 for (i = start; i <= end; i++) {
31 if (expression.charAt(i) == '(') {
32 if (count == 0) {
33 left = i + 1;
34 }
35 count++;
36 } else if (expression.charAt(i) == ')') {
37 count--;
38 if (count == 0) {
39 right = i - 1;
40 }
41 }
42 if (count == 0) {
43 reduced[index] = reduce(expression, left, right);
44 if (index == 0) {
45 reduced[index + 1] = Character.toString(expression.charAt(i + 1));
46 i += 1;
47 left = i + 1;
48 right = i + 1;
49 }
50 index += 2;
51 }
52 }
53 if (reduced[1].equals("&")) {
54 if (reduced[0].equals("1") && reduced[2].equals("1")) {
55 return "1";
56 }
57 return "0";
58 } else if (reduced[1].equals("|")) {
59 if (reduced[0].equals("1") || reduced[2].equals("1")) {
60 return "1";
61 }
62 return "0";
63 } else if (reduced[1].equals("^")) {
64 if (reduced[0].equals("1") && reduced[2].equals("0")) {
65 return "1";
66 } else if (reduced[0].equals("0") && reduced[2].equals("1")) {
67 return "1";
68 }
69 return "0";
70 }
71 return "0";
72 }
73

Callers 1

evaluateMethod · 0.95

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected