Last active
January 5, 2020 19:33
-
-
Save RMuskovets/9f060df54099c6641a811eecbcef4119 to your computer and use it in GitHub Desktop.
Calculating the correct expression for JavaRush task03.task0306 task.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task03.task0306; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
import javax.script.ScriptException; | |
import java.util.*; | |
import java.util.function.Function; | |
import java.util.regex.MatchResult; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import java.util.stream.Collectors; | |
import java.util.stream.StreamSupport; | |
public class Podbor { | |
public static final int EXPECTED_RESULT = 382; | |
public static final String EXPRESSION = "2 * 3 + 4 * 5 + 6 * 7"; | |
static ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); | |
private static boolean checkSolution(String solution) { | |
try { | |
return EXPECTED_RESULT == (int) engine.eval(solution); | |
} catch (ScriptException e) { | |
return false; | |
} | |
} | |
private static int[] getStartNumberPositions(String expr) { | |
Matcher m = Pattern.compile("[0-9]+").matcher(expr); | |
Iterator<MatchResult> matchResultsIter = new Iterator<MatchResult>() { | |
@Override | |
public boolean hasNext() { | |
return m.find(); | |
} | |
@Override | |
public MatchResult next() { | |
return m.toMatchResult(); | |
} | |
}; | |
List<MatchResult> matchResults = StreamSupport | |
.stream(Spliterators.spliteratorUnknownSize(matchResultsIter, Spliterator.ORDERED), false).collect(Collectors.toList()); | |
return matchResults.stream().mapToInt(MatchResult::start).toArray(); | |
} | |
private static int[] getEndNumberPositions(String expr) { | |
Matcher m = Pattern.compile("[0-9]+").matcher(expr); | |
Iterator<MatchResult> matchResultsIter = new Iterator<MatchResult>() { | |
@Override | |
public boolean hasNext() { | |
return m.find(); | |
} | |
@Override | |
public MatchResult next() { | |
return m.toMatchResult(); | |
} | |
}; | |
List<MatchResult> matchResults = StreamSupport | |
.stream(Spliterators.spliteratorUnknownSize(matchResultsIter, Spliterator.ORDERED), false).collect(Collectors.toList()); | |
return matchResults.stream().mapToInt(MatchResult::end) .toArray(); | |
} | |
private static List<String> calculateParens(String initial, int nParens, Function<String, Boolean> checkerFn) { | |
int[] starts = getStartNumberPositions(initial); | |
int[] ends = getEndNumberPositions(initial); | |
Set<String> res = new HashSet<>(); | |
nParens--; | |
for (int i : starts) { | |
for (int j : ends) { | |
if (j < i) continue; | |
String str = initial.substring(0, i) + "(" + initial.substring(i, j) + ")" + initial.substring(j); | |
if ((nParens <= 0 && checkerFn.apply(str))) | |
res.add(str); | |
if (nParens != 0) | |
res.addAll(calculateParens(str, nParens, checkerFn)); | |
} | |
} | |
return new ArrayList<>(res); | |
} | |
public static void main(String[] args) { | |
String expr = EXPRESSION.replaceAll(" ", ""); | |
calculateParens( | |
expr, | |
2, | |
Podbor::checkSolution | |
).forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment