Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I've been chewing on this problem for a day now. This is clearly problem with two logical parts. Part 1 is to generate all legal mappings of N columns of data into M buckets, given the constraints that no bucket can be empty, and that the columns need to stay ordered. The second part is apply these mappings to the input data, and select a mapping that yields a "best fit."

I focused on the first part, looking for a quicker, simpler solution. I think I have one. Here it is. Given a number of columns and a number of buckets, the code below calculates all legal mappings of columns to buckets, and returns these in a hash, where the key is a printable string, and the value is an anonymous array.

{ # map columns to buckets. key is string, value is anonymous array. my %c2bMap; sub c2bMappings { my($buckets, $columns) = @_; die "bogus args" unless $buckets > 1 && $columns > $buckets; %c2bMap = (); _genFrom(0, (0) x ($columns - $buckets), 1 .. ($buckets - 1)); return \%c2bMap; } sub _genFrom { my @c2bMap = @_; return if exists $c2bMap{"@c2bMap"}; print "@c2bMap\n"; #DEBUG $c2bMap{"@c2bMap"} = \@c2bMap; foreach my $i ( 2 .. $#c2bMap ) { my $n = $c2bMap[$i] - 1; if ( $c2bMap[$i - 2] == $n && $c2bMap[$i - 1] == $n ) { local $c2bMap[$i-1] = $c2bMap[$i]; _genFrom(@c2bMap); } } } } c2bMappings(4,6); __END__ 0 0 0 1 2 3 0 0 1 1 2 3 0 1 1 1 2 3 0 1 1 2 2 3 0 1 2 2 2 3 0 1 2 2 3 3 0 1 2 3 3 3 0 1 1 2 3 3 0 0 1 2 2 3 0 0 1 2 3 3
Recursion only happens with valid mappings. Note the selective localization of an element of the array that the code is about to recurse on.


In reply to Re: Puzzle: need a more general algorithm by dws
in thread Puzzle: need a more general algorithm by Ovid

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-19 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found