Advertisement
meteor4o

JF-RegEx-Exec-02.Race

Jul 29th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Race {
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.  //       String[] names = sc.nextLine().split(", ");
  13.         List<String> testNames = Arrays.stream(sc.nextLine().split(", ")).collect(Collectors.toList());
  14.  
  15.          String input = sc.nextLine();
  16.  
  17.  
  18.         String regexName = "([A-Za-z]+)";
  19.         Pattern namePattern = Pattern.compile(regexName);
  20.  
  21.         Matcher matcher = namePattern.matcher(input);
  22.  
  23.         String regexDistance = "([\\d])";
  24.         Pattern distancePattern = Pattern.compile(regexDistance);
  25.  
  26.         Map<String, Integer> results = new LinkedHashMap<>();
  27.  
  28.         while (!input.equals("end of race")) {
  29.  
  30.             String currentName = "";
  31.                 while (matcher.find()) {
  32.                     currentName = currentName + matcher.group(1);
  33.                 }
  34.  
  35.                     if (testNames.contains(currentName)) {
  36.                         matcher = distancePattern.matcher(input);
  37.  
  38.                         int newResult = 0;
  39.  
  40.                         while (matcher.find()) {
  41.                             newResult += Integer.parseInt(matcher.group(1));
  42.                         }
  43.  
  44.                             if (results.containsKey(currentName)) {
  45.                                 int oldResult = results.get(currentName);
  46.  
  47.                                 results.put(currentName, oldResult + newResult);
  48.                             } else {
  49.                                 results.put(currentName, newResult);
  50.                             }
  51.  
  52.                         }
  53.             input = sc.nextLine();
  54.             matcher = namePattern.matcher(input);
  55.         }
  56.  
  57. //        for (Map.Entry<String, Integer> entry : results.entrySet()) {
  58. //            System.out.printf("%s - %d%n", entry.getKey(), entry.getValue());
  59. //        }
  60.         List<String> finals = new ArrayList<>();
  61.         results.entrySet().stream().sorted((a, b) ->{
  62.             int result = b.getValue() - a.getValue();
  63.             return result;
  64.         })
  65.                 .limit(3)
  66.                 .forEach(e -> finals.add(e.getKey()));
  67.         System.out.printf("1st place: %s%n", finals.get(0));
  68.         System.out.printf("2nd place: %s%n", finals.get(1));
  69.         System.out.printf("3rd place: %s%n", finals.get(2));
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement