On May 30, 2022, at 1:16 AM, Ralph Corderoy
<ralph(a)inputplus.co.uk> wrote:
Hi Bakul,
You can write a program to generate all
permutations and use that as
your regexp. For example abc maps to abc|acb|bac|bca|cab|cba.
Being lazy, I tend to use a shell's brace expansion and then whittle it
down to generate permutations. I keep thinking it's surprising there's
no Bell Labs program to produce them given there's things like
factor(1).
For permutations I just use this k program:
$ cat perm.k
p:{:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}
perm:{x@p[#x]}
$ k perm.k
perm "abc"
("abc"
"acb"
"bac"
"bca"
"cab"
"cba")
I won’t bother explaining but p n returns all permutations of
indices 0..n-1. Then perm is easy to construct. Similarly easy to
insert | between strings but your solution is much more in the spirit
of regexp and faster than my brute force solution!
Also, thanks for catching the anchoring error!
The brace expansion is wasteful as it's going from n**l down to n! and
it can result in the old bash here not responding to SIGINT for a while
if it's producing gazillions.
$ printf '%s\n' {b,a,d,u,g,l,y}{b,a,d,u,g,l,y}{b,a,d,u,g,l,y}\
{b,a,d,u,g,l,y}{b,a,d,u,g,l,y}{b,a,d,u,g,l,y}{b,a,d,u,g,l,y} |
egrep -v '(.).*\1' |
fgrep -x -f - /usr/share/dict/words
ladybug
$
--
Cheers, Ralph.