mirror of
https://github.com/gchq/CyberChef.git
synced 2026-03-17 04:11:51 -07:00
Merge 90660fb41b into 4deaa0de20
This commit is contained in:
commit
5eb3e02e8d
3 changed files with 42 additions and 5 deletions
|
|
@ -108,14 +108,17 @@ export function mean(data) {
|
|||
* @returns {BigNumber}
|
||||
*/
|
||||
export function median(data) {
|
||||
if ((data.length % 2) === 0 && data.length > 0) {
|
||||
if (data.length > 0) {
|
||||
data.sort(function(a, b) {
|
||||
return a.minus(b);
|
||||
});
|
||||
const first = data[Math.floor(data.length / 2)];
|
||||
const second = data[Math.floor(data.length / 2) - 1];
|
||||
return mean([first, second]);
|
||||
} else {
|
||||
|
||||
if ((data.length % 2) === 0) {
|
||||
const first = data[Math.floor(data.length / 2)];
|
||||
const second = data[Math.floor(data.length / 2) - 1];
|
||||
return mean([first, second]);
|
||||
}
|
||||
|
||||
return data[Math.floor(data.length / 2)];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ import "./tests/LuhnChecksum.mjs";
|
|||
import "./tests/LZNT1Decompress.mjs";
|
||||
import "./tests/LZString.mjs";
|
||||
import "./tests/Magic.mjs";
|
||||
import "./tests/Median.mjs";
|
||||
import "./tests/Media.mjs";
|
||||
import "./tests/MIMEDecoding.mjs";
|
||||
import "./tests/Modhex.mjs";
|
||||
|
|
|
|||
33
tests/operations/tests/Median.mjs
Normal file
33
tests/operations/tests/Median.mjs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Median operation tests.
|
||||
*
|
||||
* @author copilot-swe-agent[bot]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
import TestRegister from "../../lib/TestRegister.mjs";
|
||||
|
||||
TestRegister.addTests([
|
||||
{
|
||||
name: "Median: odd-length input",
|
||||
input: "10 1 2",
|
||||
expectedOutput: "2",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Median",
|
||||
args: ["Space"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Median: even-length input",
|
||||
input: "10 1 2 5",
|
||||
expectedOutput: "3.5",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Median",
|
||||
args: ["Space"],
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
Loading…
Add table
Add a link
Reference in a new issue