Finished
1from typing import List
1from typing import List, Dict
22
33def 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
Test NameStatus
test_a_different_team_can_win
Pass
test_a_draw_is_one_point_each
Pass
test_a_win_can_also_be_expressed_as_a_loss
Pass
test_a_win_is_three_points_a_loss_is_zero_points
Pass
test_ensure_points_sorted_numerically
Pass
test_incomplete_competition_not_all_pairs_have_played
Pass
test_just_the_header_if_no_input
Pass
test_there_can_be_more_than_one_match
Pass
test_there_can_be_more_than_one_winner
Pass
test_there_can_be_more_than_two_teams
Pass
test_ties_broken_alphabetically
Pass
test_typical_input
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.