Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

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


in reply to Re: x objects in y containers where all objects are used
in thread x objects in y containers where all objects are used

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 Smart::Comments '###';#, '####' use YAML; $| = 1; my ( $Containers, $ObjectCount, $ContArrayRef, $AnswerHashRef ) = (); #### 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 $AnswerHashRef->{'boxes'} = $Containers; } } #### 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 $AnswerHashRef->{'objects'} = $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 } } ### Load the answer to the output if valid if ( $test == 1 ) { my @ContainerOut = split "Box", $SprintfFleximal; shift @ContainerOut; ### @ContainerOut for my $object (0..$ObjectCount-1) { push @{$AnswerHashRef->{$SprintfFleximal}->{"Box". $Contai +nerOut[$object]}}, $object +1; } } $CountFleximal = $CountFleximal->add( $OneFleximal );#++ routine } ### $AnswerHashRef YAML::DumpFile ( 'Answer.txt', $AnswerHashRef ); 1;


Comment on Re^2: x objects in y containers where all objects are used
Download Code
Re^3: x objects in y containers where all objects are used
by MidLifeXis (Prior) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://805725]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-28 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls