Advertisement
GeorgiLukanov87

03. Football League , Exam Preparation l 100/100 REGEX

Jul 26th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. # 03. Football League , Exam Preparation l 100/100 REGEX .
  2. # https://judge.softuni.org/Contests/Practice/Index/967#2
  3.  
  4. def adding_results(team):
  5.     goals = team[2].split(":")
  6.     team1 = team[0]
  7.     team2 = team[1]
  8.     goals1 = int(goals[0])
  9.     goals2 = int(goals[1])
  10.  
  11.     if team1 not in top_3_goals:
  12.         top_3_goals[team1] = goals1
  13.     else:
  14.         top_3_goals[team1] += goals1
  15.     if team2 not in top_3_goals:
  16.         top_3_goals[team2] = goals2
  17.     else:
  18.         top_3_goals[team2] += goals2
  19.  
  20.     if goals1 == 0 and goals2 == 0 or goals1 == goals2:  # for Draw 0:0
  21.         if team1 not in league_standings:
  22.             league_standings[team1] = 0
  23.             league_standings[team1] += 1
  24.         else:
  25.             league_standings[team1] += 1
  26.         if team2 not in league_standings:
  27.             league_standings[team2] = 0
  28.             league_standings[team2] += 1
  29.         else:
  30.             league_standings[team2] += 1
  31.  
  32.     elif goals1 > goals2:  # TEAM 1 WON
  33.         if team1 not in league_standings:
  34.             league_standings[team1] = 0
  35.             league_standings[team1] += 3
  36.         else:
  37.             league_standings[team1] += 3
  38.         if team2 not in league_standings:
  39.             league_standings[team2] = 0
  40.  
  41.     elif goals1 < goals2:  # TEAM 2 WON
  42.         if team2 not in league_standings:
  43.             league_standings[team2] = 0
  44.             league_standings[team2] += 3
  45.         else:
  46.             league_standings[team2] += 3
  47.         if team1 not in league_standings:
  48.             league_standings[team1] = 0
  49.  
  50.  
  51. import re
  52.  
  53. top_3_goals = {}
  54. league_standings = {}
  55.  
  56. key_pass = input()
  57. data = input()
  58.  
  59. new_key_pass = ""
  60. for char in key_pass:
  61.     if not char.isalnum():
  62.         new_key_pass += "\ " + char
  63.     else:
  64.         new_key_pass += char
  65.  
  66. new_key_pass = new_key_pass.replace(" ", "")
  67. pattern_teams = fr'({new_key_pass})([a-zA-Z0-9]+)\1'
  68. teams = []
  69.  
  70. while not data == 'final':
  71.     matched_teams = re.findall(pattern_teams, data)
  72.     for index, el in enumerate(matched_teams):
  73.         reverse_name = el[1]
  74.         teams.append(reverse_name[::-1].upper())
  75.     current_score = data.split(' ')[-1]
  76.     teams.append(current_score)
  77.     if len(teams) < 3:
  78.         teams.insert(0, "")
  79.     adding_results(teams)
  80.     teams.clear()
  81.     data = input()
  82.  
  83. details = []
  84. print('League standings:')
  85. team_counter1 = 0
  86. for team in sorted(league_standings.items(), key=lambda kv: (-kv[1], kv[0])):
  87.     team_counter1 += 1
  88.     print(f'{team_counter1}. {team[0]} {team[1]}')
  89.  
  90. print('Top 3 scored goals:')
  91. team_counter2 = 0
  92. for el in sorted(top_3_goals.items(), key=lambda x: (-x[1], x[0])):
  93.     team_counter2 += 1
  94.     print(f'- {el[0]} -> {el[1]}')
  95.     if team_counter2 == 3:
  96.         break
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement