Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm not sure about your requirements. I wrote a solution that produces

[1,2,3] [3,4,7] [3,6,9]

This seems more consistent with your stated requirements than your example results. 4 & 8 are equally common -- they appear twice in the arrays. 4 occurs before 8 in $b, thus the elements appear in $b's order of insertion.

At any rate, my algorithm was:

  1. Traverse all the arrays, counting how many of each element appear.
  2. Sort the results in order of ascending frequency and assign it to @frequency.
  3. Take the last of these (i.e. most frequent, or tied for most frequent, and thus common to all) and assign it to $common.
  4. Die with an error message if $common < the number of arrays -- there wasn't really an element common to all.
  5. Start building the results for each array:
    1. First, assign $common.
    2. Then, for each element in @frequency, test if that element is present in the current array. If it is, assign it to the results for this array. Do this X-1 times (assigning $common took care of one element.)
    3. Sort this array's results.
  6. Push an arrayref for this array's results on your final results array.

Updated: my unstated assumption in the above was that that code would be in a subroutine to be called like so:

my ($a_, $b_, $c_) = mutually_exclusive($X, $a, $b, $c);

In reply to Re: Mutually Exclusive Elements by Zed_Lopez
in thread Mutually Exclusive Elements by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2023-09-25 03:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found