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

Published on 12 May 2024 09:40 PM

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

Source code in the liberty programming language

print "Loading dictionary file."
open "unixdict.txt" for input as #1
a$=input$(#1,lof(#1))
close #1

dim theWord$(30000)
dim ssWord$(30000)

c10$ = chr$(10)
i = 1
print "Creating array of words."
while instr(a$,c10$,i) <> 0
  j     = instr(a$,c10$,i)
  ln    = j - i
  again = 1
  sWord$ = mid$(a$,i,j-i)
  n = n + 1
 theWord$(n) = sWord$

 while again = 1
   again  = 0
   for kk = 1 to len(sWord$) - 1
   if mid$(sWord$,kk,1) > mid$(sWord$,kk +1,1) then
     sWord$ = left$(sWord$,kk-1);mid$(sWord$,kk+1,1);mid$(sWord$,kk,1);mid$(sWord$,kk+2)
     again  = 1
   end if
   next kk
 wend
 ssWord$(n) = sWord$
 i = j + 1
wend
print "Checking for deranged anagrams."
for i = 1 to n
  if len(theWord$(i)) > maxLen then
    for j = 1 to n
      if ssWord$(i) = ssWord$(j) and i <> j then
        cnt = 0
    for k = 1 to len(theWord$(i))
      if mid$(theWord$(i),k,1) = mid$(theWord$(j),k,1) then cnt = cnt + 1
    next k
    if cnt = 0 then
      maxLen = len(theWord$(i))
      maxPtrI = i
      maxPtrJ = j
    end if
      end if
    next j
  end if
next i

print theWord$(maxPtrI);" => ";theWord$(maxPtrJ)
end

  

You may also check:How to resolve the algorithm Maze generation step by step in the Raku programming language
You may also check:How to resolve the algorithm Arithmetic numbers step by step in the Arturo programming language
You may also check:How to resolve the algorithm Four is magic step by step in the Sidef programming language
You may also check:How to resolve the algorithm Polynomial regression step by step in the Raku programming language
You may also check:How to resolve the algorithm Polymorphic copy step by step in the Lua programming language