Computer Old Farts Forum
 help / color / mirror / Atom feed
From: Ralph Corderoy <ralph@inputplus.co.uk>
To: Computer Old Farts Followers <coff@tuhs.org>
Subject: [COFF] Re: Grep has gone overboard
Date: Mon, 30 May 2022 10:19:35 +0100	[thread overview]
Message-ID: <20220530091935.073CA21F37@orac.inputplus.co.uk> (raw)
In-Reply-To: <DB37381E-6260-4974-B1B7-C28E0241D5F1@iitbombay.org>

Hi Bakul,

> 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]}

I was thinking it looks like APL and it turns out to be a descendant.
https://en.wikipedia.org/wiki/K_(programming_language)

That pushed me into writing an awk-permutation generator.  It's
inefficient as it keeps altering $0 which cascades into re-working $1,
etc., even if that's lazily done.  Still, it sufficies for the small
cases I typically need.

    $ cat perm
    #! /bin/awk -f

    function perm(s,   f, l, t) {
        if (!NF) {
            print substr(s, 2)
            return
        }
        l = $0
        for (f = 1; f <= NF; f++) {
            t = $f
            $f = ""; $0 = $0
            perm(s " " t)
            $0 = l
        }
    }

    { perm("") }
    $ 
    $ ./perm <<<''

    $ ./perm <<<1
    1
    $ ./perm <<<'1 2'
    1 2
    2 1
    $ ./perm <<<'1 2 3'
    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1
    $ 
    $ time ./perm <<<'1 2 3 4 5 6 7 8 9' | wc -l
    362880

    real    0m5.421s
    user    0m5.406s
    sys     0m0.082s
    $
    $ dc <<<'9 8*7*6*5*4*3*2*p'
    362880
    $

-- 
Cheers, Ralph.

  reply	other threads:[~2022-05-30  9:19 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
2022-05-30  9:19       ` Ralph Corderoy [this message]
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=20220530091935.073CA21F37@orac.inputplus.co.uk \
    --to=ralph@inputplus.co.uk \
    --cc=coff@tuhs.org \
    /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).