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

Published on 12 May 2024 09:40 PM

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

Source code in the setl programming language

h := open('unixdict.txt', "r");
anagrams := {};
while not eof(h) loop
  geta(h, word);
  if word = om or word = "" then
    continue;
  end if;
  sorted := insertion_sort(word);
  anagrams{sorted} with:= word;
end loop;

max_size := 0;
max_words := {};
for words = anagrams{sorted} loop
  size := #words;
  if size > max_size then
    max_size := size;
    max_words := {words};
  elseif size = max_size then
    max_words with:= words;
  end if;
end loop;

for w in max_words loop
  print(w);
end loop;

-- GNU SETL has no built-in sort()
procedure insertion_sort(A);
  for i in [2..#A] loop
    v := A(i);
    j := i-1;
    while j >= 1 and A(j) > v loop
      A(j+1) := A(j);
      j := j - 1;
    end loop;
    A(j+1) := v; 
 end loop;
 return A;
end procedure;

  

You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Haskell programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Aime programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Quackery programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Relation programming language