Re: fixed list combinatorics



"Dan Klose" schreef:
Hi list,

I am having a bad day and would really like some help (the coffee
hasn't).

I have a list that looks like:
my @list = (1,2,3,4);

I would like to generate all patterns that follow:
1
2
3
4
12
123
23
34
234
1234


The list can be of any length and the next number in the list must be
the current number +1 ( i am not working with numbers - i think it is
easier to explain this way).

How do I do this? I did look at the Combinatorics module however it
does not impose fixed ordering as far as I can see.

Compare it to this list:

0000 .... X
0001 ...1
0010 ..2.
0011 ..21
0100 .3..
0101 .3.1 X
0110 .32.
0111 .321
1000 4...
1001 4..1 X
1010 4.2. X
1011 4.21 X
1100 43..
1101 43.1 X
1110 432.
1111 4321

It seems you want to filter out the all-empty and disconnected variants
(marked with X).

perl -wle'
print for grep /\S/ && !/\S\s+\S/,
map {
$_ = reverse sprintf("%b", $_);
tr/0/ /;
s/1/$-[0]+1/eg;
$_;
} 0..2**4-1
;
'
1
2
12
3
23
123
4
34
234
1234


Alternative:

$ perl -wle'
print for grep /\S/ && !/\S\s+\S/,
map {
($_ = reverse sprintf("%b", $_)) =~ tr/01/ 7/;
$_ &= "1234";
} 0..15
;
'
1
2
12
3
23
123
4
34
234
1234

--
Affijn, Ruud

"Gewoon is een tijger."

.



Relevant Pages

  • Re: Parsing text to array
    ... Which is entirely standard Perl. ... map() actually, and for grepthe above statement holds true. ... They are builtins that make new lists of given ... This is offset by the fundamental simplification that comes with working ...
    (comp.lang.perl.misc)
  • Re: Parsing text to array
    ... Which is entirely standard Perl. ... Not understanding map is nearly as bad ... as not understanding hashes: it is a fundamental part of Perl. ... map() actually, and for grepthe above statement holds true. ...
    (comp.lang.perl.misc)
  • Re: any pointers please? combine words script
    ... it is a very common perl idiom so use it. ... sf> ok...this is cute too, ... i wonder why so many perl newbies have trouble with map. ...
    (comp.lang.perl.misc)
  • Re: Style question: map versus foreach
    ... >> The only real difference between map and foreach is that map returns a ... don't create it in the first place and just use foreach. ... Which version of perl are you using? ... equivalent function exists that documents that there is no return (or ...
    (comp.lang.perl.misc)
  • Re: Is there arithmetic sequence represents?
    ... I prefer to be explicit. ... It is clear what you're shifting: an unqualified 'shift' shifts off the ... advice to use map for list ... encouraging people to learn to use Perl properly. ...
    (comp.lang.perl.misc)