Advertisement
dzocesrce

[NP] Student Hall Of Fame

Apr 16th, 2025
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.function.Predicate;
  3. import java.util.stream.Collectors;
  4.  
  5. class Student implements Comparable<Student> {
  6.     String id;
  7.     List<Integer> grades;
  8.  
  9.     public Student(String id, List<Integer> grades) {
  10.         this.id = id;
  11.         this.grades = grades;
  12.     }
  13.  
  14.     public double average() {
  15.         return grades.stream().mapToDouble(i -> i).average().getAsDouble();
  16.     }
  17.  
  18.     public int getYear() {
  19.         return (24 - Integer.parseInt(id.substring(0, 2)));
  20.     }
  21.  
  22.     public int totalCourses() {
  23.         return Math.min(getYear() * 10, 40);
  24.     }
  25.  
  26.     public double labAssistantPoints() {
  27.         return average() * ((double) grades.size() / totalCourses()) * (0.8 + ((getYear()-1)*0.2)/3.0);
  28.     }
  29.  
  30.     @Override
  31.     public int compareTo(Student o) {
  32.         return Comparator.comparing(Student::labAssistantPoints)
  33.                 .thenComparing(Student::average)
  34.                 .compare(this, o);
  35.     }
  36.  
  37.     @Override
  38.     public String toString() {
  39.         return String.format("Student %s (%d year) - %d/%d passed exam, average grade %.2f.\nLab assistant points: %.2f", id, getYear(), grades.size(), totalCourses(), average(), labAssistantPoints());
  40.     }
  41. }
  42.  
  43.  
  44. public class FilterAndSortTest {
  45.     public static void main(String[] args) {
  46.         Scanner sc = new Scanner(System.in);
  47.         int testCase = Integer.parseInt(sc.nextLine());
  48.         int n = Integer.parseInt(sc.nextLine());
  49.  
  50.         if (testCase == 1) { // students
  51.             int studentScenario = Integer.parseInt(sc.nextLine());
  52.             List<Student> students = new ArrayList<>();
  53.             while (n > 0) {
  54.  
  55.                 String line = sc.nextLine();
  56.                 String[] parts = line.split("\\s+");
  57.                 String id = parts[0];
  58.                 List<Integer> grades = Arrays.stream(parts).skip(1).map(Integer::parseInt).collect(Collectors.toList());
  59.                 students.add(new Student(id, grades));
  60.                 --n;
  61.             }
  62.  
  63.             if (studentScenario == 1) {
  64.                 //TODO filter and sort all students who have at least 8.0 points and are at least 3rd year student
  65.                 List<Student> student_results= students.stream().filter(i->i.labAssistantPoints()>=8&&i.getYear()>2).sorted(Comparator.reverseOrder())
  66.                         .collect(Collectors.toList());
  67.                 if(student_results.size()==0)
  68.                     try {
  69.                         throw new EmptyResultException("No element met the criteria");
  70.                     } catch (EmptyResultException e) {
  71.                         System.out.println(e.getMessage());
  72.                     }
  73.                 student_results.stream().forEach(i-> System.out.println(i));
  74.  
  75.  
  76.             } else {
  77.                 //TODO filter and sort all students who have passed at least 90% of their total courses with an average grade of at least 9.0
  78.                 List<Student> student_results= students.stream().filter(i->i.average()>=9&&i.grades.size()>=i.totalCourses()*0.9).sorted(Comparator.reverseOrder())
  79.                         .collect(Collectors.toList());
  80.                 if(student_results.size()==0)
  81.                     try {
  82.                         throw new EmptyResultException("No element met the criteria");
  83.                     } catch (EmptyResultException e) {
  84.                         System.out.println(e.getMessage());
  85.                     }
  86.                 student_results.stream().forEach(i-> System.out.println(i));
  87.             }
  88.         } else { //integers
  89.             List<Integer> integers = new ArrayList<>();
  90.             while (n > 0) {
  91.                 integers.add(Integer.parseInt(sc.nextLine()));
  92.                 --n;
  93.             }
  94.  
  95.             //TODO filter and sort all even numbers divisible with 15
  96.             List<Integer> integer_results =integers.stream().filter(i->i%30==0)
  97.                     .sorted(Comparator.reverseOrder()).collect(Collectors.toList());
  98.             if(integer_results.size()==0)
  99.                 try {
  100.                     throw new EmptyResultException("No element met the criteria");
  101.                 } catch (EmptyResultException e) {
  102.                     System.out.println(e.getMessage());
  103.                 }
  104.             integer_results.stream().forEach(i-> System.out.println(i));
  105.            
  106.         }
  107.  
  108.     }
  109. }
  110. class EmptyResultException extends Exception{
  111.     public EmptyResultException(String message) {
  112.         super(message);
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement