0
\$\begingroup\$

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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What 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. \$\endgroup\$
    – isaacg
    Aug 11, 2015 at 9:45
  • \$\begingroup\$ @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 \$\endgroup\$
    – d.putto
    Aug 11, 2015 at 11:23
  • \$\begingroup\$ Sometimes this is not possible. What to do then? \$\endgroup\$ Aug 11, 2015 at 15:36
  • \$\begingroup\$ 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. \$\endgroup\$ Aug 11, 2015 at 15:37

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.