How to resolve the algorithm Set consolidation step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Set consolidation step by step in the zkl programming language

Table of Contents

Problem Statement

Given two sets of items then if any item is common to any set then the result of applying consolidation to those sets is a set of sets whose contents is: Given N sets of items where N>2 then the result is the same as repeatedly replacing all combinations of two sets by their consolidation until no further consolidation between set pairs is possible. If N<2 then consolidation has no strict meaning and the input can be returned.

See also

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Set consolidation step by step in the zkl programming language

Source code in the zkl programming language

fcn consolidate(sets){  // set are munged if they are read/write
   if(sets.len()<2) return(sets);
   r,r0 := List(List()),sets[0];
   foreach x in (consolidate(sets[1,*])){
      i,ni:=x.filter22(r0.holds); //-->(intersection, !intersection)
      if(i) r0=r0.extend(ni);
      else  r.append(x);
   }
   r[0]=r0;
   r
}

fcn prettize(sets){
   sets.apply("concat"," ").pump(String,"(%s),".fmt)[0,-1]
}

foreach sets in (T( 
  T(L("A","B")), 
  T(L("A","B"),L("C","D")), 
  T(L("A","B"),L("B","D")),
  T(L("A","B"),L("C","D"),L("D","B")),
  T(L("H","I","K"),L("A","B"),L("C","D"),L("D","B"),L("F","G","H")),
  T(L("A","H"),L("H","I","K"),L("A","B"),L("C","D"),L("D","B"),L("F","G","H")),
  T(L("H","I","K"),L("A","B"),L("C","D"),L("D","B"),L("F","G","H"), L("A","H")),
)){
   prettize(sets).print(" --> ");
   consolidate(sets) : prettize(_).println();
}

  

You may also check:How to resolve the algorithm Prime decomposition step by step in the Elm programming language
You may also check:How to resolve the algorithm Mertens function step by step in the BCPL programming language
You may also check:How to resolve the algorithm Sparkline in unicode step by step in the C programming language
You may also check:How to resolve the algorithm Send an unknown method call step by step in the C# programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the SenseTalk programming language