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

comment on

( [id://3333]=superdoc: 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 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;

In reply to Re^2: x objects in y containers where all objects are used by jandrew
in thread x objects in y containers where all objects are used by Ectaris

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 pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found