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

Published on 12 May 2024 09:40 PM

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

Source code in the basic programming language

OPTION COLLAPSE TRUE

DECLARE idx$ ASSOC STRING

FOR w$ IN LOAD$("unixdict.txt") STEP NL$

    set$ = SORT$(EXPLODE$(w$, 1))

    idx$(set$) = APPEND$(idx$(set$), 0, w$)
    total = AMOUNT(idx$(set$))

    IF MaxCount < total THEN MaxCount = total
NEXT

PRINT "Analyzing took ", TIMER, " msecs.", NL$

LOOKUP idx$ TO n$ SIZE x
FOR y = 0 TO x-1
    IF MaxCount = AMOUNT(idx$(n$[y])) THEN PRINT n$[y], ": ", idx$(n$[y])
NEXT

      INSTALL @lib$+"SORTLIB"
      sort% = FN_sortinit(0,0)
      
      REM Count number of words in dictionary:
      nwords% = 0
      dict% = OPENIN("unixdict.txt")
      WHILE NOT EOF#dict%
        word$ = GET$#dict%
        nwords% += 1
      ENDWHILE
      CLOSE #dict%
      
      REM Create arrays big enough to contain the dictionary:
      DIM dict$(nwords%), sort$(nwords%)
      
      REM Load the dictionary and sort the characters in the words:
      dict% = OPENIN("unixdict.txt")
      FOR word% = 1 TO nwords%
        word$ = GET$#dict%
        dict$(word%) = word$
        sort$(word%) = FNsortchars(word$)
      NEXT word%
      CLOSE #dict%
      
      REM Sort arrays using the 'sorted character' words as a key:
      C% = nwords%
      CALL sort%, sort$(1), dict$(1)
      
      REM Count the longest sets of anagrams:
      max% = 0
      set% = 1
      FOR word% = 1 TO nwords%-1
        IF sort$(word%) = sort$(word%+1) THEN
          set% += 1
        ELSE
          IF set% > max% THEN max% = set%
          set% = 1
        ENDIF
      NEXT word%
      
      REM Output the results:
      set% = 1
      FOR word% = 1 TO nwords%-1
        IF sort$(word%) = sort$(word%+1) THEN
          set% += 1
        ELSE
          IF set% = max% THEN
            FOR anagram% = word%-max%+1 TO word%
              PRINT dict$(anagram%),;
            NEXT
            PRINT
          ENDIF
          set% = 1
        ENDIF
      NEXT word%
      END
      
      DEF FNsortchars(word$)
      LOCAL C%, char&()
      DIM char&(LEN(word$))
      $$^char&(0) = word$
      C% = LEN(word$)
      CALL sort%, char&(0)
      = $$^char&(0)


  

You may also check:How to resolve the algorithm Balanced brackets step by step in the TMG programming language
You may also check:How to resolve the algorithm Box the compass step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the C++ programming language
You may also check:How to resolve the algorithm String case step by step in the J programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Wren programming language