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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Bracmat 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 Bracmat programming language

Source code in the bracmat programming language

  get$("unixdict.txt",STR):?wordList
& 1:?product
& :?unsorted
&   whl
  ' ( @(!wordList:(%?word:?letterString) \n ?wordList)
    & :?letterSum
    &   whl
      ' ( @(!letterString:%?letter ?letterString)
        &   (!letter:~#|str$(N !letter))+!letterSum
          : ?letterSum
        )
    & !letterSum^!word !unsorted:?unsorted
    )
& ( mergeSort
  =   newL L first second
    .   !arg:?L
      &   whl
        ' ( !L:% %
          & :?newL
          &   whl
            ' ( !L:%?first %?second ?L
              & !first*!second !newL:?newL
              )
          & !L !newL:?L
          )
      & !L
  )
& mergeSort$!unsorted:?product
& 0:?maxLength:?oldMaxLength
& :?derangedAnagrams
& ( deranged
  =   nextLetter Atail Btail
    .   !arg
      : ( (.)
        |   ( @(?:%@?nextLetter ?Atail)
            . @(?:(%@:~!nextLetter) ?Btail)
            )
          & deranged$(!Atail.!Btail)
        )
  )
& (   !product
    :   ?
      *   ?
        ^ ( %+%
          : @(%:? ([~
          :   ?
            + %@?anagramA
            + ?
            + %@?anagramB
            + ( ?
              & deranged$(!anagramA.!anagramB)
              &     (!anagramA.!anagramB)
                    (   !maxLength:>!oldMaxLength:?oldMaxLength
                      & 
                    | !derangedAnagrams
                    )
                : ?derangedAnagrams
              & ~
              )
          )
      * ?
  | out$!derangedAnagrams
  );

  

You may also check:How to resolve the algorithm Boolean values step by step in the VBScript programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Racket programming language
You may also check:How to resolve the algorithm Solve a Hopido puzzle step by step in the Picat programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Microsoft Small Basic programming language
You may also check:How to resolve the algorithm Pythagoras tree step by step in the Ruby programming language