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

Published on 12 May 2024 09:40 PM

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

Source code in the liberty programming language

' count the word list
open "unixdict.txt" for input as #1
while not(eof(#1))
    line input #1,null$
    numWords=numWords+1
wend
close #1

'import to an array appending sorted letter set
open "unixdict.txt" for input as #1
dim wordList$(numWords,3)
dim chrSort$(45)
wordNum=1
while wordNum
    line input #1,actualWord$
    wordList$(wordNum,1)=actualWord$
    wordList$(wordNum,2)=sorted$(actualWord$)
    wordNum=wordNum+1
wend

'sort on letter set
sort wordList$(),1,numWords,2

'count and store number of anagrams found
wordNum=1
startPosition=wordNum
numAnagrams=0
currentChrSet$=wordList$(wordNum,2)
while wordNum < numWords
    while currentChrSet$=wordList$(wordNum,2)
        numAnagrams=numAnagrams+1
        wordNum=wordNum+1
    wend
    for n= startPosition to startPosition+numAnagrams
        wordList$(n,3)=right$("0000"+str$(numAnagrams),4)+wordList$(n,2)
    next
    startPosition=wordNum
    numAnagrams=0
    currentChrSet$=wordList$(wordNum,2)
wend

'sort on number of anagrams+letter set
sort wordList$(),numWords,1,3

'display the top anagram sets found
wordNum=1
while wordNum<150
    currentChrSet$=wordList$(wordNum,2)
    print "Anagram set";
    while currentChrSet$=wordList$(wordNum,2)
        print " : ";wordList$(wordNum,1);
        wordNum=wordNum+1
    wend
    print
    currentChrSet$=wordList$(wordNum,2)
wend

close #1
end

function sorted$(w$)
    nchr=len(w$)
    for chr = 1 to nchr
        chrSort$(chr)=mid$(w$,chr,1)
    next
    sort chrSort$(),1,nchr
    sorted$=""
    for chr = 1 to nchr
        sorted$=sorted$+chrSort$(chr)
    next
end function

  

You may also check:How to resolve the algorithm Variable size/Set step by step in the BASIC programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Cut a rectangle step by step in the jq programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Smart BASIC programming language
You may also check:How to resolve the algorithm Factorial step by step in the MAXScript programming language