Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Matching permutations with regex

by tybalt89 (Monsignor)
on Nov 20, 2018 at 12:51 UTC ( [id://1226069]=note: print w/replies, xml ) Need Help??


in reply to Matching permutations with regex

#!/usr/bin/perl -l # https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographi +c_order use strict; use warnings; $_ = 'aabbcc'; 1 while print, s/.*\K # find the last (.) # char such that (.*) # there is a later (latest) (.)(??{$1 ge $3 and '(*F)'}) # char greater than it (.*) # and get rest # swap those two chars ( $1 & $3 ) # then reverse everything after the first swapped char / $3 . reverse $2.$1.$4 /xe

Replies are listed 'Best First'.
Re^2: Matching permutations with regex
by QM (Parson) on Nov 22, 2018 at 09:20 UTC
    OK. That's probably pretty fast too.

    But I think you misunderstood? I can generate them, but I wanted to _match_ any permutation of a given string.

    Update: Oh, I had the =~ backwards, and that led you astray.

    (Not shown in the OP is a programmatic way to generate the regex solution based on the input string, but that was left out for clarity.)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Like so ?

      #!/usr/bin/perl -l # https://perlmonks.org/?node_id=1226058 use strict; use warnings; $_ = 'aabbcc'; my $len = length; my $inner = join ',', split //, tr///csr; my $glob = "{$inner}" x $len; my $re = "^(?=.{$len}\$)"; while( /./ ) { my $count = s/$&//g; $re .= "(?=(?:.*$&){$count})"; } $_ =~ $re and print for glob $glob;

      Outputs the 90 valid permutations.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found