-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
110 lines (99 loc) · 2.56 KB
/
util.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const countNoDup = (str, s) => {
return str.split(s).length - 1;
};
const occurs = (str) => {
let dicts = {};
[...str].forEach((c) => {
dicts[c] = dicts[c] ?? 0;
dicts[c]++;
})
return dicts;
};
const array = (d, def = 0) => {
if (d.length > 1) {
let dim = d[0];
let rest = d.slice(1);
let newArray = [];
for (let i = 0; i < dim; i++) {
newArray.push(array(rest, def));
}
return newArray;
} else {
return Array(d[0]).fill(def);
}
};
const zip = (list1, list2) => {
if (list1.length <= list2.length) {
return list1.map((e, i) => [e, list2[i]]);
} else {
return list2.map((e, i) => [list1[i], e]);
}
};
const dict = (list1, list2) => {
return zip(list1, list2).reduce((m, [k, v]) => {
m[k] = v;
return m;
}, {});
};
const norm = (n) => {
if (n == 0)
return 0;
return n / Math.abs(n);
};
const print = (...inp) => {
inp.forEach((val) => {
if (typeof val === 'object' && val != null && !Array.isArray(val)) {
console.log("aa")
console.dir(val, { depth: null })
} else {
console.log(val)
}
})
};
const printTable = (table, suffix = "", n = null, m = null, a = 0, b = 0) => {
n = n ?? table.length;
m = m ?? table[0].length;
for (let i = a; i < n; i++) {
console.log(table[i].slice(b, m).join(suffix))
}
}
// inc = inclusive for en
const range = (st, en, step = 1, inc = false) => {
let extra = inc == true ? 1 : 0;
if (st < en)
return Array(Math.ceil((en - st + extra) / step)).fill().map((_, i) => st + step * i);
else
return Array(Math.ceil((st - en + extra) / step)).fill().map((_, i) => st + step * i * -1);
};
const isOrder = (...arr) => {
let order = true;
for (let i = 0; i < arr.length - 1; i++) {
if (i == arr.length - 2) {
order &= arr[i] < arr[i + 1]
} else {
order &= arr[i] <= arr[i + 1]
}
}
return order
}
const hash = (...params) => {
return params.join(",")
}
module.exports = {
countNoDup: countNoDup,
occurs: occurs,
array: array,
zip: zip,
dict: dict,
norm: norm,
print: print,
printTable: printTable,
range: range,
direct4: [[0, 1], [1, 0], [0, -1], [-1, 0]],
direct8: [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]],
ascIntCmp: (a, b) => a - b,
descIntCmp: (a, b) => b - a,
ord: (c) => c.charCodeAt(0),
isOrder: isOrder,
hash: hash
};