help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Composing words from acronyms


From: Emanuel Berg
Subject: Re: Composing words from acronyms
Date: Tue, 25 Jul 2023 20:40:30 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

uzibalqa wrote:

> For instance, from the acronym 'Emacs' I can
> compose 'maces'.

Indeed, those are called string permutations and there are

  5! = 5 * 4 * 3 * 2 * 1 = 120

for "Emacs" since that string has 5 chars. The exclamation
mark is the faculty operator, and this type of computation is
part of a math field known as Combinatorics.

> Is this difficult to de in eamcs, and how may I approach
> this task ?

For example like this:

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/perm.el

(require 'cl-lib)
(require 'spell)

;; Christoph Conrad @ https://www.emacswiki.org/emacs/StringPermutations
(defun perms (l)
  (if l (cl-mapcan (lambda (a)
                     (cl-mapcan (lambda (p)
                                  (list (cons a p)))
                                (perms (cl-remove a l :count 1)) )) l)
    '(()) ))

(defun string-perms (str)
  (let*((chars      (string-to-list str))
        (char-perms (perms chars)) )
    (mapcar (lambda (a)
              (concat a) )
            char-perms) ))
;; (string-perms "hte") ; ("hte" "het" "the" "teh" "eht" "eth")

(defun string-perms-filter (str)
  (let ((strs (cl-remove-duplicates
               (cl-remove-if-not (lambda (w) (spell-word w)) (string-perms str))
               :test #'string=) ))
    (if (= 1 (length strs))
        (car strs)
      strs) ))

;; eht kudtce pigelsen tagni ogod seot erontuf si ni fo hte
;;
;; (string-perms-filter "ogod")     ; good
;; (string-perms-filter "erontuf")  ; fortune
;; (string-perms-filter "si")       ; is
;; (string-perms-filter "kudtce")   ; tucked
;; (string-perms-filter "ni")       ; in
;; (string-perms-filter "eth")      ; the
;; (string-perms-filter "seot")     ; toes
;; (string-perms-filter "fo")       ; of
;; (string-perms-filter "hte")      ; the
;; (string-perms-filter "pigelsen") ; ("peelings" "sleeping")
;; (string-perms-filter "tagni")    ; giant

(defun factorial (n)
  (if (> n 1)
      (* n (factorial (1- n)))
    1))
;; (factorial  5) ;       120
;; (factorial 10) ; 3 628 800

(defun cl-factorial (n)
  (cl-loop
    with prod = 1
    for i from 2 to n
    do (setq prod (* i prod))
    finally return prod) )
;; (cl-factorial  5) ;       120
;; (cl-factorial 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-factorial (count e str-list)))) )
    prod ))
;; (product-string "ogod") ; 2

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

(provide 'perm)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]