Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const isSubset = function (A: Array<string> | Set<string>, B: Set<string>): boolean {
- return !!([...A].reduce((acc, curr) => acc && B.has(curr), true));
- }
- const isEqual = function (A: Array<string> | Set<string>, B: Array<string> | Set<string>): boolean {
- const _A = new Set(A);
- const _B = new Set(B);
- return isSubset(_A, _B) && isSubset(_B, _A);
- }
- const prefixed = function (subset: Set<string> | Array<string>, available: Array<Set<string>>): Array<Set<string>>{
- return available.filter(_ => isSubset(subset, _));
- }
- // Returns null if no perfect match was found
- // Retuns the perfect match as array otherwise
- const exactMatch = function (subset: Set<string> | Array<string>, matching: Array<Set<string>>): Array<string> | null {
- const match = matching.find(_ => isEqual(_, subset));
- return match ? [...match] : null;
- }
- let A = [
- '1', '2', '3'
- ];
- let B = [
- new Set([
- '2', '3'
- ]),
- new Set([
- '1', '2', '3', '4'
- ]),
- new Set([
- '1', '2', '5', '3'
- ]),
- new Set([
- '4', '5', '6', '7'
- ])
- ];
- const exSet = new Set([1,2,3,4]);
- console.log('Mo m\'array', Array.isArray([...exSet]));
- console.log("fochetta", isSubset(A, B[0]));
- console.log(prefixed(A, B));
- const outcome = exactMatch(A, prefixed(A, B));
- console.log("outcome:", outcome);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement