Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_tournament_table(match_data):
- raw_match_data = [line for line in match_data.split('\n') if line]
- teams = {}
- for match in raw_match_data[1:]:
- team1, score1, team2, score2 = match.split(';')
- if score1 > score2:
- update_team_stats(team1, 3, teams)
- update_team_stats(team2, 0, teams)
- elif score2 > score1:
- update_team_stats(team1, 0, teams)
- update_team_stats(team2, 3, teams)
- else:
- update_team_stats(team1, 1, teams)
- update_team_stats(team2, 1, teams)
- sorted_teams = sorted(teams.items(), key=lambda item: -item[1]['points'])
- print_tournament(sorted_teams)
- def update_team_stats(team, points, teams):
- if team in teams:
- teams[team]['points'] += points
- teams[team]['win'] += 1 if points == 3 else 0
- teams[team]['lose'] += 1 if points == 0 else 0
- teams[team]['draw'] += 1 if points == 1 else 0
- else:
- teams[team] = {
- 'points': points,
- 'win': 1 if points == 3 else 0,
- 'lose': 1 if points == 0 else 0,
- 'draw': 1 if points == 1 else 0
- }
- def print_tournament(teams):
- print('Team | Games | Wins | Draws | Loses | Points')
- print('-----------------------------------------------------')
- for name, team in teams:
- games = team['win'] + team['lose'] + team['draw']
- print(f'{name.ljust(13)} | {games:5d} | {team["win"]:4d} | {team["draw"]:5d} | {team["lose"]:5d} | {team["points"]:6d}')
- create_tournament_table("""
- 3
- Зенит;3;Спартак;1
- Спартак;1;ЦСКА;1
- ЦСКА;0;Зенит;2
- """)
- # Output:
- # Team | Games | Wins | Draws | Loses | Points
- # -----------------------------------------------------
- # Зенит | 2 | 2 | 0 | 0 | 6
- # Спартак | 2 | 0 | 1 | 1 | 1
- # ЦСКА | 2 | 0 | 1 | 1 | 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement