Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Challenge: Generate a glob patterns from a word list

by LanX (Saint)
on May 05, 2021 at 18:36 UTC ( [id://11132094]=note: print w/replies, xml ) Need Help??


in reply to Challenge: Generate a glob patterns from a word list

> Find the shortest glob expression ...

So what's your metric for "shortest"?

String length doesn't equal compression.

use v5.12; use warnings; use Data::Dump; use Test::More; test ( [ <{b,{c,d}e}> ], [ <{b,ce,de}> ], ); test ( [ <{c,d}e> ], [ <{ce,de}> ], ); sub test { my @g = @_; for my $i (0..$#g) { for my $j ($i+1..$#g) { is_deeply($g[$i],$g[$j],"$i,$j"); } } ddx @g; } done_testing;

ok 1 - 0,1 # challenge_choroba_globs.pl:26: (["b", "ce", "de"], ["b", "ce", "de"] +) ok 2 - 0,1 # challenge_choroba_globs.pl:26: (["ce", "de"], ["ce", "de"]) 1..2

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Challenge: Generate a glob patterns from a word list
by choroba (Cardinal) on May 05, 2021 at 18:45 UTC
    OP updated. Don't count the curlies and commas.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      furthermore, in your example the elements from the "alphabet" always only appear once

      'a{b,c}d{e,fg{,h,ij}k}l{,m{n,o}}';

      I suppose that is not a given, but this might guaranty a unique solution.

      OTOH if characters can be repeated I'd bet that multiple solutions are possible.

      Please clarify.

      update

      for instance all these glob expressions are equivalent ( I removed the variations where {,a} and {a,} were swapped)

      use v5.12; use warnings; use Data::Dump; use Test::More; test ( [ <{a,aa}> ], [ <{,a}{a}> ], [ <{a}{,a}> ], [ <a{,a}> ], [ <{,a}a> ], ); sub test { my @g = @_; my (%h1,%h2); for my $i (0..$#g) { for my $j ($i+1..$#g) { is_deeply( [sort @{$g[$i]}], [sort @{$g[$j]}], "$i,$j") or ddx $g[$i],$g[$j]; } } } done_testing;

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      > OP updated. Don't count the curlies and commas.

      you should also add that curlies and commas should be minimal as a second criteria.

      for instance a , {a} and {{a}} are all equivalent.

      best you provide a sub metric

      edit

      sub cmp_glob($g1,$g2) which returns -1, 0 or 1 like cmp does.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        > for instance a , {a} and {{a}} are all equivalent.

        Interestingly, we've found a place where Perl's glob behaves differently to the brace expansion in bash:

        $ perl -wE 'say glob "{{a}}"' a $ echo {{a}} {{a}}

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found