join lists by common elements

-1

You have N lists

A = [5, 6, 10, 15]
B = [3, 0, 7, 4]
C = [49, 59, 29]
D = [19, 29, 39]
E = [9, 15, 3, 10]

You have to combine them such that: Two lists which have a common elements they should be next to each other. In this case answer is:

A,E,B,C,D
[5, 6, 10, 15, 9, 3, 0, 7, 4, 49, 59, 29, 19, 39]

E comes after A as they have 2 common elements. After E the B comes as they have common elements. B do not have common elements with C or D so anything can come (C or D), lets say C comes. Now C should be followed by D as they have common elements.

Note: This is also acceptable solution

B,E,A,C,D
C,D,A,E,B
.... 

Thus the main condition is if two lists have common elements they should be next to each other.

Note 2: For simplicity we assume a list can have common elements with maximum 2 other lists. However there can be list which have no common elements with any other list.

Note 3: Output format : preferably both, the name of list and the number sequence.

d.putto

Posted 2015-08-11T09:37:19.037

Reputation: 107

Question was closed 2015-08-11T19:52:35.220

1What exactly are the input and output formats? Is the output A,E,B,C,D or the list of numbers? is it always a single letter identifier before the equals? Please clarify. – isaacg – 2015-08-11T09:45:06.363

@isaacg output format can be A,E,B,C,D . Does the identifier of list matter !! If so yes it can be more then one letter – d.putto – 2015-08-11T11:23:48.510

Sometimes this is not possible. What to do then? – proud haskeller – 2015-08-11T15:36:56.547

I think you'll have to specify if the number sequence is required or not. Or give a bonus for outputting the number sequence. For a code golf contest, "preferably" is too vague. Also, if the number sequence is involved, I don't see a clear definition of what it is. I think I understand it based on the example (common elements are only listed once, between the two lists?), but a more formal definition would be useful. But if it's purely optional, you might just as well remove it completely. – Reto Koradi – 2015-08-11T15:37:59.040

Answers

1

CJam, 47 44 bytes

q~]e!{_2ew[{~_,@@\-,-}/]:+_W>{:W;:L}&}/];L:p

Explanation:

                                             e# L = [], W = -1
q~                                           e# Evaluate input
  ]                                          e# Wrap in array
   e!                                        e# Push all unique permutations
     {                               }/      e# For each permutation:
      _2ew                                   e#   Push all overlapping slices of length 2
           {         }/                      e#   For each slice:
            ~_,@@\-,-                        e#     Push number of common elements
          [            ]:+                   e#   Sum the number of common elements from all the slices
                          _W>{     }&        e#   If sum > W:
                              :W;            e#     Assign sum to W and pop off the stack
                                 :L          e#     Assign the permutation to L
                                       ];    e# Clear the stack
                                         L:p e# Print each list in L

Try it online.

Andrea Biondo

Posted 2015-08-11T09:37:19.037

Reputation: 1 452