From 83c89ad94046cca590d2f0851d2fde520459f277 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Fri, 6 May 2022 17:19:56 -0400 Subject: [PATCH] calculateNumericDpmShares: use sorted order --- common/calculate-dpm.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/common/calculate-dpm.ts b/common/calculate-dpm.ts index 4e8e3eeb..1d933408 100644 --- a/common/calculate-dpm.ts +++ b/common/calculate-dpm.ts @@ -107,14 +107,6 @@ export function calculateDpmShares( return Math.sqrt(bet ** 2 + shares ** 2 + c) - shares } -const zigZagOrder = (length: number) => { - const mid = Math.floor(length / 2) - - return _.range(0, mid) - .flatMap((i) => [i, length - i - 1]) - .concat(length % 2 === 0 ? [] : [mid]) -} - export function calculateNumericDpmShares( totalShares: { [outcome: string]: number @@ -125,7 +117,12 @@ export function calculateNumericDpmShares( totalShares = _.cloneDeep(totalShares) - for (let i of zigZagOrder(bets.length)) { + const order = _.sortBy( + bets.map(([, amount], i) => [amount, i]), + ([amount]) => amount + ).map(([, i]) => i) + + for (let i of order) { const [bucket, bet] = bets[i] shares[i] = calculateDpmShares(totalShares, bet, bucket) totalShares = addObjects(totalShares, { [bucket]: shares[i] })