Task: Odd words





Java: import java.io.*;
import java.util.*;

public class OddWords {
    public static void main(String[] args) {
        try {
            Set<String> dictionary = new TreeSet<>();
            final int minLength = 5;
            String fileName = "unixdict.txt";
            if (args.length != 0)
                fileName = args[0];
            try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.length() >= minLength)
                        dictionary.add(line);
                }
            }
            StringBuilder word1 = new StringBuilder();
            StringBuilder word2 = new StringBuilder();
            List<StringPair> evenWords = new ArrayList<>();
            List<StringPair> oddWords = new ArrayList<>();
            for (String word : dictionary) {
                int length = word.length();
                if (length < minLength + 2 * (minLength/2))
                    continue;
                word1.setLength(0);
                word2.setLength(0);
                for (int i = 0; i < length; ++i) {
                    if ((i & 1) == 0)
                        word1.append(word.charAt(i));
                    else
                        word2.append(word.charAt(i));
                }
                String oddWord = word1.toString();
                String evenWord = word2.toString();
                if (dictionary.contains(oddWord))
                    oddWords.add(new StringPair(word, oddWord));
                if (dictionary.contains(evenWord))
                    evenWords.add(new StringPair(word, evenWord));
            }
            System.out.println("Odd words:");
            printWords(oddWords);
            System.out.println("\nEven words:");
            printWords(evenWords);
        } catch (Exception e)  {
            e.printStackTrace();
        }
    }

    private static void printWords(List<StringPair> strings) {
        int n = 1;
        for (StringPair pair : strings) {
            System.out.printf("%2d: %-14s%s\n", n++,
                                    pair.string1, pair.string2);
        }
    }

    private static class StringPair {
        private String string1;
        private String string2;
        private StringPair(String s1, String s2) {
            string1 = s1;
            string2 = s2;
        }
    }
}







Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Odd_Words is
   use Ada.Text_Io;

   package String_Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => String,
                                                 Element_Type => String);

   Filename : constant String := "unixdict.txt";
   Words    : String_Maps.Map;

   function Get_Odd (Word : String) return String is
      Odd : String (1 .. (Word'Length + 1) / 2);
   begin
      for Index in Odd'Range loop
         Odd (Index) := Word (1 + 2 * (Index - 1));
      end loop;
      return Odd;
   end Get_Odd;

   function Get_Even (Word : String) return String is
      Even : String (1 .. Word'Length / 2);
   begin
      for Index in Even'Range loop
         Even (Index) := Word (1 + 1 + 2 * (Index - 1));
      end loop;
      return Even;
   end Get_Even;

   generic
      with function Filter (Word : String) return String;
   procedure Iterate_Map;

   procedure Iterate_Map is
   begin
      for Word of Words loop
         declare
            Half : constant String := Filter (Word);
         begin
            if Half'Length > 4 and then Words.Contains (Half) then
               Put (Word); Set_Col (15); Put_Line (Half);
            end if;
         end;
      end loop;
   end Iterate_Map;

   procedure Put_Odd_Words  is new Iterate_Map (Get_Odd);
   procedure Put_Even_Words is new Iterate_Map (Get_Even);

   File : File_Type;
begin
   Open (File, In_File, Filename);
   while not End_Of_File (File) loop
      declare
         Word : constant String := Get_Line (File);
      begin
         Words.Insert (Word, Word);
      end;
   end loop;
   Close (File);

   Put_Line ("Odd words:");
   Put_Odd_Words;
   New_Line;

   Put_Line ("Even words:");
   Put_Even_Words;
end Odd_Words;