Advertisement
desislava_topuzakova

02. Race

Nov 20th, 2022
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. package regex;
  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.  
  9. public class Race {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String namesParticipants = scanner.nextLine(); //"George, Peter, Bill, Tom"
  13.         List<String> names = Arrays.stream(namesParticipants.split(", ")).collect(Collectors.toList());
  14.         //"George, Peter, Bill, Tom".split(", ")
  15.         //масив: ["George", "Peter", "Bill", "Tom"].toList()
  16.         //лист: {"George", "Peter", "Bill", "Tom"}
  17.  
  18.         //състезател -> дистанция
  19.         Map<String, Integer> racerDistances = new LinkedHashMap<>();
  20.         names.forEach(name -> racerDistances.put(name, 0));
  21.  
  22.         //REGEX
  23.         String regexLetters = "[A-Za-z]+";
  24.         Pattern patternLetters = Pattern.compile(regexLetters); //шаблон за бувките
  25.  
  26.         String regexDigits = "[0-9]";
  27.         Pattern patterDigits = Pattern.compile(regexDigits); //шаблон за цифрите
  28.  
  29.         String input = scanner.nextLine();
  30.         while (!input.equals("end of race")) {
  31.             //input = "G4e@55or%6g6!68e!!@"
  32.             //1. име на състезателя -> всички букви
  33.             StringBuilder racerName = new StringBuilder();
  34.             Matcher matcherLetters = patternLetters.matcher(input);
  35.             //matcher = "G", "e", "or", "g", "e"
  36.             while (matcherLetters.find()) {
  37.                 racerName.append(matcherLetters.group());
  38.             }
  39.  
  40.             //2. дистанция -> сума от цифрите
  41.             int distance = 0;
  42.             Matcher matcherDigits = patterDigits.matcher(input);
  43.             //matcher = "4", "5", "5", "6", "6", "6", "8"
  44.             while (matcherDigits.find()) {
  45.                 distance += Integer.parseInt(matcherDigits.group());
  46.             }
  47.  
  48.             if (names.contains(racerName.toString())) {
  49.                 //имаме играча в първоначалния списък
  50.                 int currentDistance = racerDistances.get(racerName.toString()); //текущата дистанция избягана от играча
  51.                 racerDistances.put(racerName.toString(), currentDistance + distance);
  52.             }
  53.  
  54.             input = scanner.nextLine();
  55.         }
  56.  
  57.  
  58.         //сортираме по дистанция в descending order
  59.         //comparingByValue -> ascending order
  60.         List<String> top3Names = racerDistances.entrySet().stream()
  61.                 .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) // сортирали сме по value (дистанция) в намаляващ ред
  62.                 .limit(3) //оставяме само първите 3 записа
  63.                 .map(entry -> entry.getKey()) //на всеки от трите записа взимаме ключа (име на играча)
  64.                 .collect(Collectors.toList()); // {"George", "Peter", "Tom"}
  65.  
  66.         System.out.println("1st place: " + top3Names.get(0));
  67.         System.out.println("2nd place: " + top3Names.get(1));
  68.         System.out.println("3rd place: " + top3Names.get(2));
  69.  
  70.  
  71.         //без сортировка
  72.         //racerDistances: име -> дистанция
  73.        /* int count = 1; //реда на текущия играч
  74.         for (Map.Entry<String, Integer> pair : racerDistances.entrySet()) {
  75.             //pair: име -> дистанция
  76.             if (count == 1) {
  77.                 //първия играч
  78.                 System.out.println("1st place: " + pair.getKey());
  79.                 count++;
  80.             } else if (count == 2) {
  81.                 //втория играч
  82.                 System.out.println("2nd place: " + pair.getKey());
  83.                 count++;
  84.             } else if (count == 3) {
  85.                 //третия играч
  86.                 System.out.println("3rd place: " + pair.getKey());
  87.                 break;
  88.             }
  89.         }*/
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement