Skip to content

Commit

Permalink
fix(stats): fix mean calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
GabinL21 committed Aug 12, 2023
1 parent 1d00774 commit eeb32f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/stats/utils/stats_calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ class StatsCalculator {
}

static Stat computeMean(List<Solve> solves, [int? nbSolves]) {
nbSolves ??= solves.length;
if (solves.isEmpty) return MeanStat.empty(nbSolves);
if (solves.every((s) => s.dnf)) return MeanStat.dnf(nbSolves);
if (solves.isEmpty) return MeanStat.empty(nbSolves ?? solves.length);
if (solves.every((s) => s.dnf)) {
return MeanStat.dnf(nbSolves ?? solves.length);
}
final nonDnfSolves = solves.where((s) => !s.dnf);
if (nbSolves <= 0 || nbSolves > nonDnfSolves.length) {
if (nbSolves != null && (nbSolves <= 0 || nbSolves > nonDnfSolves.length)) {
return MeanStat.empty(nbSolves);
}
final times = _getTimes(nonDnfSolves.take(nbSolves));
final meanSolves =
nbSolves == null ? nonDnfSolves : nonDnfSolves.take(nbSolves);
final times = _getTimes(meanSolves);
final mean = times.average.round();
return MeanStat(mean, nbSolves);
return MeanStat(mean, nbSolves ??= nonDnfSolves.length);
}

static Stat computeAverage(List<Solve> solves, int nbSolves) {
Expand Down
7 changes: 7 additions & 0 deletions test/stats/utils/stats_calculator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ void main() {
});

test('filters DNF solves when not every solve is DNF', () {
final meanStat = StatsCalculator.computeMean(solvesWithDnf);
expect(meanStat, MeanStat(10000, 5));
});

test(
'filters DNF solves when not every solve is DNF '
'and nb solves is defined', () {
final meanStat = StatsCalculator.computeMean(solvesWithDnf, 5);
expect(meanStat, MeanStat(10000, 5));
});
Expand Down

0 comments on commit eeb32f6

Please sign in to comment.