Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Map;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class FootballLeague {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String key = scanner.nextLine();
- Map<String, Integer> teamsPoints = new TreeMap<>();
- Map<String, Integer> teamsGoals = new TreeMap<>();
- String input = scanner.nextLine();
- while (!input.equals("final")) {
- String regex = "(.*)?" + Pattern.quote(key) + "(?<teamA>[A-Za-z]*)(.*?)" +
- Pattern.quote(key) + "(.*?)" + Pattern.quote(key) +
- "(?<teamB>[A-Za-z]*)" + Pattern.quote(key) + "(.*?)(?<scoreTeamA>\\d+):(?<scoreTeamB>\\d+)";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- String teamAReversed = matcher.group("teamA");
- String teamBReversed = matcher.group("teamB");
- int scoreTeamA = Integer.parseInt(matcher.group("scoreTeamA"));
- int scoreTeamB = Integer.parseInt(matcher.group("scoreTeamB"));
- int pointsTeamA = 0;
- int pointsTeamB = 0;
- if (scoreTeamA > scoreTeamB) {
- pointsTeamA = 3;
- } else if (scoreTeamA == scoreTeamB) {
- pointsTeamA = 1;
- pointsTeamB = 1;
- } else {
- pointsTeamB = 3;
- }
- String teamA = reverse(teamAReversed).toUpperCase();
- String teamB = reverse(teamBReversed).toUpperCase();
- if (!teamsPoints.containsKey(teamA)) {
- teamsPoints.put(teamA, pointsTeamA);
- teamsGoals.put(teamA, scoreTeamA);
- } else {
- teamsPoints.put(teamA, teamsPoints.get(teamA) + pointsTeamA);
- teamsGoals.put(teamA, teamsGoals.get(teamA) + scoreTeamA);
- }
- if (!teamsPoints.containsKey(teamB)) {
- teamsPoints.put(teamB, pointsTeamB);
- teamsGoals.put(teamB, scoreTeamB);
- } else {
- teamsPoints.put(teamB, teamsPoints.get(teamB) + pointsTeamB);
- teamsGoals.put(teamB, teamsGoals.get(teamB) + scoreTeamB);
- }
- }
- input = scanner.nextLine();
- }
- if (!teamsPoints.isEmpty()) {
- final int[] position = {1};
- System.out.println("League standings:");
- teamsPoints.entrySet()
- .stream()
- .sorted((f, s) -> {
- return s.getValue() - f.getValue();
- })
- .forEach(e -> {
- System.out.printf("%d. %s %d%n", position[0], e.getKey(), e.getValue());
- position[0]++;
- });
- System.out.println("Top 3 scored goals:");
- teamsGoals.entrySet()
- .stream()
- .sorted((f, s) -> {
- return s.getValue() - f.getValue();
- })
- .limit(3)
- .forEach(e -> {
- System.out.printf("- %s -> %d%n", e.getKey(), e.getValue());
- });
- }
- }
- private static String reverse(String text) {
- String result = "";
- for (int i = text.length() - 1; i >= 0; i--) {
- result += text.charAt(i);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment