Advertisement
dzocesrce

[NP] The Chase

Apr 24th, 2025
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7. import java.util.stream.Collectors;
  8. import java.util.AbstractMap;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.stream.Collectors;
  13. class FishyQuizDetection extends Exception {
  14.     public FishyQuizDetection(String message) {
  15.         super(message);
  16.     }
  17. }
  18. class Quiz {
  19.     public static double calculatePoints(List<String> correctAnswers, List<String> givenAnswers) throws FishyQuizDetection {
  20.         if(correctAnswers.size()!=givenAnswers.size())
  21.             throw new FishyQuizDetection("A quiz must have same number of correct and selected answers");
  22.         double totalPoints=0;
  23.         for(int i=0;i<correctAnswers.size();i++){
  24.             if(correctAnswers.get(i).equals(givenAnswers.get(i)))
  25.                 totalPoints++;
  26.             else
  27.                 totalPoints-=0.25;
  28.         }
  29.         return totalPoints;
  30.     }
  31.     public static Map.Entry<String,Double> create(String string) {
  32.         String[] parts = string.split(";");
  33.         String id = parts[0];
  34.         List<String> correctAnswers = Arrays.stream(parts[1].trim().split(",")).collect(Collectors.toList());
  35.         List<String> givenAnswers = Arrays.stream(parts[2].trim().split(",")).collect(Collectors.toList());
  36.  
  37.         try {
  38.             return new AbstractMap.SimpleEntry<>(id, calculatePoints(correctAnswers,givenAnswers));
  39.         } catch (FishyQuizDetection e) {
  40.             System.out.println(e.getMessage());
  41.             return new AbstractMap.SimpleEntry<>("AUF", 0.0);
  42.         }
  43.  
  44.     }
  45. }
  46.  
  47. class QuizProcessor {
  48.     Map<String ,Double> answers;
  49.  
  50.     public QuizProcessor() {
  51.         answers= new LinkedHashMap<>();
  52.     }
  53.  
  54.     public static Map<String, Double> processAnswers(InputStream in) {
  55.         BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(in));
  56.  
  57.         Map<String ,Double> answers= bufferedReader.lines().map(Quiz::create).filter(i->!i.getKey().equals("AUF")).collect(Collectors.toMap(
  58.                 entry->entry.getKey(),
  59.                 entry->entry.getValue(),
  60.                 (a,b)->a,
  61.                 LinkedHashMap::new
  62.         ));
  63.  
  64.         return answers;
  65.     }
  66. }
  67.  
  68.  
  69. public class QuizProcessorTest {
  70.     public static void main(String[] args) {
  71.         QuizProcessor.processAnswers(System.in).forEach((k, v) -> System.out.printf("%s -> %.2f%n", k, v));
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement