Advertisement
Edzhevit

Race

Nov 20th, 2018
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package RegularExpressions;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Arrays;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11.  
  12. public class Race {
  13.     public static void main(String[] args) throws IOException {
  14.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  15.         String line = reader.readLine();
  16.         Map<String, Integer> participants = new HashMap<>();
  17.         String[] names = line.split(", ");
  18.         Arrays.stream(names).forEach(n -> participants.put(n,0));
  19.  
  20.         line = reader.readLine();
  21.  
  22.         while (!line.equals("end of race")){
  23.             Pattern patternNames = Pattern.compile("[A-Za-z]+");
  24.             Matcher matcherNames = patternNames.matcher(line);
  25.             StringBuilder name = new StringBuilder();
  26.             while (matcherNames.find()){
  27.                 name.append(matcherNames.group());
  28.             }
  29.  
  30.             if (participants.containsKey(name.toString())){
  31.                 patternNames = Pattern.compile("\\d");
  32.                 matcherNames = patternNames.matcher(line);
  33.                 while (matcherNames.find()){
  34.                     int digit = Integer.parseInt(matcherNames.group());
  35.                     participants.put(name.toString(),participants.get(name.toString()) + digit);
  36.                 }
  37.             }
  38.  
  39.  
  40.             line = reader.readLine();
  41.         }
  42.         int[] id = {1};
  43.         participants.entrySet().stream().sorted((e1,e2)-> e2.getValue().compareTo(e1.getValue())).limit(3).forEach(
  44.                 n -> {
  45.                     switch (id[0]){
  46.                         case 1 :
  47.                             System.out.print("1st place: ");
  48.                             break;
  49.                         case 2:
  50.                             System.out.print("2nd place: ");
  51.                             break;
  52.                         case 3:
  53.                             System.out.print("3rd place: ");
  54.                             break;
  55.                     }
  56.                     System.out.println(n.getKey());
  57.                     id[0] += 1;
  58.                 }
  59.         );
  60.  
  61.  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement