Computer Old Farts Forum
 help / color / mirror / Atom feed
From: Bakul Shah <bakul@iitbombay.org>
To: Ralph Corderoy <ralph@inputplus.co.uk>
Cc: Computer Old Farts Followers <coff@tuhs.org>
Subject: [COFF] Re: Grep has gone overboard
Date: Mon, 30 May 2022 01:43:52 -0700	[thread overview]
Message-ID: <DB37381E-6260-4974-B1B7-C28E0241D5F1@iitbombay.org> (raw)
In-Reply-To: <20220530081604.A9FBF21F8A@orac.inputplus.co.uk>



> On May 30, 2022, at 1:16 AM, Ralph Corderoy <ralph@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.

  reply	other threads:[~2022-05-30  8:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-30  4:12 [COFF] " Dave Horsfall
2022-05-30  4:47 ` [COFF] " Bakul Shah
2022-05-30  6:35   ` Michael Kjörling
2022-05-30  6:51     ` Bakul Shah
2022-05-30  8:26       ` Ralph Corderoy
2022-05-30  8:01     ` Ralph Corderoy
2022-05-30  8:16   ` Ralph Corderoy
2022-05-30  8:43     ` Bakul Shah [this message]
2022-05-30  9:19       ` Ralph Corderoy
2022-05-30  7:38 ` Ralph Corderoy
2022-05-30  7:44   ` Ralph Corderoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DB37381E-6260-4974-B1B7-C28E0241D5F1@iitbombay.org \
    --to=bakul@iitbombay.org \
    --cc=coff@tuhs.org \
    --cc=ralph@inputplus.co.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).