How to resolve the algorithm Anagrams/Deranged anagrams step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Anagrams/Deranged anagrams step by step in the zkl programming language

Table of Contents

Problem Statement

Two or more words are said to be anagrams if they have the same characters, but in a different order. By analogy with derangements we define a deranged anagram as two words with the same characters, but in which the same character does not appear in the same position in both words. Use the word list at unixdict to find and display the longest deranged anagram.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Anagrams/Deranged anagrams step by step in the zkl programming language

Source code in the zkl programming language

words:=Dictionary(25000);  //-->Dictionary(sorted word:all anagrams, ...)
File("unixdict.txt").read().pump(Void,'wrap(w){
   w=w.strip(); key:=w.sort(); words[key]=words.find(key,T).append(w);
});

nws:=words.values.pump(List,fcn(ws){ //-->( (len,words), ...)
   if(ws.len()>1){ // two or more anagrams
      r:=List(); n:=ws[0].len(); // length of these anagrams
      foreach idx,w in (ws.enumerate()){
	 foreach w2 in (ws[idx+1,*]){
	    if(Utils.zipWith('!=,w,w2).filter().len()==n)
	       r.write(T(w,w2));
	 }
      }
      if(r) return(r.insert(0,n));
   }
   Void.Skip
});

nws.filter(fcn(nws,max){ nws[0]==max },
   nws.reduce(fcn(p,nws){ p.max(nws[0]) },0) )
.println();

nws:=words.values.pump(List,fcn(ws){ //-->( (len,words), ...)
   if(ws.len()>1){ // two or more anagrams
      n:=ws[0].len(); // length of these anagrams
      r:=Utils.Helpers.permute(ws).filter('wrap(ws2){
            n == Utils.zipWith('!=,ws2.xplode()).filter().len();
	 });
      if(r) return(n,r[0]); // L(L("glove","vogel"))-->L(5,L("glove","vogel"))
   }
   Void.Skip
});

  

You may also check:How to resolve the algorithm Truth table step by step in the Go programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the Julia programming language
You may also check:How to resolve the algorithm Assertions step by step in the Oforth programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the Red programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Julia programming language