Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > finding every combination of n values at y positions

Reply
Thread Tools

finding every combination of n values at y positions

 
 
Steve (another one)
Guest
Posts: n/a
 
      07-02-2004
I have just wasted a crazy amount of time trying to populate an array
containing every combination of n values at y positions. It feels as
though it should be trivial and the sort of thing I do every day, but in
fact I could not find the simple solution that surely must exist.

Below is my best attempt - it will also explain the task if I haven't
been clear - can someone tell me if there is a simpler way of doing it.

Thanks

Steve

(Actually I am probably really hoping you will all say "cool, what a
brilliant way to do it", but I somehow doubt that will happen!)


#!/usr/bin/perl

## find all possible combinations of values at $number_of_position positions

use strict;

my $min_value = 1;
my $max_value = 4;
my $number_of_positions = 3;


my $individual = 0;
my $incrementing = 0;
my $dun;
my @data_set;
my @one_set;
for my $x (0..($number_of_positions-1)){push @one_set,$min_value}

################################################## #
sub increment{
if ($one_set[$incrementing] > $max_value){$incrementing++;
$one_set[$incrementing]++;
increment();
$incrementing--;

$one_set[$incrementing]=$min_value;
}
}
################################################## #

while (1){
push @{$data_set[$individual]}, @one_set;

# done yet ?
foreach (@one_set){ $dun = ($_ == $max_value); last unless $dun}
last if $dun;

$one_set[$incrementing]++;
$individual++;

increment();
}

# show array
$individual=0;
foreach (@data_set){ print "$individual - @{$_}\n"; $individual++ }







 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-02-2004
On Fri, 2 Jul 2004, Steve (another one) wrote:

> I have just wasted a crazy amount of time trying to populate an array
> containing every combination of n values at y positions. It feels as
> though it should be trivial and the sort of thing I do every day, but in
> fact I could not find the simple solution that surely must exist.


You might want to consult CPAN the next time you feel yourself spending a
crazy amount of time doing anything in Perl. Chances are, someone's
already done it.

You might also want to check the FAQ, which could possibly tell you if
someone's already done it, and if so, where to find their solution.

In this case,

perldoc -q permute

shows you first a straight Perl solution, which it itself claims is "very
inefficient". It then tells you about two modules on CPAN which are
faster. One requires a C compiler (Algoritm:ermute). One does not
(List:ermutor):
http://search.cpan.org/~phoenix/List...22/Permutor.pm

Note that this doesn't do *exactly* what you wanted, but it's so similar,
I'd think it'd be trivial to use or modify for your own goals.

Paul Lalli



 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      07-02-2004
"Steve (another one)" wrote:
>
> I have just wasted a crazy amount of time trying to populate an array
> containing every combination of n values at y positions. It feels as
> though it should be trivial and the sort of thing I do every day, but in
> fact I could not find the simple solution that surely must exist.
>
> Below is my best attempt - it will also explain the task if I haven't
> been clear - can someone tell me if there is a simpler way of doing it.
>
> [snip]


You could do it like this:

#!/usr/bin/perl
use warnings;
use strict;

my $min_value = 1;
my $max_value = 4;
my $number_of_positions = 3;

my $glob = ( '{' . join( ',', $min_value .. $max_value ) . '}' ) x $number_of_positions;

$individual = 0;
print $individual++, " - @$_\n" for map [ /./g ], glob $glob;

__END__




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Steve (another one)
Guest
Posts: n/a
 
      07-05-2004
John W. Krahn wrote:
> "Steve (another one)" wrote:
>
>>I have just wasted a crazy amount of time trying to populate an array
>>containing every combination of n values at y positions. It feels as
>>though it should be trivial and the sort of thing I do every day, but in
>>fact I could not find the simple solution that surely must exist.
>>
>>Below is my best attempt - it will also explain the task if I haven't
>>been clear - can someone tell me if there is a simpler way of doing it.
>>
>>[snip]

>
>
> You could do it like this:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $min_value = 1;
> my $max_value = 4;
> my $number_of_positions = 3;
>
> my $glob = ( '{' . join( ',', $min_value .. $max_value ) . '}' ) x $number_of_positions;
>
> $individual = 0;
> print $individual++, " - @$_\n" for map [ /./g ], glob $glob;
>
> __END__
>
>
>
>
> John

cool, what a brilliant way to do it

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-05-2004
Steve (another one) <> wrote in comp.lang.perl.misc:
> John W. Krahn wrote:
> > "Steve (another one)" wrote:
> >
> >>I have just wasted a crazy amount of time trying to populate an array
> >>containing every combination of n values at y positions. It feels as
> >>though it should be trivial and the sort of thing I do every day, but in
> >>fact I could not find the simple solution that surely must exist.
> >>
> >>Below is my best attempt - it will also explain the task if I haven't
> >>been clear - can someone tell me if there is a simpler way of doing it.
> >>
> >>[snip]

> >
> >
> > You could do it like this:
> >
> > #!/usr/bin/perl
> > use warnings;
> > use strict;
> >
> > my $min_value = 1;
> > my $max_value = 4;
> > my $number_of_positions = 3;
> >
> > my $glob = ( '{' . join( ',', $min_value .. $max_value ) . '}' ) x

> $number_of_positions;
> >
> > $individual = 0;
> > print $individual++, " - @$_\n" for map [ /./g ], glob $glob;
> >
> > __END__
> >
> >
> >
> >
> > John

> cool, what a brilliant way to do it


It can also be considered a counting problem.

Consider the number system whose base is $base = $max_value - $min_value + 1.
Enumerate all numbers with $number_of_positions digits in this system.
(There will be $base**$number_of_positions such numbers.) Increment each
digit individually by $min_value. That yields the required set of
combinations.

The glob is a clever hack that does the required counting. A more
direct way is this:

my @digits = ( $min_value) x $number_of_positions;
my $individual = 0;
while ( 1 ) {
print $individual++, " - @digits\n";
last unless grep $_ < $max_value, @digits;
for ( reverse @digits ) { # increment with carry
last if ++ $_ <= $max_value;
$_ = $min_value;
}
}

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unique combination of values from arrays Milo Thurston Ruby 10 12-18-2009 04:57 PM
Explorer.exe error report every time access every type of video me =?Utf-8?B?SXJ3YW5zeWFo?= ASP .Net 4 10-30-2007 07:49 AM
finding positions in a string bwv549 Ruby 7 07-24-2007 09:05 AM
fixed positions of images versus relative positions Knut Krueger HTML 2 05-21-2007 10:18 AM
every combination of letters roger Perl Misc 3 10-12-2003 12:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57