Task: Anagrams








VB: dim x as integer, y as integer
dim SortX as integer
dim StrOutPut as string
dim Count as integer
dim MaxCount as integer

dim AnaList as QStringlist
dim wordlist as QStringlist
dim Templist as QStringlist
dim Charlist as Qstringlist


function sortChars(expr as string) as string
    Charlist.clear
    for SortX = 1 to len(expr)
         Charlist.AddItems expr[SortX]
    next
    charlist.sort
    result = Charlist.text - chr$(10) - chr$(13)
end function
 

    wordlist.loadfromfile ("unixdict.txt")

    
    for x = 0 to wordlist.itemcount-1
        AnaList.AddItems sortChars(wordlist.item(x))
    next
       
    
    analist.sort
    MaxCount = 0
    
    for x = 0 to AnaList.Itemcount-1
        Count = 0
        for y = x+1 to AnaList.Itemcount-1
            if AnaList.item(y) = AnaList.item(x) then
                inc(count)
            else
                if count > MaxCount then 
                    Templist.clear
                    MaxCount = Count
                    Templist.AddItems AnaList.item(x)
                elseif count = MaxCount then
                    Templist.AddItems AnaList.item(x)
                end if
                exit for
            end if
        next
    next
       
    
    for x = 0 to Templist.Itemcount-1
        for y = 0 to wordlist.Itemcount-1 
            if Templist.item(x) = sortChars(wordlist.item(y)) then
                StrOutPut = StrOutPut + wordlist.item(y) + "  "
            end if
        next
        StrOutPut = StrOutPut + chr$(13) + chr$(10)
    next

ShowMessage StrOutPut 
End




Ada: with Ada.Text_IO;  use Ada.Text_IO;

with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Containers.Indefinite_Ordered_Sets;

procedure Words_Of_Equal_Characters is
   package Set_Of_Words is new Ada.Containers.Indefinite_Ordered_Sets (String);
   use Ada.Containers, Set_Of_Words;
   package Anagrams is new Ada.Containers.Indefinite_Ordered_Maps (String, Set);
   use Anagrams;

   File   : File_Type;
   Result : Map;
   Max    : Count_Type := 1;

   procedure Put (Position : Anagrams.Cursor) is
      First : Boolean := True;
      List  : Set renames Element (Position);
      procedure Put (Position : Set_Of_Words.Cursor) is
      begin
         if First then
            First := False;
         else
            Put (',');
         end if;
         Put (Element (Position));
      end Put;
   begin
      if List.Length = Max then
         Iterate (List, Put'Access);
         New_Line;
      end if;
   end Put;

begin
   Open (File, In_File, "unixdict.txt");
   loop
      declare
         Word : constant String     := Get_Line (File);
         Key  : String (Word'Range) := (others => Character'Last);
         List : Set;
         Position : Anagrams.Cursor;
      begin
         for I in Word'Range loop
            for J in Word'Range loop
               if Key (J) > Word (I) then
                  Key (J + 1..I) := Key (J..I - 1);
                  Key (J) := Word (I);
                  exit;
               end if;
            end loop;
         end loop;
         Position := Find (Result, Key);
         if Has_Element (Position) then
            List := Element (Position);
            Insert (List, Word);
            Replace_Element (Result, Position, List);
         else
            Insert (List, Word);
            Include (Result, Key, List);
         end if;
         Max := Count_Type'Max (Max, Length (List));
      end;
   end loop;
exception
   when End_Error =>
      Iterate (Result, Put'Access);
      Close (File);
end Words_Of_Equal_Characters;