Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/lib/calculateBestAo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Solve } from "@/interfaces/Solve";
import { sort } from "fast-sort";
import calculateCurrentAo from "./calculateCurrentAo";

export default function calculateBestAo(solves: Solve[], ao: number) {
if (solves.length < ao) {
Expand All @@ -9,13 +10,8 @@ export default function calculateBestAo(solves: Solve[], ao: number) {
const averages: number[] = [];

for (let i = 0; i <= solves.length - ao; i++) {
const cubeAo = solves.slice(i, i + ao);
const sum = cubeAo.reduce(
(accumulator, currentValue) => accumulator + currentValue.time,
0
);
const average = sum / ao;
averages.push(average);
const currentAo = calculateCurrentAo(solves.slice(i, i + ao), ao);
averages.push(currentAo);
}

const averagesSorted = sort(averages).asc();
Expand Down
12 changes: 10 additions & 2 deletions src/lib/calculateCurrentAo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Solve } from "@/interfaces/Solve";
import { sort } from "fast-sort";

export default function calculateCurrentAo(solves: Solve[], ao: number) {
let result = 0;
Expand All @@ -7,10 +8,17 @@ export default function calculateCurrentAo(solves: Solve[], ao: number) {
}

const cubeAo = solves.slice(0, ao);
const sum = cubeAo.reduce(
// sort by fastest solve time
const sortedSolves = sort(cubeAo).asc((u) => u.time);
// remove best and worst time from array
const trimmedSolves = sortedSolves.slice(1, ao - 1);

const sum = trimmedSolves.reduce(
(accumulator, currentValue) => accumulator + currentValue.time,
0
);
result = sum / ao;

// 2 balance the removed solves (best - worst)
result = sum / (ao - 2);
return result;
}
4 changes: 2 additions & 2 deletions src/lib/getDeviation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Solve } from "@/interfaces/Solve";

export default function getDeviation(solves: Solve[]) {
if (solves.length < 1) return 0;
if (solves.length < 2) return 0; // standard deviation require min 2 values
const n = solves.length;
const totalSolves = solves.reduce(
(acumulador, solve) => acumulador + solve.time,
Expand All @@ -14,7 +14,7 @@ export default function getDeviation(solves: Solve[]) {
return acumulador + difference ** 2;
}, 0);

const desvStandard = Math.sqrt(diff / n);
const desvStandard = Math.sqrt(diff / (n - 1));

return desvStandard;
}