Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { median_high } from 'locutus/python/statistics/median_high'.
Or with CommonJS: const { median_high } = require('locutus/python/statistics/median_high')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
median_high([1, 2, 3, 4])
3
2
median_high([1, 3, 5])
3
3
median_high(['d', 'c', 'b', 'a'])
'c'
Notes
Returns the higher of the two middle values for even-sized input.
typeStatisticsSortableInput = number | boolean | bigint | string
functionsortStatisticsValues(data: unknown, functionName: string): StatisticsSortableInput[] { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
if (data.length === 0) { return [] }
const values = data asStatisticsSortableInput[] const firstKind = getStatisticsSortKind(values[0], functionName) for (let index = 1; index < values.length; index += 1) { if (getStatisticsSortKind(values[index], functionName) !== firstKind) { thrownewTypeError(`${functionName}() requires data values that can be ordered together`) } }
functiontoNumericSortValue(value: StatisticsSortableInput): number { returntypeof value === 'boolean' ? (value ? 1 : 0) : Number(value) }
exportfunctionmedian_high(data) { // discuss at: https://locutus.io/python/statistics/median_high/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns the higher of the two middle values for even-sized input. // example 1: median_high([1, 2, 3, 4]) // returns 1: 3 // example 2: median_high([1, 3, 5]) // returns 2: 3 // example 3: median_high(['d', 'c', 'b', 'a']) // returns 3: 'c'
const values = sortStatisticsValues(data, 'median_high') if (values.length === 0) { thrownewError('no median for empty data') }
return values[Math.floor(values.length / 2)] }
functionsortStatisticsValues(data, functionName) { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
if (data.length === 0) { return [] }
const values = data const firstKind = getStatisticsSortKind(values[0], functionName) for (let index = 1; index < values.length; index += 1) { if (getStatisticsSortKind(values[index], functionName) !== firstKind) { thrownewTypeError(`${functionName}() requires data values that can be ordered together`) } }
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.