Skip to content

Commit 07face9

Browse files
✨ feat: First draft for isPartition.
1 parent 0e4333c commit 07face9

File tree

5 files changed

+8375
-1
lines changed

5 files changed

+8375
-1
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
},
7878
"dependencies": {},
7979
"devDependencies": {
80+
"@aureooms/js-itertools": "^4.1.0",
8081
"@babel/cli": "7.11.6",
8182
"@babel/core": "7.11.6",
8283
"@babel/preset-env": "7.11.5",
@@ -101,7 +102,11 @@
101102
"lib"
102103
],
103104
"homepage": "https://aureooms.github.io/js-set-partition",
104-
"keywords": ["choose", "partition", "set"],
105+
"keywords": [
106+
"choose",
107+
"partition",
108+
"set"
109+
],
105110
"license": "AGPL-3.0",
106111
"main": "lib/index.js",
107112
"prettier": {

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import isPartition from './isPartition';
2+
3+
/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */
4+
export default {
5+
isPartition,
6+
};
7+
8+
export {isPartition};

src/isPartition.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const isPartition = (elements, partition) => {
2+
const notyetseen = new Set(elements);
3+
4+
for (const part of partition) {
5+
let partIsEmptySet = true;
6+
for (const element of part) {
7+
partIsEmptySet = false;
8+
if (notyetseen.has(element)) notyetseen.delete(element);
9+
else return false;
10+
}
11+
12+
if (partIsEmptySet) return false;
13+
}
14+
15+
return notyetseen.size === 0;
16+
};
17+
18+
export default isPartition;

test/src/isPartition.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from 'ava';
2+
3+
import {iter, range, list, map} from '@aureooms/js-itertools';
4+
5+
import {isPartition} from '../../src';
6+
7+
const macro = (t, n, partition, is) => {
8+
t.is(isPartition(range(n), partition), is, 'partition');
9+
t.is(isPartition(range(n), map(iter, partition)), is, 'map(iter, partition)');
10+
t.is(
11+
isPartition(range(n), list(map(iter, partition))),
12+
is,
13+
'list(map(iter, partition))',
14+
);
15+
t.is(
16+
isPartition(range(n), list(map((x) => x, partition))),
17+
is,
18+
'list(map(x => x, partition))',
19+
);
20+
};
21+
22+
macro.title = (title, n, partition, is) =>
23+
`isPartition(${n}, ${JSON.stringify(partition)} = ${is})`;
24+
25+
test(macro, 5, [[0, 1, 2, 3, 4]], true);
26+
test(macro, 5, [[0], [1], [2], [3], [4]], true);
27+
test(macro, 5, [[0, 1], [2], [3], [4]], true);
28+
test(macro, 5, [[2, 1], [3], [0, 4]], true);
29+
30+
test(macro, 5, [[], [0], [1], [2], [3], [4]], false);
31+
test(macro, 5, [[0], [0], [1], [2], [3], [4]], false);
32+
test(macro, 5, [[5], [0], [1], [2], [3], [4]], false);
33+
test(macro, 5, [[-1], [0], [1], [2], [3], [4]], false);
34+
test(macro, 5, [[2 ** 53 - 1], [0], [1], [2], [3], [4]], false);
35+
test(macro, 5, [[-(2 ** 53)], [0], [1], [2], [3], [4]], false);

0 commit comments

Comments
 (0)