Perl: the Markov chain saw PerlMonks

### Re^2: x objects in y containers where all objects are used

by jandrew (Chaplain)
 on Nov 08, 2009 at 04:55 UTC ( #805725=note: print w/replies, xml ) Need Help??

I was intrigued and found Math::Fleximal and leveraged it into the following code based on MidLifeXis proposed solution.

```#! C:/Perl/bin/perl
### Test for x objects in y containers where all objects are used
### Treats the containers y as n in a base n system and the objects x
+as the digits
use strict;
use warnings;
use Math::Fleximal;
use YAML;

\$| = 1;
my (
\$Containers,
\$ObjectCount,
\$ContArrayRef,
)   = ();

#### Get the container count
while ( !\$Containers ) {
print "Please enter a positive number of Containers ";
\$Containers = <>;
chomp \$Containers;
### \$Containers
if ( \$Containers ) {
push @\$ContArrayRef, "WBox\$_" for (1..\$Containers);
### \$ContArrayRef
}
}
#### Get the object count
my \$CorrectObjectCount = 0;
while (     \$CorrectObjectCount == 0
or  !\$ObjectCount           ) {
print "Please enter the number of Objects ";
\$ObjectCount = <>;
chomp \$ObjectCount;
if (\$ObjectCount >= \$Containers) {
\$CorrectObjectCount = 1;
### \$ObjectCount
} else {
print "You have not chosen enough objects to fill all containe
+rs\n";
}
}

#### Iterate through all possible combinations of all digits
#### Start at 0
my \$CountFleximal   = Math::Fleximal->new( \$ContArrayRef->[0],  \$ContA
+rrayRef );
#### Fleck value one
my \$OneFleximal     = \$CountFleximal->one();
#### \$OneFleximal
#### build the Max Fleck for iteration
my \$MaxFlex;
\$MaxFlex .= \$ContArrayRef->[\$Containers - 1] for (1..\$ObjectCount);
### \$MaxFlex
my \$MaxFleximal     = Math::Fleximal->new( \$MaxFlex,    \$ContArrayRef
+);

#### Iterate through all possiblities
while ( \$CountFleximal->cmp(\$MaxFleximal) < 1) {
#### sprintif for Fleximal to fill all digits
my \$SprintfFleximal;
my @FleximalDigits = split "W", \$CountFleximal->to_str;
#### @FleximalDigits
for (1..\$ObjectCount) {
my \$NextFleck = pop @FleximalDigits;
\$NextFleck ||= \$ContArrayRef->[0] ;
\$NextFleck =~ s/W(.+)/\$1/;
\$SprintfFleximal =
( \$SprintfFleximal )
? \$NextFleck . \$SprintfFleximal :
\$NextFleck ;
}
### \$SprintfFleximal
### Check that all containers have objects
my \$test = 1;
for my \$i (1..\$Containers) {
if ( \$SprintfFleximal !~ /Box\$i/ ) {
\$test = 0 ;
### Box not represented: \$i
}
}
if ( \$test == 1 ) {
my @ContainerOut = split "Box", \$SprintfFleximal;
shift @ContainerOut;
### @ContainerOut
for my \$object (0..\$ObjectCount-1) {
+nerOut[\$object]}}, \$object +1;
}
}
\$CountFleximal = \$CountFleximal->add( \$OneFleximal );#++ routine
}
1;

Replies are listed 'Best First'.
Re^3: x objects in y containers where all objects are used
by MidLifeXis (Monsignor) on Nov 09, 2009 at 15:08 UTC

Ok, not quite what I understood. My understanding of the problem were that empty bins were allowed.

In that case, my suggestion does nothing to make the solution easier, except that given X > Y, Yx << Xy (thanks Corion and moritz for the confirmation).

Ok, given the numbers we are talking about, I guess that it could make it simpler.

--MidLifeXis

Create A New User
Node Status?
node history
Node Type: note [id://805725]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-04-23 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (85 votes). Check out past polls.

Notices?