Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Combinations with constraints

by tybalt89 (Monsignor)
on Jun 28, 2022 at 21:02 UTC ( [id://11145161]=note: print w/replies, xml ) Need Help??


in reply to Combinations with constraints

TMTOWTDI

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11145154 use warnings; use ntheory qw( forcomb ); use List::Util qw( all ); my $firstLetter = 'c'; my $str = 'a?1,a?2,a%3,b?2,b?3,b%5,c%4,c%5,c?6,d%2'; my $subset = 'a?,b%,c?'; my @str = sort { $b =~ $firstLetter <=> $a =~ $firstLetter or # sort o +nly once $b =~ tr/0-9//cdr <=> $a =~ tr/0-9//cdr } split /,/, $str; forcomb { my $comb = join ',', @str[@_]; $comb =~ /\b(\d+)\b.*\b\1\b/ or # duplicate number all { $comb =~ /\Q$_/ } split /,/, $subset and # each subset print "$comb\n"; } scalar @str, 4;

Outputs:

c?6,c%4,b%5,a?2 c?6,c%4,b%5,a?1 c?6,b%5,a%3,a?2 c?6,b%5,a%3,a?1 c?6,b%5,b?3,a?2 c?6,b%5,b?3,a?1 c?6,b%5,a?2,a?1 c?6,b%5,b?2,a?1 c?6,b%5,d%2,a?1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-25 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found