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