Task: Alternade words







Python: WORDFILE = 'unixdict.txt'
MINLEN = 6

class Trie(object):
    
    class Node(object):
        
        def __init__(self, char='\0', parent=None):
            self.children = {}
            self.char = char
            self.final = False
            self.parent = parent
            
        def descend(self, char, extend=False):
            
            if not char in self.children:
                if not extend: return None
                self.children[char] = Trie.Node(char,self)
            return self.children[char]
        
    def __init__(self):
        self.root = Trie.Node()
    
    def insert(self, word):
        
        node = self.root
        for char in word: node = node.descend(char, extend=True)
        node.final = True
        return node
    
    def __contains__(self, word):
        
        node = self.root
        for char in word:
            node = node.descend(char)
            if not node: return False
        return node.final 
    
    def words(self):
        
        nodes = [self.root]
        while nodes:
            node = nodes.pop()
            nodes += node.children.values()
            if node.final:
                word = []
                while node:
                    if node.char != '\0': word.append(node.char)
                    node = node.parent
                yield "".join(reversed(word))
    
    def __iter__(self):
        return self.words()
                

words = Trie()
with open(WORDFILE, "rt") as f:
    for word in f.readlines():
        words.insert(word.strip())

for word in words:
    if len(word) < MINLEN: continue
    even = word[::2]
    odd = word[1::2]
    if even in words and odd in words:
        print(word, even, odd)





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

procedure Alternade_Words is
   use Ada.Text_Io;

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

   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;

   Filename : constant String := "unixdict.txt";
   Words    : String_Maps.Map;
   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);

   for Word of Words loop
      declare
         Odd  : constant String := Get_Odd  (Word);
         Even : constant String := Get_Even (Word);
      begin
         if
           Word'Length >= 6       and then
             Words.Contains (Odd) and then
             Words.Contains (Even)
         then
            Put (Word);
            Set_Col (15); Put (Odd);
            Set_Col (25); Put (Even);
            New_Line;
         end if;
      end;
   end loop;
end Alternade_Words;