訂判斷及所需輸入
本例每區塊十單位,兩工作區用六及四。先合併是十單位、一區塊;各自取整則一加一、共兩區塊。同一用量,計費集合範圍不同,結果就不同。
| 情況 | 輸入 JSON |
|---|---|
| 原例測試 | {"scope":"per-workspace","workspaceUnits":[6,4]} |
跑所附比較
這裏先核報價如何計整個區塊,尚未分攤內部費用。計法、各區用量及區塊數一同留;把兩區塊分到部門,不會令帳單變一區塊。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"scope":"per-workspace","blocks":1} |
| 所需路徑 | {"scope":"per-workspace","blocks":2} |
import assert from 'node:assert/strict';
const smallWhole=(x,max)=>Number.isSafeInteger(x)&&x>=0&&x<=max;
const run=function run(i,corrected=true){
if(!['pooled','per-workspace'].includes(i.scope)||!i.workspaceUnits.every(x=>smallWhole(x,1000000)))return {held:true};
const pooled=Math.ceil(i.workspaceUnits.reduce((a,b)=>a+b,0)/10);
const separate=i.workspaceUnits.reduce((a,b)=>a+Math.ceil(b/10),0);
return {scope:i.scope,blocks:corrected?(i.scope==='pooled'?pooled:separate):pooled};
};
const cases=[{"input":{"scope":"per-workspace","workspaceUnits":[6,4]},"corrected":false,"expected":{"scope":"per-workspace","blocks":1}},{"input":{"scope":"per-workspace","workspaceUnits":[6,4]},"corrected":true,"expected":{"scope":"per-workspace","blocks":2}},{"input":{"scope":"pooled","workspaceUnits":[6,4]},"corrected":true,"expected":{"scope":"pooled","blocks":1}},{"input":{"scope":"per-workspace","workspaceUnits":[0,0]},"corrected":true,"expected":{"scope":"per-workspace","blocks":0}},{"input":{"scope":null,"workspaceUnits":[6,4]},"corrected":true,"expected":{"held":true}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
按邊界依據作判斷
明訂合併就一區塊;本例兩區都未用,按零用量規則是零。集合範圍未知便暫停。
- 核是否先合併
- 在該範圍取整
- 保留真正計費區塊數
未訂合併規則就停,不能靠平均用量替代。
| 條件 | 輸入 JSON | 預期輸出 JSON |
|---|---|---|
| 明訂先合併 | {"scope":"pooled","workspaceUnits":[6,4]} | {"scope":"pooled","blocks":1} |
| 兩區都未用 | {"scope":"per-workspace","workspaceUnits":[0,0]} | {"scope":"per-workspace","blocks":0} |
| 集合範圍未訂 | {"scope":null,"workspaceUnits":[6,4]} | {"held":true} |
原例每工作區分開計,應填多少區塊?
查看參考答案
兩個。六取一區塊,四再取一區塊。一區塊是另一份已訂合併規則的結果。