| 1 | 1 | def tally(rows): |
| 2 | | pass |
| 2 | # Initialize dictionary to store team statistics |
| 3 | teams = {} |
| 4 | |
| 5 | # Process each match result |
| 6 | for row in rows: |
| 7 | if not row.strip(): |
| 8 | continue |
| 9 | |
| 10 | parts = row.strip().split(';') |
| 11 | if len(parts) != 3: |
| 12 | continue |
| 13 | |
| 14 | team1, team2, result = [part.strip() for part in parts] |
| 15 | |
| 16 | # Validate result |
| 17 | if result not in ['win', 'loss', 'draw']: |
| 18 | continue |
| 19 | |
| 20 | # Ensure both teams exist in the dictionary |
| 21 | if team1 not in teams: |
| 22 | teams[team1] = {'MP': 0, 'W': 0, 'D': 0, 'L': 0, 'P': 0} |
| 23 | if team2 not in teams: |
| 24 | teams[team2] = {'MP': 0, 'W': 0, 'D': 0, 'L': 0, 'P': 0} |
| 25 | |
| 26 | # Update match played count for both teams |
| 27 | teams[team1]['MP'] += 1 |
| 28 | teams[team2]['MP'] += 1 |
| 29 | |
| 30 | # Update statistics based on result |
| 31 | if result == 'win': |
| 32 | teams[team1]['W'] += 1 |
| 33 | teams[team1]['P'] += 3 |
| 34 | teams[team2]['L'] += 1 |
| 35 | elif result == 'loss': |
| 36 | teams[team1]['L'] += 1 |
| 37 | teams[team2]['W'] += 1 |
| 38 | teams[team2]['P'] += 3 |
| 39 | elif result == 'draw': |
| 40 | teams[team1]['D'] += 1 |
| 41 | teams[team1]['P'] += 1 |
| 42 | teams[team2]['D'] += 1 |
| 43 | teams[team2]['P'] += 1 |
| 44 | |
| 45 | # Edge Case: Empty input - return header only |
| 46 | if not teams: |
| 47 | return ["Team | MP | W | D | L | P"] |
| 48 | |
| 49 | # Sort teams by points (descending) and then by name (ascending) |
| 50 | sorted_teams = sorted(teams.items(), key=lambda x: (-x[1]['P'], x[0])) |
| 51 | |
| 52 | # Format the output |
| 53 | result_table = ["Team | MP | W | D | L | P"] |
| 54 | |
| 55 | for team_name, stats in sorted_teams: |
| 56 | line = f"{team_name:<30} | {stats['MP']:>2} | {stats['W']:>2} | {stats['D']:>2} | {stats['L']:>2} | {stats['P']:>2}" |
| 57 | result_table.append(line) |
| 58 | |
| 59 | return result_table |
| 60 | # Handled Edge Cases: Empty input, invalid match format, proper sorting by points and team name |