Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.Optional;
- import java.util.function.Function;
- import java.util.function.Supplier;
- import java.util.Optional;
- import java.util.function.Function;
- import java.util.function.Predicate;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class EquationTester {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int testCase = Integer.parseInt(sc.nextLine());
- if (testCase == 1) { // Testing with Integer, Integer
- List<Equation<Integer, Integer>> equations1 = new ArrayList<>();
- List<Integer> inputs = new ArrayList<>();
- while (sc.hasNext()) {
- inputs.add(Integer.parseInt(sc.nextLine()));
- }
- // TODO: Add an equation where you get the 3rd integer from the inputs list, and the result is the sum of that number and the number 1000.
- equations1.add(new Equation<>(
- () -> inputs.get(2),
- integer -> integer+1000
- ));
- // TODO: Add an equation where you get the 4th integer from the inputs list, and the result is the maximum of that number and the number 100.
- equations1.add(new Equation<>(
- () -> inputs.get(3),
- integer -> Math.max(integer,100)
- ));
- EquationProcessor.process(inputs, equations1);
- } else { // Testing with Line, Integer
- List<Equation<Line, Double>> equations2 = new ArrayList<>();
- List<Line> inputs = new ArrayList<>();
- while (sc.hasNext()) {
- inputs.add(Line.createLine(sc.nextLine()));
- }
- //TODO Add an equation where you get the 2nd line, and the result is the value of y in the line equation.
- equations2.add(new Equation<>(
- () -> inputs.get(1),
- i -> i.calculateLine()
- ));
- //TODO Add an equation where you get the 1st line, and the result is the sum of all y values for all lines that have a greater y value than that equation.
- equations2.add(new Equation<>(
- () -> inputs.get(0),
- i-> {
- double Y = i.calculateLine();
- return inputs.stream()
- .mapToDouble(Line::calculateLine)
- .filter(y->y>Y)
- .sum();
- }
- ));
- EquationProcessor.process(inputs, equations2);
- }
- }
- }
- class Rule<IN, OUT> {
- Predicate<IN> predicate;
- Function<IN,OUT> function;
- public Rule(Predicate<IN> predicate, Function<IN, OUT> function) {
- this.predicate = predicate;
- this.function = function;
- }
- public Optional<OUT> apply (IN input){
- if(predicate.test(input)){
- return Optional.of(function.apply(input));
- }
- return Optional.empty();
- }
- }
- class Line {
- Double coeficient;
- Double x;
- Double intercept;
- public Line(Double coeficient, Double x, Double intercept) {
- this.coeficient = coeficient;
- this.x = x;
- this.intercept = intercept;
- }
- public static Line createLine(String line) {
- String[] parts = line.split("\\s+");
- return new Line(
- Double.parseDouble(parts[0]),
- Double.parseDouble(parts[1]),
- Double.parseDouble(parts[2])
- );
- }
- public double calculateLine() {
- return coeficient * x + intercept;
- }
- @Override
- public String toString() {
- return String.format("%.2f * %.2f + %.2f", coeficient, x, intercept);
- }
- }
- class Equation<IN,OUT> {
- Supplier<IN> supplier;
- Function<IN,OUT> function;
- public Equation(Supplier<IN> supplier, Function<IN, OUT> function) {
- this.supplier = supplier;
- this.function = function;
- }
- public Optional<OUT> calculate(){
- return Optional.of(function.apply(supplier.get()));
- }
- }
- class EquationProcessor {
- public static <IN, OUT> void process(List<IN> inputs, List<Equation<IN, OUT>> equations) {
- List<List<OUT>> resultsPerEquation = new ArrayList<>();
- // Print all inputs
- for (IN input : inputs) {
- System.out.println("Input: " + input);
- }
- // Evaluate each equation once per input and store results
- for (Equation<IN, OUT> eq : equations) {
- List<OUT> results = new ArrayList<>();
- for (IN input : inputs) {
- Optional<OUT> result = eq.calculate();
- result.ifPresent(results::add);
- }
- resultsPerEquation.add(results);
- }
- // Check if each equation gives the same result for all inputs
- for (List<OUT> results : resultsPerEquation) {
- if (results.isEmpty()) continue;
- OUT first = results.get(0);
- boolean allEqual = results.stream().allMatch(r -> r.equals(first));
- if (allEqual) {
- System.out.println("Result: " + first);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement