Advertisement
informaticage

Tricheco patate e cozze

Aug 3rd, 2021
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const isSubset = function (A: Array<string> | Set<string>, B: Set<string>): boolean {
  2.     return !!([...A].reduce((acc, curr) => acc && B.has(curr), true));
  3. }
  4.  
  5. const isEqual = function (A: Array<string> | Set<string>, B: Array<string> | Set<string>): boolean {
  6.     const _A = new Set(A);
  7.     const _B = new Set(B);
  8.  
  9.     return isSubset(_A, _B) && isSubset(_B, _A);
  10. }
  11.  
  12. const prefixed = function (subset: Set<string> | Array<string>, available: Array<Set<string>>): Array<Set<string>>{
  13.     return available.filter(_ => isSubset(subset, _));
  14. }
  15.  
  16. // Returns null if no perfect match was found
  17. // Retuns the perfect match  as array otherwise
  18. const exactMatch = function (subset: Set<string> | Array<string>, matching: Array<Set<string>>): Array<string> | null {
  19.     const match = matching.find(_ => isEqual(_, subset));
  20.     return match ? [...match] : null;
  21. }
  22.  
  23. let A = [
  24.     '1', '2', '3'
  25. ];
  26.  
  27. let B = [
  28.     new Set([
  29.         '2', '3'
  30.     ]),
  31.     new Set([
  32.         '1', '2', '3', '4'
  33.     ]),
  34.     new Set([
  35.         '1', '2', '5', '3'
  36.     ]),
  37.     new Set([
  38.         '4', '5', '6', '7'
  39.     ])
  40. ];
  41.  
  42. const exSet = new Set([1,2,3,4]);
  43. console.log('Mo m\'array', Array.isArray([...exSet]));
  44.  
  45. console.log("fochetta", isSubset(A, B[0]));
  46. console.log(prefixed(A, B));
  47. const outcome = exactMatch(A, prefixed(A, B));
  48. console.log("outcome:", outcome);
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement