How to resolve the algorithm Anagrams step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Anagrams step by step in the EchoLisp programming language

Table of Contents

Problem Statement

When two or more words are composed of the same characters, but in a different order, they are called anagrams. Using the word list at   http://wiki.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Anagrams step by step in the EchoLisp programming language

Source code in the echolisp programming language

(require 'struct)
(require 'hash)
(require 'sql)
(require 'words)
(require 'dico.fr.no-accent)


(define mots-français (words-select #:any null 999999))
(string-delimiter "")

(define (string-sort str)
	(list->string (list-sort stringlist str))))
	
(define (ana-sort H words) ;; bump counter for each word
	(for ((w words))
		#:continue (< (string-length w) 4)
		(let [(key (string-sort w))]  (hash-set H key (1+ (hash-ref! H key 0))))))
		
;; input w word
;; output : list of matching words
(define (anagrams w words)
	(set! w (string-sort w))
	(make-set
	(for/list (( ana words))
		#:when (string=? w (string-sort ana))
		ana)))

(define (task words)
(define H (make-hash))
	(ana-sort H words) ;; build counters key= sorted-string, value = count
	(hash-get-keys H   ;; extract max count values
	    (for/fold (hmax 0) ((h H) ) 
	    #:when (>= (cdr h) hmax)
	    (cdr h))
	))


(length mots-français)
    → 209315
(task mots-français)
    → (aeilns acenr) ;; two winners
(anagrams "acenr" mots-français)
    → { ancre caner caren carne ceran cerna encra nacre nerac rance renac }
(anagrams "aeilns" mots-français)
    → { alisen enlias enlisa ensila islaen islean laines lianes salien saline selina }


  

You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Oforth programming language
You may also check:How to resolve the algorithm I'm a software engineer, get me out of here step by step in the F# programming language
You may also check:How to resolve the algorithm XML/Output step by step in the Objeck programming language
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the REXX programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the Ruby programming language