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

Published on 12 May 2024 09:40 PM

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

Source code in the basic programming language

DECLARE idx$ ASSOC STRING

FUNCTION Deranged(a$, b$)
    FOR i = 1 TO LEN(a$)
        IF MID$(a$, i, 1) = MID$(b$, i, 1) THEN RETURN FALSE
    NEXT
    RETURN TRUE
END FUNCTION

FOR w$ IN LOAD$(DIRNAME$(ME$) & "/unixdict.txt") STEP NL$
    set$ = EXTRACT$(SORT$(EXPLODE$(w$, 1)), " ")
    idx$(set$) = APPEND$(idx$(set$), 0, w$)
NEXT

FOR w$ IN OBTAIN$(idx$)
    FOR x = 1 TO AMOUNT(idx$(w$))
        FOR y = x+1 TO AMOUNT(idx$(w$))
            IF Deranged(TOKEN$(idx$(w$), x), TOKEN$(idx$(w$), y)) AND LEN(TOKEN$(idx$(w$), x)) > current THEN
                current = LEN(TOKEN$(idx$(w$), x))
                an1$ = TOKEN$(idx$(w$), x)
                an2$ = TOKEN$(idx$(w$), y)
            END IF
        NEXT
    NEXT
NEXT

PRINT "Maximum deranged anagrams: ", an1$, " and ", an2$

PRINT NL$, "Total time: ", TIMER, " msecs.", NL$

      INSTALL @lib$+"SORTLIB"
      Sort% = FN_sortinit(0,0)
      
      DIM dict$(26000), sort$(26000), indx%(26000)
      
      REM Load the dictionary:
      dict% = OPENIN("C:\unixdict.txt")
      IF dict%=0 ERROR 100, "No dictionary file"
      index% = 0
      REPEAT
        index% += 1
        dict$(index%) = GET$#dict%
        indx%(index%) = index%
      UNTIL EOF#dict%
      CLOSE #dict%
      Total% = index%
      
      TIME = 0
      REM Sort the letters in each word:
      FOR index% = 1 TO Total%
        sort$(index%) = FNsortstring(dict$(index%))
      NEXT
      
      REM Sort the sorted words:
      C% = Total%
      CALL Sort%, sort$(1), indx%(1)
      
      REM Find anagrams and deranged anagrams:
      maxlen% = 0
      maxidx% = 0
      FOR index% = 1 TO Total%-1
        IF sort$(index%) = sort$(index%+1) THEN
          One$ = dict$(indx%(index%))
          Two$ = dict$(indx%(index%+1))
          FOR c% = 1 TO LEN(One$)
            IF MID$(One$,c%,1) = MID$(Two$,c%,1) EXIT FOR
          NEXT
          IF c%>LEN(One$) IF c%>maxlen% maxlen% = c% : maxidx% = index%
        ENDIF
      NEXT
      
      PRINT "The longest deranged anagrams are '" dict$(indx%(maxidx%));
      PRINT "' and '" dict$(indx%(maxidx%+1)) "'"
      PRINT "(taking " ; TIME/100 " seconds)"
      END
      
      DEF FNsortstring(A$)
      LOCAL C%, a&()
      C% = LEN(A$)
      DIM a&(C%)
      $$^a&(0) = A$
      CALL Sort%, a&(0)
      = $$^a&(0)


  

You may also check:How to resolve the algorithm Additive primes step by step in the Python programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Delphi programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the F# programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the Scala programming language
You may also check:How to resolve the algorithm Metaprogramming step by step in the M2000 Interpreter programming language