@gkucmierz/utils 1.28.3 → 1.28.8
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/README.md +55 -4
- package/package.json +31 -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/.github/workflows/deploy-docs.yml +0 -22
- package/.github/workflows/npm-publish.yml +0 -25
- package/.husky/pre-commit +0 -4
- package/jsdoc.json +0 -10
- package/package-lock.json +0 -1422
- package/scripts/generate_main.mjs +0 -43
- package/spec/SetCnt.spec.mjs +0 -58
- package/spec/Trie.spec.mjs +0 -69
- package/spec/base64.spec.mjs +0 -29
- package/spec/bijective-numeration.spec.mjs +0 -123
- package/spec/binary-search.spec.mjs +0 -91
- package/spec/copy-case.spec.mjs +0 -12
- package/spec/factors.spec.mjs +0 -35
- package/spec/gcd.spec.mjs +0 -27
- package/spec/get-type.spec.mjs +0 -75
- package/spec/gpn.spec.mjs +0 -35
- package/spec/heap.spec.mjs +0 -32
- package/spec/herons-formula.spec.mjs +0 -41
- package/spec/list-node.spec.mjs +0 -33
- package/spec/main.spec.mjs +0 -8
- package/spec/matrix.spec.mjs +0 -19
- package/spec/memoize.spec.mjs +0 -46
- package/spec/mod.spec.mjs +0 -39
- package/spec/phi.spec.mjs +0 -60
- package/spec/pow-mod.spec.mjs +0 -21
- package/spec/range-array.spec.mjs +0 -19
- package/spec/square-root.spec.mjs +0 -22
- package/spec/support/jasmine.json +0 -16
- package/spec/tonelli-shanks.spec.mjs +0 -15
package/README.md
CHANGED
|
@@ -1,11 +1,62 @@
|
|
|
1
|
+
# @gkucmierz/utils
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
A collection of useful utility functions and data structures for solving algorithmic tasks, competitive programming, and everyday development.
|
|
8
|
+
|
|
9
|
+
## 📦 Installation
|
|
3
10
|
|
|
4
11
|
```bash
|
|
5
|
-
npm
|
|
12
|
+
npm install @gkucmierz/utils
|
|
6
13
|
```
|
|
7
14
|
|
|
8
|
-
|
|
15
|
+
## 🚀 Features
|
|
16
|
+
|
|
17
|
+
This library provides a wide range of mathematical functions and data structures, including:
|
|
18
|
+
|
|
19
|
+
- **Data Structures**:
|
|
20
|
+
- `SetCnt`: A set-like structure with element counting.
|
|
21
|
+
- `Trie`: Efficient prefix tree implementation.
|
|
22
|
+
- `Heap`: Min-heap priority queue.
|
|
23
|
+
- `ListNode`: Linked list node implementation.
|
|
24
|
+
- `matrixAsArray`: 2D matrix representation as a flat array.
|
|
25
|
+
|
|
26
|
+
- **Number Theory**:
|
|
27
|
+
- `gcd`, `lcm`: Greatest Common Divisor and Least Common Multiple (supports BigInt).
|
|
28
|
+
- `factors`: Prime factorization.
|
|
29
|
+
- `phi`: Euler's totient function.
|
|
30
|
+
- `mod`, `powMod`: Modular arithmetic with Python-like behavior for negative numbers.
|
|
31
|
+
- `egcd`: Extended Euclidean Algorithm.
|
|
32
|
+
- `tonelliShanksBI`: Modular square root algorithm.
|
|
33
|
+
|
|
34
|
+
- **Sequences & Formulas**:
|
|
35
|
+
- `gpn`: Generalized Pentagonal Numbers.
|
|
36
|
+
- `heronsFormula`: Triangle area calculation.
|
|
37
|
+
- `squareRoot`: Integer square root using Newton's method.
|
|
38
|
+
|
|
39
|
+
- **String & Encoding**:
|
|
40
|
+
- `base64`: Base64 and Base64Url encoding/decoding.
|
|
41
|
+
- `copyCase`: Match case of a string to another.
|
|
42
|
+
- `bijectiveNumeration`: Bijective base-k numeration system.
|
|
43
|
+
|
|
44
|
+
- **Utilities**:
|
|
45
|
+
- `memoize`: Function memoization based on arguments.
|
|
46
|
+
- `binarySearch`: Various binary search implementations (exact, lower bound, upper bound).
|
|
47
|
+
- `range2array`, `array2range`: Convert between ranges and arrays.
|
|
48
|
+
- `getType`: Precise type checking.
|
|
49
|
+
|
|
50
|
+
## 📚 Documentation
|
|
51
|
+
|
|
52
|
+
Full documentation with examples is available at:
|
|
53
|
+
👉 **[https://gkucmierz.github.io/utils](https://gkucmierz.github.io/utils)**
|
|
54
|
+
|
|
55
|
+
## 🔗 Links
|
|
56
|
+
|
|
57
|
+
- **NPM Package**: [https://www.npmjs.com/package/@gkucmierz/utils](https://www.npmjs.com/package/@gkucmierz/utils)
|
|
58
|
+
- **GitHub Repository**: [https://github.com/gkucmierz/utils](https://github.com/gkucmierz/utils)
|
|
9
59
|
|
|
10
|
-
|
|
60
|
+
## 📄 License
|
|
11
61
|
|
|
62
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.28.
|
|
3
|
+
"version": "1.28.8",
|
|
4
4
|
"description": "Usefull functions for solving programming tasks",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"utils",
|
|
7
|
+
"algorithms",
|
|
8
|
+
"math",
|
|
9
|
+
"number-theory",
|
|
10
|
+
"gcd",
|
|
11
|
+
"lcm",
|
|
12
|
+
"factors",
|
|
13
|
+
"phi",
|
|
14
|
+
"mod",
|
|
15
|
+
"pow-mod",
|
|
16
|
+
"binary-search",
|
|
17
|
+
"heap",
|
|
18
|
+
"trie",
|
|
19
|
+
"base64",
|
|
20
|
+
"bijective",
|
|
21
|
+
"tonelli-shanks",
|
|
22
|
+
"square-root",
|
|
23
|
+
"memoize",
|
|
24
|
+
"range-array",
|
|
25
|
+
"javascript",
|
|
26
|
+
"node",
|
|
27
|
+
"competitive-programming",
|
|
28
|
+
"data-structures"
|
|
29
|
+
],
|
|
30
|
+
"files": [
|
|
31
|
+
"main.mjs",
|
|
32
|
+
"src/"
|
|
33
|
+
],
|
|
5
34
|
"main": "main.mjs",
|
|
6
35
|
"scripts": {
|
|
7
36
|
"test": "jasmine",
|
|
@@ -22,6 +51,6 @@
|
|
|
22
51
|
"husky": "^8.0.1",
|
|
23
52
|
"jasmine": "^4.4.0",
|
|
24
53
|
"jsdoc": "^4.0.4",
|
|
25
|
-
"nodemon": "^
|
|
54
|
+
"nodemon": "^3.1.14"
|
|
26
55
|
}
|
|
27
56
|
}
|
package/src/SetCnt.mjs
CHANGED
|
@@ -1,28 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// .cnt - get element occurrences
|
|
6
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Set-like data structure built using Map with elements counter.
|
|
3
|
+
* Allows adding multiple occurrences of the same element.
|
|
4
|
+
*/
|
|
7
5
|
export class SetCnt {
|
|
8
6
|
#map = new Map();
|
|
9
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new SetCnt instance.
|
|
10
|
+
* @param {Iterable} [iter] - An iterable object to initialize the set with.
|
|
11
|
+
*/
|
|
10
12
|
constructor(iter) {
|
|
11
13
|
for (const el of iter ?? []) {
|
|
12
14
|
this.add(el);
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Adds an element to the set.
|
|
20
|
+
* Increments the counter for the element.
|
|
21
|
+
* @param {*} el - The element to add.
|
|
22
|
+
* @returns {SetCnt} The SetCnt instance.
|
|
23
|
+
*/
|
|
16
24
|
add(el) {
|
|
17
25
|
const cnt = this.cnt(el);
|
|
18
26
|
this.#map.set(el, cnt + 1);
|
|
19
27
|
return this;
|
|
20
28
|
}
|
|
21
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Checks if an element exists in the set.
|
|
32
|
+
* @param {*} el - The element to check.
|
|
33
|
+
* @returns {boolean} True if the element exists, false otherwise.
|
|
34
|
+
*/
|
|
22
35
|
has(el) {
|
|
23
36
|
return this.#map.has(el);
|
|
24
37
|
}
|
|
25
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Removes one occurrence of an element from the set.
|
|
41
|
+
* If the count reaches 0, the element is removed completely.
|
|
42
|
+
* @param {*} el - The element to remove.
|
|
43
|
+
* @returns {boolean} True if the element was removed (or count decremented), false if it didn't exist.
|
|
44
|
+
*/
|
|
26
45
|
delete(el) {
|
|
27
46
|
if (!this.#map.has(el)) return false;
|
|
28
47
|
const cnt = this.cnt(el);
|
|
@@ -31,31 +50,58 @@ export class SetCnt {
|
|
|
31
50
|
return true;
|
|
32
51
|
}
|
|
33
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Removes all occurrences of an element from the set.
|
|
55
|
+
* @param {*} el - The element to remove.
|
|
56
|
+
* @returns {boolean} True if the element was removed, false otherwise.
|
|
57
|
+
*/
|
|
34
58
|
deleteAll(el) {
|
|
35
59
|
this.#map.delete(el);
|
|
36
60
|
return true;
|
|
37
61
|
}
|
|
38
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Gets the number of occurrences of an element.
|
|
65
|
+
* @param {*} el - The element to check.
|
|
66
|
+
* @returns {number} The count of the element.
|
|
67
|
+
*/
|
|
39
68
|
cnt(el) {
|
|
40
69
|
return this.#map.get(el) ?? 0;
|
|
41
70
|
}
|
|
42
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Returns an iterator over the elements in the set.
|
|
74
|
+
* Each element is yielded as many times as its count.
|
|
75
|
+
* @returns {Iterator} An iterator over the elements.
|
|
76
|
+
*/
|
|
43
77
|
*[Symbol.iterator]() {
|
|
44
78
|
for (const el of this.#map) {
|
|
45
79
|
yield el;
|
|
46
80
|
}
|
|
47
81
|
}
|
|
48
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Returns the SetCnt instance itself (for compatibility with Map-like interfaces).
|
|
85
|
+
* @returns {SetCnt} The SetCnt instance.
|
|
86
|
+
*/
|
|
49
87
|
entries() {
|
|
50
88
|
return this;
|
|
51
89
|
}
|
|
52
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Returns an iterator over the unique elements in the set.
|
|
93
|
+
* @returns {Iterator} An iterator over the unique elements.
|
|
94
|
+
*/
|
|
53
95
|
*values() {
|
|
54
96
|
for (const [el, cnt] of this.#map) {
|
|
55
97
|
yield el;
|
|
56
98
|
}
|
|
57
99
|
}
|
|
58
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Returns an iterator over the counts of the elements.
|
|
103
|
+
* @returns {Iterator} An iterator over the counts.
|
|
104
|
+
*/
|
|
59
105
|
*keys() {
|
|
60
106
|
for (const [el, cnt] of this.#map) {
|
|
61
107
|
yield cnt;
|
package/src/Trie.mjs
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Trie (prefix tree) data structure.
|
|
4
|
+
* @param {string[]} [words=[]] - Initial words to add to the Trie.
|
|
5
|
+
* @param {boolean} [strict=true] - If true, removing a word cleans up unused nodes.
|
|
6
|
+
* If false, removal is faster but may leave unused nodes.
|
|
7
|
+
* @returns {object} The Trie instance with methods.
|
|
8
|
+
*/
|
|
4
9
|
export const Trie = (words = [], strict = true) => {
|
|
5
10
|
const HAS = 0;
|
|
6
11
|
const MAP = 1;
|
|
7
12
|
const data = [false, new Map()];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Adds a word to the Trie.
|
|
16
|
+
* @param {string} word - The word to add.
|
|
17
|
+
* @returns {boolean} True if the word was added (didn't exist before), false otherwise.
|
|
18
|
+
*/
|
|
8
19
|
const add = word => {
|
|
9
20
|
let node = data;
|
|
10
21
|
for (let i = 0; i < word.length; ++i) {
|
|
@@ -34,6 +45,12 @@ export const Trie = (words = [], strict = true) => {
|
|
|
34
45
|
const findNode = word => {
|
|
35
46
|
return listNodes(word).at(-1);
|
|
36
47
|
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Removes a word from the Trie.
|
|
51
|
+
* @param {string} word - The word to remove.
|
|
52
|
+
* @returns {boolean} True if the word was removed, false if it didn't exist.
|
|
53
|
+
*/
|
|
37
54
|
const remove = word => {
|
|
38
55
|
const nodes = listNodes(word);
|
|
39
56
|
const rev = nodes.reverse();
|
|
@@ -58,7 +75,19 @@ export const Trie = (words = [], strict = true) => {
|
|
|
58
75
|
}
|
|
59
76
|
return removed;
|
|
60
77
|
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Checks if a word exists in the Trie.
|
|
81
|
+
* @param {string} word - The word to check.
|
|
82
|
+
* @returns {boolean} True if the word exists, false otherwise.
|
|
83
|
+
*/
|
|
61
84
|
const has = word => findNode(word)[HAS];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Returns all words in the Trie that start with the given prefix.
|
|
88
|
+
* @param {string} begin - The prefix to search for.
|
|
89
|
+
* @returns {string[]} An array of words starting with the prefix.
|
|
90
|
+
*/
|
|
62
91
|
const get = begin => {
|
|
63
92
|
const res = [];
|
|
64
93
|
const loop = (node, str = '') => {
|
|
@@ -73,6 +102,10 @@ export const Trie = (words = [], strict = true) => {
|
|
|
73
102
|
words.map(word => add(word));
|
|
74
103
|
return {
|
|
75
104
|
add, has, get, remove,
|
|
105
|
+
/**
|
|
106
|
+
* Returns the internal data structure of the Trie.
|
|
107
|
+
* @returns {Array} The root node of the Trie.
|
|
108
|
+
*/
|
|
76
109
|
getData: () => data,
|
|
77
110
|
};
|
|
78
111
|
};
|
package/src/base64.mjs
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* Encodes a string to Base64.
|
|
4
|
+
* Handles Unicode characters correctly.
|
|
5
|
+
* @param {string} string - The string to encode.
|
|
6
|
+
* @returns {string} The Base64 encoded string.
|
|
7
|
+
*/
|
|
2
8
|
export const toBase64 = string => {
|
|
3
9
|
const codeUnits = new Uint16Array(string.length);
|
|
4
10
|
for (let i = 0; i < codeUnits.length; i++) {
|
|
@@ -7,6 +13,12 @@ export const toBase64 = string => {
|
|
|
7
13
|
return btoa(String.fromCharCode(...new Uint8Array(codeUnits.buffer)));
|
|
8
14
|
};
|
|
9
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Decodes a Base64 string.
|
|
18
|
+
* Handles Unicode characters correctly.
|
|
19
|
+
* @param {string} encoded - The Base64 encoded string.
|
|
20
|
+
* @returns {string} The decoded string.
|
|
21
|
+
*/
|
|
10
22
|
export const fromBase64 = encoded => {
|
|
11
23
|
const binary = atob(encoded);
|
|
12
24
|
const bytes = new Uint8Array(binary.length);
|
|
@@ -20,6 +32,12 @@ const toUrl = {
|
|
|
20
32
|
'+': '-',
|
|
21
33
|
'/': '_',
|
|
22
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Encodes a string to Base64Url format.
|
|
37
|
+
* Replaces '+' with '-' and '/' with '_'.
|
|
38
|
+
* @param {string} string - The string to encode.
|
|
39
|
+
* @returns {string} The Base64Url encoded string.
|
|
40
|
+
*/
|
|
23
41
|
export const toBase64Url = string => {
|
|
24
42
|
return toBase64(string).replace(/[\+\/]/g, c => toUrl[c]);
|
|
25
43
|
};
|
|
@@ -28,6 +46,12 @@ const fromUrl = {
|
|
|
28
46
|
'-': '+',
|
|
29
47
|
'_': '/',
|
|
30
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Decodes a Base64Url string.
|
|
51
|
+
* Replaces '-' with '+' and '_' with '/'.
|
|
52
|
+
* @param {string} encoded - The Base64Url encoded string.
|
|
53
|
+
* @returns {string} The decoded string.
|
|
54
|
+
*/
|
|
31
55
|
export const fromBase64Url = encoded => {
|
|
32
56
|
return fromBase64(encoded.replace(/[\-\_]/g, c => fromUrl[c]));
|
|
33
57
|
};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* Converts a number to a bijective base-k string.
|
|
4
|
+
* @param {number} num - The number to convert.
|
|
5
|
+
* @param {string} [alpha='12'] - The alphabet to use for the conversion.
|
|
6
|
+
* @returns {string} The bijective base-k string representation.
|
|
7
|
+
*/
|
|
2
8
|
export const num2bijective = (num, alpha = '12') => {
|
|
3
9
|
const len = alpha.length;
|
|
4
10
|
let c = 0;
|
|
@@ -17,6 +23,12 @@ export const num2bijective = (num, alpha = '12') => {
|
|
|
17
23
|
return res.join('');
|
|
18
24
|
};
|
|
19
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Converts a bijective base-k string to a number.
|
|
28
|
+
* @param {string} str - The bijective base-k string to convert.
|
|
29
|
+
* @param {string} [alpha='12'] - The alphabet used for the conversion.
|
|
30
|
+
* @returns {number} The numeric value of the string.
|
|
31
|
+
*/
|
|
20
32
|
export const bijective2num = (str, alpha = '12') => {
|
|
21
33
|
const map = new Map([...alpha].map((c, i) => [c, i]));
|
|
22
34
|
let res = 0;
|
|
@@ -26,6 +38,12 @@ export const bijective2num = (str, alpha = '12') => {
|
|
|
26
38
|
}, 0);
|
|
27
39
|
};
|
|
28
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Converts a BigInt to a bijective base-k string.
|
|
43
|
+
* @param {bigint} num - The BigInt to convert.
|
|
44
|
+
* @param {string} [alpha='12'] - The alphabet to use for the conversion.
|
|
45
|
+
* @returns {string} The bijective base-k string representation.
|
|
46
|
+
*/
|
|
29
47
|
export const num2bijectiveBI = (num, alpha = '12') => {
|
|
30
48
|
const len = BigInt(alpha.length);
|
|
31
49
|
let c = 0n;
|
|
@@ -44,6 +62,12 @@ export const num2bijectiveBI = (num, alpha = '12') => {
|
|
|
44
62
|
return res.join('');
|
|
45
63
|
};
|
|
46
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Converts a bijective base-k string to a BigInt.
|
|
67
|
+
* @param {string} str - The bijective base-k string to convert.
|
|
68
|
+
* @param {string} [alpha='12'] - The alphabet used for the conversion.
|
|
69
|
+
* @returns {bigint} The BigInt value of the string.
|
|
70
|
+
*/
|
|
47
71
|
export const bijective2numBI = (str, alpha = '12') => {
|
|
48
72
|
const map = new Map([...alpha].map((c, i) => [c, BigInt(i)]));
|
|
49
73
|
let res = 0n;
|
package/src/binary-search.mjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Performs a binary search for an exact element in a sorted array.
|
|
4
|
+
* @param {Array<number>} arr - The sorted array to search in.
|
|
5
|
+
* @param {number} target - The element to search for.
|
|
6
|
+
* @returns {number} The index of the element if found, otherwise -1.
|
|
7
|
+
*/
|
|
4
8
|
export const binarySearchArr = (arr, target) => {
|
|
5
9
|
let [a, b] = [0, arr.length];
|
|
6
10
|
let lm;
|
|
@@ -20,7 +24,12 @@ export const binarySearchArr = (arr, target) => {
|
|
|
20
24
|
return -1;
|
|
21
25
|
};
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Performs a binary search for the largest element less than or equal to the target.
|
|
29
|
+
* @param {Array<number>} arr - The sorted array to search in.
|
|
30
|
+
* @param {number} target - The target value.
|
|
31
|
+
* @returns {number} The index of the found element.
|
|
32
|
+
*/
|
|
24
33
|
export const binarySearchLE = (arr, target) => {
|
|
25
34
|
let [a, b] = [0, arr.length];
|
|
26
35
|
let lm;
|
|
@@ -38,7 +47,12 @@ export const binarySearchLE = (arr, target) => {
|
|
|
38
47
|
return -1;
|
|
39
48
|
};
|
|
40
49
|
|
|
41
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Performs a binary search for the smallest element greater than or equal to the target.
|
|
52
|
+
* @param {Array<number>} arr - The sorted array to search in.
|
|
53
|
+
* @param {number} target - The target value.
|
|
54
|
+
* @returns {number} The index of the found element.
|
|
55
|
+
*/
|
|
42
56
|
export const binarySearchGE = (arr, target) => {
|
|
43
57
|
let [a, b] = [0, arr.length];
|
|
44
58
|
let lm;
|
|
@@ -56,7 +70,13 @@ export const binarySearchGE = (arr, target) => {
|
|
|
56
70
|
return 0;
|
|
57
71
|
};
|
|
58
72
|
|
|
59
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Performs a binary search for a range of elements inclusive of the target.
|
|
75
|
+
* Uses binarySearchGE and binarySearchLE.
|
|
76
|
+
* @param {Array<number>} arr - The sorted array to search in.
|
|
77
|
+
* @param {number} target - The target value.
|
|
78
|
+
* @returns {Array<number>} An array containing the start and end indices of the range [start, end].
|
|
79
|
+
*/
|
|
60
80
|
export const binarySearchRangeIncl = (arr, target) => {
|
|
61
81
|
return [
|
|
62
82
|
binarySearchGE(arr, target),
|
package/src/copy-case.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @param {string}
|
|
6
|
-
* @param {string} source
|
|
7
|
-
* @
|
|
3
|
+
* Copies the case pattern from one string to another.
|
|
4
|
+
* Handles lower case, upper case, and capitalized strings.
|
|
5
|
+
* @param {string} word - The string to change the case of.
|
|
6
|
+
* @param {string} from - The source string providing the case pattern.
|
|
7
|
+
* @returns {string} The converted string.
|
|
8
8
|
*/
|
|
9
9
|
export const copyCase = (word, from) => {
|
|
10
10
|
const isLower = w => w.toLowerCase() === w;
|
package/src/egcd.mjs
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* Extended Euclidean Algorithm.
|
|
4
|
+
* Computes the greatest common divisor (GCD) of a and b,
|
|
5
|
+
* and the coefficients of Bézout's identity.
|
|
6
|
+
* @param {number} a - The first number.
|
|
7
|
+
* @param {number} b - The second number.
|
|
8
|
+
* @returns {number[]} An array [gcd, x, y] such that ax + by = gcd.
|
|
9
|
+
*/
|
|
2
10
|
export const egcd = (a, b) => {
|
|
3
11
|
let [x, y] = [0, 1];
|
|
4
12
|
let [u, v] = [1, 0];
|
package/src/factors.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
6
|
-
* @return {Number} result
|
|
3
|
+
* Calculates the prime factorization of a number.
|
|
4
|
+
* @param {number} n - The number to factorize.
|
|
5
|
+
* @returns {number[]} An array of prime factors.
|
|
7
6
|
*/
|
|
8
7
|
export const factors = n => {
|
|
9
8
|
if (n < 2) return [];
|
|
@@ -22,10 +21,9 @@ export const factors = n => {
|
|
|
22
21
|
};
|
|
23
22
|
|
|
24
23
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @
|
|
27
|
-
* @
|
|
28
|
-
* @return {BigInt} result
|
|
24
|
+
* Calculates the prime factorization of a BigInt.
|
|
25
|
+
* @param {bigint} n - The BigInt to factorize.
|
|
26
|
+
* @returns {bigint[]} An array of prime factors as BigInts.
|
|
29
27
|
*/
|
|
30
28
|
export const factorsBI = n => {
|
|
31
29
|
if (n < 2n) return [];
|
package/src/gcd.mjs
CHANGED
|
@@ -16,17 +16,17 @@ const getGCD = ZERO => {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @
|
|
21
|
-
* @param {
|
|
22
|
-
* @
|
|
19
|
+
* Calculates the Greatest Common Divisor (GCD) of two numbers.
|
|
20
|
+
* @param {number} a - The first number.
|
|
21
|
+
* @param {number} b - The second number.
|
|
22
|
+
* @returns {number} The GCD of a and b.
|
|
23
23
|
*/
|
|
24
24
|
export const gcd = getGCD(0);
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
28
|
-
* @
|
|
29
|
-
* @param {
|
|
30
|
-
* @
|
|
27
|
+
* Calculates the Greatest Common Divisor (GCD) of two BigInts.
|
|
28
|
+
* @param {bigint} a - The first BigInt.
|
|
29
|
+
* @param {bigint} b - The second BigInt.
|
|
30
|
+
* @returns {bigint} The GCD of a and b.
|
|
31
31
|
*/
|
|
32
32
|
export const gcdBI = getGCD(0n);
|
package/src/get-type.mjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Returns the type of the value as a lowercase string.
|
|
4
|
+
* More specific than the typeof operator (e.g., distinguishes 'array', 'null', 'date').
|
|
5
|
+
* @param {*} val - The value to check.
|
|
6
|
+
* @returns {string} The type of the value.
|
|
7
|
+
*/
|
|
4
8
|
export const getType = val => {
|
|
5
9
|
const str = Object.prototype.toString.call(val);
|
|
6
10
|
return str.slice(8, -1).toLowerCase();
|
package/src/gpn.mjs
CHANGED
|
@@ -10,5 +10,19 @@ const getGpn = (zero, one, two, three) => {
|
|
|
10
10
|
};
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Generalized Pentagonal Numbers (GPN).
|
|
15
|
+
* Formula: P_k = k(3k - 1) / 2
|
|
16
|
+
* The sequence includes k = 0, 1, -1, 2, -2, 3, -3...
|
|
17
|
+
* https://oeis.org/A001318
|
|
18
|
+
* @param {number} n - The index of the sequence (0-based).
|
|
19
|
+
* @returns {number} The n-th generalized pentagonal number.
|
|
20
|
+
*/
|
|
13
21
|
export const gpn = getGpn(0, 1, 2, 3);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generalized Pentagonal Numbers (GPN) - BigInt version.
|
|
25
|
+
* @param {bigint} n - The index of the sequence (0-based).
|
|
26
|
+
* @returns {bigint} The n-th generalized pentagonal number.
|
|
27
|
+
*/
|
|
14
28
|
export const gpnBI = getGpn(0n, 1n, 2n, 3n);
|
package/src/heap.mjs
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Min Heap data structure.
|
|
4
|
+
* @param {Function} [valFn] - A function to extract the comparison value from an element.
|
|
5
|
+
* Defaults to identity (n => n).
|
|
6
|
+
* @returns {object} The Heap instance with methods.
|
|
7
|
+
*/
|
|
2
8
|
export const Heap = (valFn = n => n) => {
|
|
3
9
|
const arr = [-1];
|
|
4
10
|
|
|
@@ -14,7 +20,17 @@ export const Heap = (valFn = n => n) => {
|
|
|
14
20
|
};
|
|
15
21
|
|
|
16
22
|
return {
|
|
23
|
+
/**
|
|
24
|
+
* Adds an element to the heap.
|
|
25
|
+
* @param {*} el - The element to add.
|
|
26
|
+
* @returns {number} The new index of the added element.
|
|
27
|
+
*/
|
|
17
28
|
add: el => up(arr.push(el) - 1),
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Removes and returns the minimum element (root) from the heap.
|
|
32
|
+
* @returns {*} The minimum element.
|
|
33
|
+
*/
|
|
18
34
|
take: () => {
|
|
19
35
|
const len = arr.length;
|
|
20
36
|
if (len <= 1) return [][0];
|
|
@@ -40,7 +56,17 @@ export const Heap = (valFn = n => n) => {
|
|
|
40
56
|
}
|
|
41
57
|
return res;
|
|
42
58
|
},
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns the number of elements in the heap.
|
|
62
|
+
* @returns {number} The size of the heap.
|
|
63
|
+
*/
|
|
43
64
|
size: () => arr.length - 1,
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns the internal array representation of the heap (excluding the dummy first element).
|
|
68
|
+
* @returns {Array} The heap elements.
|
|
69
|
+
*/
|
|
44
70
|
data: () => arr.slice(1),
|
|
45
71
|
};
|
|
46
72
|
};
|
package/src/herons-formula.mjs
CHANGED
|
@@ -9,5 +9,22 @@ const getHeronsFormula = (four, sq) => (a, b, c) => {
|
|
|
9
9
|
return sq((a + b + c) * (-a + b + c) * (a - b + c) * (a + b - c)) / four;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Calculates the area of a triangle given the lengths of its three sides.
|
|
14
|
+
* Uses Heron's formula.
|
|
15
|
+
* @param {number} a - Length of side a.
|
|
16
|
+
* @param {number} b - Length of side b.
|
|
17
|
+
* @param {number} c - Length of side c.
|
|
18
|
+
* @returns {number} The area of the triangle.
|
|
19
|
+
*/
|
|
12
20
|
export const heronsFormula = getHeronsFormula(4, n => n ** 0.5);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Calculates the area of a triangle given the lengths of its three sides (BigInt version).
|
|
24
|
+
* Returns the integer part of the area (truncated).
|
|
25
|
+
* @param {bigint} a - Length of side a.
|
|
26
|
+
* @param {bigint} b - Length of side b.
|
|
27
|
+
* @param {bigint} c - Length of side c.
|
|
28
|
+
* @returns {bigint} The area of the triangle.
|
|
29
|
+
*/
|
|
13
30
|
export const heronsFormulaBI = getHeronsFormula(4n, squareRootBI);
|
package/src/lcm.mjs
CHANGED
|
@@ -10,5 +10,18 @@ const getLcm = gcd => {
|
|
|
10
10
|
return (a, b) => a * b / gcd(a, b);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Calculates the Least Common Multiple (LCM) of two numbers.
|
|
15
|
+
* @param {number} a - The first number.
|
|
16
|
+
* @param {number} b - The second number.
|
|
17
|
+
* @returns {number} The LCM of a and b.
|
|
18
|
+
*/
|
|
13
19
|
export const lcm = getLcm(gcd);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Calculates the Least Common Multiple (LCM) of two BigInts.
|
|
23
|
+
* @param {bigint} a - The first BigInt.
|
|
24
|
+
* @param {bigint} b - The second BigInt.
|
|
25
|
+
* @returns {bigint} The LCM of a and b.
|
|
26
|
+
*/
|
|
14
27
|
export const lcmBI = getLcm(gcdBI);
|