-
Notifications
You must be signed in to change notification settings - Fork 15
/
helpers.js
86 lines (69 loc) · 1.78 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
const range = require('node-range');
const pad = require('pad');
const helpers = {};
Array.prototype.trim = function(proc) {
const newArr = [];
for (let i = 0; i < this.length * proc; i++) {
newArr.push(this[i]);
}
return newArr;
};
helpers.std = (arr)=> {
arr = arr.sort();
let left = arr.trim(0.5);
let right = arr.reverse().trim(0.5);
const leftCnt = left.length;
const rightCnt = right.length;
const leftMean = helpers.avg(left);
const rightMean = helpers.avg(right);
left = left.map(val => Math.pow(Math.abs(leftMean - val), 2));
right = right.map(val => Math.pow(Math.abs(rightMean - val), 2));
left = helpers.sum(left) / leftCnt;
right = helpers.sum(right) / rightCnt;
left = Math.sqrt(left);
right = Math.sqrt(right);
return {
left,
right,
}
};
helpers.max = (arr) => {
let max = 0;
for (let i = 0; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
};
helpers.min = (arr) => {
let min = Infinity;
for (let i = 0; i < arr.length; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
return min;
};
helpers.avg = (arr) => {
return helpers.sum(arr) / arr.length;
};
helpers.sum = (arr) => {
return arr.reduce((sum, val) => sum + val);
};
helpers.outputTime = (arr, proc) => {
arr = arr.trim(proc);
const std = helpers.std(arr);
const str = [
'time @ ', pad(3, proc * 100), '%', '|',
'min:', helpers.min(arr) | 0,
'left std:', std.left | 0,
'mean:', helpers.avg(arr) | 0,
'right std:', std.right | 0, '|',
'max:', helpers.max(arr) | 0
].join(' ');
console.log(str);
return str;
};
module.exports = helpers;