Advertisement
dzocesrce

[NP] Equation

Apr 18th, 2025
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.Optional;
  3. import java.util.function.Function;
  4. import java.util.function.Supplier;
  5. import java.util.Optional;
  6. import java.util.function.Function;
  7. import java.util.function.Predicate;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11.  
  12. public class EquationTester {
  13.     public static void main(String[] args) {
  14.         Scanner sc = new Scanner(System.in);
  15.         int testCase = Integer.parseInt(sc.nextLine());
  16.  
  17.         if (testCase == 1) { // Testing with Integer, Integer
  18.             List<Equation<Integer, Integer>> equations1 = new ArrayList<>();
  19.             List<Integer> inputs = new ArrayList<>();
  20.             while (sc.hasNext()) {
  21.                 inputs.add(Integer.parseInt(sc.nextLine()));
  22.             }
  23.  
  24.             // 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.
  25.             equations1.add(new Equation<>(
  26.                     () -> inputs.get(2),
  27.                     integer -> integer+1000
  28.             ));
  29.  
  30.             // 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.
  31.  
  32.             equations1.add(new Equation<>(
  33.                     () -> inputs.get(3),
  34.                     integer -> Math.max(integer,100)
  35.             ));
  36.             EquationProcessor.process(inputs, equations1);
  37.  
  38.         } else { // Testing with Line, Integer
  39.             List<Equation<Line, Double>> equations2 = new ArrayList<>();
  40.             List<Line> inputs = new ArrayList<>();
  41.             while (sc.hasNext()) {
  42.                 inputs.add(Line.createLine(sc.nextLine()));
  43.             }
  44.  
  45.             //TODO Add an equation where you get the 2nd line, and the result is the value of y in the line equation.
  46.             equations2.add(new Equation<>(
  47.                     () -> inputs.get(1),
  48.                     i -> i.calculateLine()
  49.             ));
  50.  
  51.             //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.
  52.             equations2.add(new Equation<>(
  53.                     () -> inputs.get(0),
  54.                     i-> {
  55.                         double Y = i.calculateLine();
  56.                         return inputs.stream()
  57.                                 .mapToDouble(Line::calculateLine)
  58.                                 .filter(y->y>Y)
  59.                                 .sum();
  60.                     }
  61.             ));
  62.             EquationProcessor.process(inputs, equations2);
  63.         }
  64.     }
  65. }
  66. class Rule<IN, OUT> {
  67.  
  68.     Predicate<IN> predicate;
  69.     Function<IN,OUT> function;
  70.  
  71.     public Rule(Predicate<IN> predicate, Function<IN, OUT> function) {
  72.         this.predicate = predicate;
  73.         this.function = function;
  74.     }
  75.  
  76.     public Optional<OUT> apply (IN input){
  77.         if(predicate.test(input)){
  78.             return Optional.of(function.apply(input));
  79.         }
  80.         return Optional.empty();
  81.     }
  82. }
  83.  
  84. class Line {
  85.     Double coeficient;
  86.     Double x;
  87.     Double intercept;
  88.  
  89.     public Line(Double coeficient, Double x, Double intercept) {
  90.         this.coeficient = coeficient;
  91.         this.x = x;
  92.         this.intercept = intercept;
  93.     }
  94.  
  95.     public static Line createLine(String line) {
  96.         String[] parts = line.split("\\s+");
  97.         return new Line(
  98.                 Double.parseDouble(parts[0]),
  99.                 Double.parseDouble(parts[1]),
  100.                 Double.parseDouble(parts[2])
  101.         );
  102.     }
  103.  
  104.     public double calculateLine() {
  105.         return coeficient * x + intercept;
  106.     }
  107.  
  108.     @Override
  109.     public String toString() {
  110.         return String.format("%.2f * %.2f + %.2f", coeficient, x, intercept);
  111.     }
  112. }
  113.  
  114. class Equation<IN,OUT> {
  115.     Supplier<IN> supplier;
  116.     Function<IN,OUT> function;
  117.  
  118.     public Equation(Supplier<IN> supplier, Function<IN, OUT> function) {
  119.         this.supplier = supplier;
  120.         this.function = function;
  121.     }
  122.  
  123.     public Optional<OUT> calculate(){
  124.         return Optional.of(function.apply(supplier.get()));
  125.     }
  126. }
  127. class EquationProcessor {
  128.     public static <IN, OUT> void process(List<IN> inputs, List<Equation<IN, OUT>> equations) {
  129.         List<List<OUT>> resultsPerEquation = new ArrayList<>();
  130.  
  131.         // Print all inputs
  132.         for (IN input : inputs) {
  133.             System.out.println("Input: " + input);
  134.         }
  135.  
  136.         // Evaluate each equation once per input and store results
  137.         for (Equation<IN, OUT> eq : equations) {
  138.             List<OUT> results = new ArrayList<>();
  139.             for (IN input : inputs) {
  140.                 Optional<OUT> result = eq.calculate();
  141.                 result.ifPresent(results::add);
  142.             }
  143.             resultsPerEquation.add(results);
  144.         }
  145.  
  146.         // Check if each equation gives the same result for all inputs
  147.         for (List<OUT> results : resultsPerEquation) {
  148.             if (results.isEmpty()) continue;
  149.             OUT first = results.get(0);
  150.             boolean allEqual = results.stream().allMatch(r -> r.equals(first));
  151.             if (allEqual) {
  152.                 System.out.println("Result: " + first);
  153.             }
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement