Problem 56538. Cricket - Report the Result (Part II: Test Matches)

Given two scalar strings representing the scores for a test match, return a string reporting the result in the appropriate form:
• "Match drawn"
• "TeamName won by n runs"
• "TeamName won by n wickets"
• "TeamName won by an innings and n runs"
The strings will be given in the order the teams batted and will have the form "TeamName r/w & r/w" (where r is runs and w is wickets). If a team is all out, their score will be given as just "r" (rather than "r/10"). The convention "d" is used for declared innings (eg "432/1d") and "(f/o)" for following on (eg "England 123 & 234 (f/o)").
Given that tied Tests are very rare (and drawn Tests with the scores level equally rare), you do not need to worry about scores being level. You can also be grammatically incorrect for a win by 1 wicket or 1 run (ie you can report the result as "... won by 1 wickets", rather than making a special case for "1 wicket"). Other than that, your function will need to cope with all other possibilities, such as "West Indies 123 & 456/7d (f/o)".
The strings will always use single spaces between the team name and the scores, but team names may be multiple words ("Sri Lanka", "South Africa", etc) which will also be separated by single spaces. The two innings scores will be separated by "&".
For a match to have a result (not a draw), the losing team must have completed two innings with their total score still below the winning team’s score. If the winning team is batting last, their margin of victory will be in terms of wickets. Otherwise, it will be in terms of runs. If they batted only one innings, their victory will be given as an innings and runs. All possible permutations of how this can occur are summarized in the table below.
s1 = "India 250 & 307";
s2 = "Australia 235 & 291";
res = reportresult(s1,s2)
res =
"India won by 31 runs"
s1 = "South Africa 573/4d";
s2 = "Bangladesh 147 & 172 (f/o)";
res = reportresult(s1,s2)
res =
"South Africa won by an innings and 254 runs"
• Test matches are played over a fixed time period. If the match is not completed in that time, the result is a draw (regardless of the score).
• To avoid running out of time (thus resulting in a draw), a team can choose to declare their innings completed before they are all out. This is effectively a gamble to forfeit the remainder of their innings for the sake of time.
• After both teams' first innings, if Team B is sufficiently far (200+ runs) behind Team A, A can choose to make B follow-on, meaning Team B has to bat again immediately, while Team A can keep their second innings in reserve if needed. Hence, the order of innings is A, B, B, A (if necessary), instead of the usual A, B, A, B.
With these rules, you can always figure out the order of play, and the result, given the team scores. For example:
Team A 543/6d & 123/4
Team B 234 & 456/7d (f/o)
1. Team A scores 543 and declares with 4 wickets still in hand
2. Team B scores 234 (all out)
3. Team B, forced to follow-on (309 runs behind), scores 456 and declares with 3 wickets in hand, giving A a target of 148 to win
4. Team A scores 123 for the loss of 4 wickets, leaving them 24 runs behind Team B's total
Result: match drawn, because the team with the lower score (A) had not yet completed their second innings.

Solution Stats

42.86% Correct | 57.14% Incorrect
Last Solution submitted on Nov 03, 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!