in reply to Challenge: Generate fixed size combination across groups without duplicates
If I correctly understand what you want then the following does the trick:
#!/usr/lib/perl use strict; use warnings; my @groups; my $size; while (<DATA>) { chomp; my ($type, $value) = split /\s*=\s*/, $_, 2; next if ! defined $value; if ($type eq 'fixed_size') { $size = $value; next; } push @groups, [split (/ /, $value), undef]; } pickem ($size, [], @groups); sub pickem { my ($needed, $givenList, @options) = @_; my $list = shift @options; for my $item (@$list) { if (! defined $item) { pickem ($needed, $givenList, @options); next; } if ($needed == 1) { print "@$givenList $item\n"; next; } pickem ($needed - 1, [@$givenList, $item], @options); } } __DATA__ group_1 = A B C group_2 = 1 2 3 4 group_3 = yellow blue green group_4 = tiny small medium large gigantic fixed_size = 2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Challenge: Generate fixed size combination across groups without duplicates
by Limbic~Region (Chancellor) on Nov 16, 2011 at 04:30 UTC | |
by GrandFather (Saint) on Nov 16, 2011 at 05:17 UTC | |
by Limbic~Region (Chancellor) on Nov 16, 2011 at 14:31 UTC | |
by ww (Archbishop) on Nov 16, 2011 at 14:40 UTC |