Problem 56513. Cricket - Peak Batting Average

Given a vector s of strings representing a batter's individual innings scores (in chronological order), return the highest batting average they achieved in their career, ignoring any undefined or infinite averages. (You can assume that every batter will be dismissed at least once in their career, but not necessarily in their first innings.) The strings will be in the form of a number ("42") or a number with an asterisk ("42*") to represent "not-out".
Batting average is defined as total runs divided by number of dismissals (ie number of innings - number of not-outs).
For example, if s = ["81","99*","57","118*","36*","0","18","10","61*","0","83*","24","10*"]:
  • After their first innings, the batter has an average of 81
  • After their second, their average is 180 (81 + 99 runs, with still only one dismissal)
  • After their third, 118.5 (237 runs, two dismissals)
  • Fourth, 177.5 (355 runs, still two dismissals)
  • Fifth, 195.5 (391/2)
  • Sixth, 130.3 (391/3)
  • etc
Hence, their career maximum was after their fifth innings: 195.5.
MATLAB line plot of the cumulative total runs and total dismissals for the given example MATLAB line plot of the running average for the given example
maxavg = maxbattingavg(s)
maxavg =
195.5000
Similarly,
s = ["17*","12*","14","59","6*","0","18"];
maxavg = maxbattingavg(s)
maxavg =
54
MATLAB line plot of the cumulative total runs and total dismissals for the given second example MATLAB line plot of the running average for the given second example

Solution Stats

60.42% Correct | 39.58% Incorrect
Last Solution submitted on Aug 29, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers26

Suggested Problems

More from this Author22

Community Treasure Hunt

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

Start Hunting!