Advertisement
Ceridan

tournament_table.py

Jan 22nd, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. def create_tournament_table(match_data):
  2.     raw_match_data = [line for line in match_data.split('\n') if line]
  3.     teams = {}
  4.    
  5.     for match in raw_match_data[1:]:
  6.         team1, score1, team2, score2 = match.split(';')
  7.  
  8.         if score1 > score2:
  9.             update_team_stats(team1, 3, teams)
  10.             update_team_stats(team2, 0, teams)
  11.         elif score2 > score1:
  12.             update_team_stats(team1, 0, teams)
  13.             update_team_stats(team2, 3, teams)
  14.         else:
  15.             update_team_stats(team1, 1, teams)
  16.             update_team_stats(team2, 1, teams)
  17.  
  18.     sorted_teams = sorted(teams.items(), key=lambda item: -item[1]['points'])
  19.     print_tournament(sorted_teams)
  20.    
  21.  
  22. def update_team_stats(team, points, teams):
  23.     if team in teams:
  24.         teams[team]['points'] += points
  25.         teams[team]['win'] += 1 if points == 3 else 0
  26.         teams[team]['lose'] += 1 if points == 0 else 0
  27.         teams[team]['draw'] += 1 if points == 1 else 0
  28.     else:
  29.         teams[team] = {
  30.             'points': points,
  31.             'win': 1 if points == 3 else 0,
  32.             'lose': 1 if points == 0 else 0,
  33.             'draw': 1 if points == 1 else 0
  34.         }
  35.  
  36.  
  37. def print_tournament(teams):
  38.     print('Team          | Games | Wins | Draws | Loses | Points')
  39.     print('-----------------------------------------------------')
  40.    
  41.     for name, team in teams:
  42.         games = team['win'] + team['lose'] + team['draw']
  43.         print(f'{name.ljust(13)} | {games:5d} | {team["win"]:4d} | {team["draw"]:5d} | {team["lose"]:5d} | {team["points"]:6d}')
  44.        
  45.  
  46. create_tournament_table("""
  47. 3
  48. Зенит;3;Спартак;1
  49. Спартак;1;ЦСКА;1
  50. ЦСКА;0;Зенит;2
  51. """)
  52.  
  53. # Output:
  54. # Team          | Games | Wins | Draws | Loses | Points
  55. # -----------------------------------------------------
  56. # Зенит         |     2 |    2 |     0 |     0 |      6
  57. # Спартак       |     2 |    0 |     1 |     1 |      1
  58. # ЦСКА          |     2 |    0 |     1 |     1 |      1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement