unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: puzzle with string permutations [photo]
Date: Wed, 08 Jun 2022 02:17:49 +0200	[thread overview]
Message-ID: <874k0wm2j6.fsf@dataswamp.org> (raw)
In-Reply-To: CAP_d_8VLm23PzuHE6L+D_4ZQotQzKM_RKmxY53v57wctChZ7_w@mail.gmail.com

Yuri Khan wrote:

> In general, with a multiset {X_1:n_1, ..., X_k:n_k}, if we
> number each letter, we get (n_1 + ... + n_k)! permutations,
> and then we have to adjust for duplicates for each
> individual letter by dividing by (n_1)! ... (n_k)!.
>
> n_perm = (n_1 + ... + n_k)! / (n_1)! ... (n_k)!

You mean like this?

(require 'cl-lib)

(defun cl-faculty (n)
  (cl-loop with prod = 1
    for i from 2 to n do
    (setq prod (* i prod))
    finally return prod) )
;; (cl-faculty  5) ;       120
;; (cl-faculty 10) ; 3 628 800

(defun count (e l)
  (seq-count (lambda (elem) (= elem e)) l) )
;; (count ?o '(?d ?g ?o ?o)) ; 2

(defun product-string (str)
  (let*((str-list         (string-to-list str))
        (str-list-no-dups (cl-remove-duplicates str-list))
        (prod 1) )
    (dolist (e str-list-no-dups)
      (setq prod (* prod (cl-faculty (count e str-list)))) )
    prod ))
;; (product-string "ogod") ; 2

(defun perms-string-num (str)
  (let ((n (cl-faculty (length str)))
        (r (product-string str)) )
    (/ n r) ))
;; (perms-string-num "ogod")   ;  12
;; (perms-string-num "kudtce") ; 720

-- 
underground experts united
https://dataswamp.org/~incal




  parent reply	other threads:[~2022-06-08  0:17 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-07  6:39 puzzle with string permutations [photo] Emanuel Berg
2022-06-07  7:12 ` Marcin Borkowski
2022-06-07  7:54   ` Emanuel Berg
2022-06-07  7:58     ` Emanuel Berg
2022-06-07  8:08       ` Emanuel Berg
2022-06-07 13:53         ` Emanuel Berg
2022-06-07 14:18           ` Emanuel Berg
2022-06-07 16:09             ` Yuri Khan
2022-06-07 22:04               ` Emanuel Berg
2022-06-08  0:17               ` Emanuel Berg [this message]
2022-06-08  2:52                 ` missing Lisp world (was: Re: puzzle with string permutations [photo]) Emanuel Berg
2022-06-07  8:37   ` puzzle with string permutations [photo] Emanuel Berg
2022-06-07 13:28     ` Emanuel Berg
2022-08-01  6:39 ` Marcin Borkowski
2022-08-01  7:39   ` Emanuel Berg
2022-08-02 12:21   ` Jean Louis

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=874k0wm2j6.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=help-gnu-emacs@gnu.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 read-only IMAP folder(s) and NNTP newsgroup(s).