Advertisement
dzocesrce

[NP] Rule

Apr 18th, 2025
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Optional;
  3. import java.util.Optional;
  4. import java.util.function.Function;
  5. import java.util.function.Predicate;
  6. import java.util.ArrayList;
  7. import java.util.Comparator;
  8. import java.util.List;
  9. import java.util.Scanner;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.stream.Collectors;
  13.  
  14. public class RuleTester {
  15.     public static void main(String[] args) {
  16.         Scanner sc = new Scanner(System.in);
  17.         int testCase = Integer.parseInt(sc.nextLine());
  18.  
  19.         if (testCase == 1) { //Test for String,Integer
  20.             List<Rule<String, Integer>> rules = new ArrayList<>();
  21.  
  22.             /*
  23.             TODO: Add a rule where if the string contains the string "NP", the result would be index of the first occurrence of the string "NP"
  24.             * */
  25.             rules.add(new Rule<>(
  26.                     str -> str.contains("NP"),
  27.                     str -> str.indexOf("NP")
  28.             ));
  29.  
  30.             /*
  31.             TODO: Add a rule where if the string starts with the string "NP", the result would be length of the string
  32.             * */
  33.             rules.add(new Rule<>(
  34.                     str -> str.startsWith("NP"),
  35.                     str -> str.length()
  36.             ));
  37.  
  38.  
  39.  
  40.             List<String> inputs = new ArrayList<>();
  41.             while (sc.hasNext()) {
  42.                 inputs.add(sc.nextLine());
  43.             }
  44.  
  45.             RuleProcessor.process(inputs, rules);
  46.  
  47.  
  48.         } else { //Test for Student, Double
  49.             List<Rule<Student, Double>> rules = new ArrayList<>();
  50.  
  51.             //TODO Add a rule where if the student has at least 3 grades, the result would be the max grade of the student
  52.             rules.add(new Rule<>(
  53.                     student -> student.grades.size()>=3,
  54.                     student -> student.grades.stream().mapToDouble(i->i).max().getAsDouble()
  55.             ));
  56.  
  57.             //TODO Add a rule where if the student has an ID that starts with 20, the result would be the average grade of the student
  58.             //If the student doesn't have any grades, the average is 5.0
  59.             rules.add(new Rule<>(
  60.                     student -> student.id.startsWith("20"),
  61.                     student -> student.grades.stream().mapToDouble(i->i).average().orElse(5.0)
  62.             ));
  63.  
  64.             List<Student> students = new ArrayList<>();
  65.             while (sc.hasNext()){
  66.                 students.add(Student.create(sc.nextLine()));
  67.             }
  68.  
  69.             RuleProcessor.process(students, rules);
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75.  
  76.  class Rule<IN, OUT> {
  77.  
  78.     Predicate<IN> predicate;
  79.     Function<IN,OUT> function;
  80.  
  81.     public Rule(Predicate<IN> predicate, Function<IN, OUT> function) {
  82.         this.predicate = predicate;
  83.         this.function = function;
  84.     }
  85.  
  86.     public Optional<OUT> apply (IN input){
  87.         if(predicate.test(input)){
  88.             return Optional.of(function.apply(input));
  89.         }
  90.         return Optional.empty();
  91.     }
  92. }
  93.  
  94. class RuleProcessor {
  95.  
  96.     public static <IN, OUT> void process(List<IN> inputs, List<Rule<IN,OUT>> rules) {
  97.  
  98.         for(IN input : inputs) {
  99.             System.out.println("Input: "+input.toString());
  100.             for(Rule<IN,OUT> rule : rules) {
  101.                 Optional<OUT> result = rule.apply(input);
  102.                 if(result.isPresent()) {
  103.                     System.out.println("Result: "+result.get().toString());
  104.                 }
  105.                 else{
  106.                     System.out.println("Condition not met");
  107.                 }
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113.  
  114. class Student {
  115.     String id;
  116.     List<Integer> grades;
  117.  
  118.     public Student(String id, List<Integer> grades) {
  119.         this.id = id;
  120.         this.grades = grades;
  121.     }
  122.  
  123.     public static Student create(String line) {
  124.         String[] parts = line.split("\\s+");
  125.         String id = parts[0];
  126.         List<Integer> grades = Arrays.stream(parts).skip(1).map(Integer::parseInt).collect(Collectors.toList());
  127.         return new Student(id, grades);
  128.     }
  129.  
  130.     @Override
  131.     public String toString() {
  132.         return "Student{" +
  133.                 "id='" + id + '\'' +
  134.                 ", grades=" + grades +
  135.                 '}';
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement