@gkucmierz/utils 1.28.3 → 1.28.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish.yml +15 -19
- package/README.md +55 -4
- package/package.json +2 -2
- package/src/SetCnt.mjs +52 -6
- package/src/Trie.mjs +35 -2
- package/src/base64.mjs +24 -0
- package/src/bijective-numeration.mjs +24 -0
- package/src/binary-search.mjs +25 -5
- package/src/copy-case.mjs +5 -5
- package/src/egcd.mjs +8 -0
- package/src/factors.mjs +6 -8
- package/src/gcd.mjs +8 -8
- package/src/get-type.mjs +6 -2
- package/src/gpn.mjs +14 -0
- package/src/heap.mjs +26 -0
- package/src/herons-formula.mjs +17 -0
- package/src/lcm.mjs +13 -0
- package/src/list-node.mjs +19 -0
- package/src/matrix.mjs +6 -3
- package/src/memoize.mjs +5 -5
- package/src/mod.mjs +8 -14
- package/src/phi.mjs +13 -0
- package/src/pow-mod.mjs +12 -12
- package/src/range-array.mjs +12 -0
- package/src/square-root.mjs +6 -8
- package/src/tonelli-shanks.mjs +7 -2
- package/package-lock.json +0 -1422
package/src/list-node.mjs
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Represents a node in a singly linked list.
|
|
5
|
+
*/
|
|
3
6
|
export class ListNode {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new ListNode.
|
|
9
|
+
* @param {*} [val=0] - The value of the node.
|
|
10
|
+
* @param {ListNode} [next=null] - The next node in the list.
|
|
11
|
+
*/
|
|
4
12
|
constructor(val = 0, next = null) {
|
|
5
13
|
this.val = val;
|
|
6
14
|
this.next = next;
|
|
7
15
|
}
|
|
8
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Converts the linked list to an array.
|
|
19
|
+
* Detects cycles and throws an error if one is found.
|
|
20
|
+
* @returns {Array} An array containing the values of the list nodes.
|
|
21
|
+
* @throws {Error} If a cyclic reference is detected.
|
|
22
|
+
*/
|
|
9
23
|
toArr() {
|
|
10
24
|
const ret = [];
|
|
11
25
|
let node = this;
|
|
@@ -19,6 +33,11 @@ export class ListNode {
|
|
|
19
33
|
return ret;
|
|
20
34
|
}
|
|
21
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Creates a linked list from an iterable.
|
|
38
|
+
* @param {Iterable} iter - The iterable to create the list from.
|
|
39
|
+
* @returns {ListNode|null} The head of the created linked list, or null if empty.
|
|
40
|
+
*/
|
|
22
41
|
static fromArr(iter) {
|
|
23
42
|
const head = new ListNode;
|
|
24
43
|
let node = head;
|
package/src/matrix.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Represents a 2D matrix as a flat 1D array using a Proxy.
|
|
5
|
+
* Allows accessing elements using a single index (row-major order).
|
|
6
|
+
* @param {Array<Array<*>>} matrix - The 2D matrix to wrap.
|
|
7
|
+
* @returns {Proxy} A proxy that behaves like a flat array.
|
|
8
|
+
*/
|
|
6
9
|
export const matrixAsArray = matrix => {
|
|
7
10
|
const [h, w] = [matrix.length, matrix[0].length];
|
|
8
11
|
return new Proxy([], {
|
package/src/memoize.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @param {Function}
|
|
7
|
-
* @
|
|
3
|
+
* Creates a memoized version of a function.
|
|
4
|
+
* Uses a tree structure to store results based on arguments.
|
|
5
|
+
* Compares arguments by reference.
|
|
6
|
+
* @param {Function} fn - The function to memoize.
|
|
7
|
+
* @returns {Function} The memoized function.
|
|
8
8
|
*/
|
|
9
9
|
export const memoize = fn => {
|
|
10
10
|
const maps = [];
|
package/src/mod.mjs
CHANGED
|
@@ -10,23 +10,17 @@ const getMod = ZERO => {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @
|
|
17
|
-
* @param {Number} Dividend
|
|
18
|
-
* @param {Number} Divisor
|
|
19
|
-
* @return {Number} Modulus
|
|
13
|
+
* Calculates the modulo of two numbers (Python-like behavior for negative numbers).
|
|
14
|
+
* @param {number} dividend - The dividend.
|
|
15
|
+
* @param {number} divisor - The divisor.
|
|
16
|
+
* @returns {number} The result of the modulo operation.
|
|
20
17
|
*/
|
|
21
18
|
export const mod = getMod(0);
|
|
22
19
|
|
|
23
20
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
28
|
-
* @param {BigInt} Dividend
|
|
29
|
-
* @param {BigInt} Divisor
|
|
30
|
-
* @return {BigInt} Modulus
|
|
21
|
+
* Calculates the modulo of two BigInts (Python-like behavior for negative numbers).
|
|
22
|
+
* @param {bigint} dividend - The dividend.
|
|
23
|
+
* @param {bigint} divisor - The divisor.
|
|
24
|
+
* @returns {bigint} The result of the modulo operation.
|
|
31
25
|
*/
|
|
32
26
|
export const modBI = getMod(0n);
|
package/src/phi.mjs
CHANGED
|
@@ -31,7 +31,20 @@ const getPhiEuler = factors => {
|
|
|
31
31
|
};
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Euler's Totient Function (Phi).
|
|
36
|
+
* Counts the positive integers less than or equal to n that are relatively prime to n.
|
|
37
|
+
* Uses Euler's product formula based on prime factorization.
|
|
38
|
+
* @param {number} n - The number to calculate phi for.
|
|
39
|
+
* @returns {number} The value of phi(n).
|
|
40
|
+
*/
|
|
34
41
|
export const phi = getPhiEuler(factors);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Euler's Totient Function (Phi) - BigInt version.
|
|
45
|
+
* @param {bigint} n - The BigInt to calculate phi for.
|
|
46
|
+
* @returns {bigint} The value of phi(n).
|
|
47
|
+
*/
|
|
35
48
|
export const phiBI = getPhiEuler(factorsBI);
|
|
36
49
|
|
|
37
50
|
// export const phi = getPhi(1, 2, gcd);
|
package/src/pow-mod.mjs
CHANGED
|
@@ -19,21 +19,21 @@ const getPowMod = (ZERO, ONE, TWO, floor) => {
|
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @param {
|
|
25
|
-
* @param {
|
|
26
|
-
* @param [
|
|
27
|
-
* @
|
|
22
|
+
* Calculates the power of a base to an exponent, optionally modulo a number.
|
|
23
|
+
* Similar to Python's pow(base, exponent, modulus).
|
|
24
|
+
* @param {number} base - The base number.
|
|
25
|
+
* @param {number} exponent - The exponent.
|
|
26
|
+
* @param {number} [modulus] - The optional modulus.
|
|
27
|
+
* @returns {number} The result of (base ** exponent) % modulus.
|
|
28
28
|
*/
|
|
29
29
|
export const powMod = getPowMod(0, 1, 2, n => Math.floor(n));
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @param {
|
|
35
|
-
* @param {
|
|
36
|
-
* @param [
|
|
37
|
-
* @
|
|
32
|
+
* Calculates the power of a base to an exponent, optionally modulo a BigInt.
|
|
33
|
+
* Similar to Python's pow(base, exponent, modulus).
|
|
34
|
+
* @param {bigint} base - The base BigInt.
|
|
35
|
+
* @param {bigint} exponent - The exponent BigInt.
|
|
36
|
+
* @param {bigint} [modulus] - The optional modulus BigInt.
|
|
37
|
+
* @returns {bigint} The result of (base ** exponent) % modulus.
|
|
38
38
|
*/
|
|
39
39
|
export const powModBI = getPowMod(0n, 1n, 2n, n => n);
|
package/src/range-array.mjs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
|
|
2
2
|
// https://www.codewars.com/kata/57d83dfc950d842dcb00005b/train/javascript
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Converts a list of ranges into a flat array of numbers.
|
|
6
|
+
* @param {Array<Array<number>>} ranges - An array of ranges, where each range is [min, max] or [min].
|
|
7
|
+
* @returns {Array<number>} An array containing all numbers in the specified ranges.
|
|
8
|
+
*/
|
|
4
9
|
export const range2array = ranges => {
|
|
5
10
|
return ranges.reduce((arr, range) => {
|
|
6
11
|
const min = range[0];
|
|
@@ -11,6 +16,13 @@ export const range2array = ranges => {
|
|
|
11
16
|
}, []);
|
|
12
17
|
};
|
|
13
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Converts a sorted array of numbers into a list of ranges.
|
|
21
|
+
* Consecutive numbers are grouped into a single range [min, max].
|
|
22
|
+
* Isolated numbers are represented as [num, num].
|
|
23
|
+
* @param {Array<number>} arr - The sorted array of numbers.
|
|
24
|
+
* @returns {Array<Array<number>>} An array of ranges.
|
|
25
|
+
*/
|
|
14
26
|
export const array2range = arr => {
|
|
15
27
|
const ranges = [];
|
|
16
28
|
let last = arr[0];
|
package/src/square-root.mjs
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @
|
|
6
|
-
* @
|
|
7
|
-
* @return {Number} result
|
|
4
|
+
* Calculates the square root of a number.
|
|
5
|
+
* @param {number} n - The number to calculate the square root of.
|
|
6
|
+
* @returns {number} The square root of n.
|
|
8
7
|
*/
|
|
9
8
|
export const squareRoot = n => n ** 0.5;
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
14
|
-
* @
|
|
15
|
-
* @return {BigInt} result
|
|
11
|
+
* Calculates the integer square root of a BigInt using Newton's method.
|
|
12
|
+
* @param {bigint} n - The BigInt to calculate the square root of.
|
|
13
|
+
* @returns {bigint} The integer square root of n.
|
|
16
14
|
*/
|
|
17
15
|
export const squareRootBI = n => {
|
|
18
16
|
if (n === 0n) return 0n;
|
package/src/tonelli-shanks.mjs
CHANGED
|
@@ -3,8 +3,13 @@ import {
|
|
|
3
3
|
powModBI,
|
|
4
4
|
} from './pow-mod.mjs';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
*
|
|
6
|
+
/**
|
|
7
|
+
* Computes the modular square root of n modulo p using the Tonelli-Shanks algorithm.
|
|
8
|
+
* Solves the congruence r^2 = n (mod p) where p is an odd prime.
|
|
9
|
+
* @param {bigint} n - The number to find the square root of.
|
|
10
|
+
* @param {bigint} p - The modulus (must be an odd prime).
|
|
11
|
+
* @returns {bigint} The square root r such that r^2 = n (mod p), or 0n if no solution exists.
|
|
12
|
+
*/
|
|
8
13
|
export const tonelliShanksBI = (n, p) => {
|
|
9
14
|
let s = 0n;
|
|
10
15
|
let q = p - 1n;
|