Task: Find words which contain the most consonants




Go: package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "unicode/utf8"
)

func contains(list []int, value int) bool {
    for _, v := range list {
        if v == value {
            return true
        }
    }
    return false
}

func main() {
    wordList := "unixdict.txt"
    b, err := ioutil.ReadFile(wordList)
    if err != nil {
        log.Fatal("Error reading file")
    }
    bwords := bytes.Fields(b)
    var words []string
    for _, bword := range bwords {
        s := string(bword)
        if utf8.RuneCountInString(s) > 10 {
            words = append(words, s)
        }
    }
    vowelIndices := []int{0, 4, 8, 14, 20}
    wordGroups := make([][]string, 12)
    for _, word := range words {
        letters := make([]int, 26)
        for _, c := range word {
            index := c - 97
            if index >= 0 && index < 26 {
                letters[index]++
            }
        }
        eligible := true
        uc := 0 
        for i := 0; i < 26; i++ {
            if !contains(vowelIndices, i) {
                if letters[i] > 1 {
                    eligible = false
                    break
                } else if letters[i] == 1 {
                    uc++
                }
            }
        }
        if eligible {
            wordGroups[uc] = append(wordGroups[uc], word)
        }
    }

    for i := 11; i >= 0; i-- {
        count := len(wordGroups[i])
        if count > 0 {
            s := "s"
            if count == 1 {
                s = ""
            }
            fmt.Printf("%d word%s found with %d unique consonants:\n", count, s, i)
            for j := 0; j < count; j++ {
                fmt.Printf("%-15s", wordGroups[i][j])
                if j > 0 && (j+1)%5 == 0 {
                    fmt.Println()
                }
            }
            fmt.Println()
            if count%5 != 0 {
                fmt.Println()
            }
        }
    }
}








Ada: with Ada.Text_Io;
with Ada.Strings.Fixed;
with Ada.Containers.Indefinite_Vectors;

procedure Most_Consonants is
   use Ada.Text_Io;

   package String_Vectors is
     new Ada.Containers.Indefinite_Vectors (Index_Type   => Positive,
                                            Element_Type => String);

   function Is_Vowel (Letter : Character) return Boolean
   is (Ada.Strings.Fixed.Index ("aeiou", "" & Letter) /= 0);

   type Consonant_Count is range 0 .. 15;
   Filename : constant String := "unixdict.txt";
   File     : File_Type;
   Words    : array (Consonant_Count) of String_Vectors.Vector;
begin
   Open (File, In_File, Filename);
   while not End_Of_File (File) loop
      declare
         Word  : constant String  := Get_Line (File);
         Found : array (Character) of Boolean := (others => False);
         Conso : Consonant_Count := 0;
         Bad   : Boolean         := False;
      begin
         for Letter of Word loop
            if Is_Vowel (Letter) then
               null;
            elsif Found (Letter) then
               Bad := True;
            else
               Found (Letter) := True;
               Conso := Conso + 1;
            end if;
         end loop;
         if not Bad and Word'Length > 10 then
            Words (Conso).Append (Word);
         end if;
      end;
   end loop;
   Close (File);


   for Cons in Words'Range loop
      if not Words (Cons).Is_Empty then
         Put (Cons'Image & " consonants: ");
         Put (Words (Cons).Length'Image & " words");
         New_Line;
      end if;
      declare
         Column : Natural := 0;
         Image  : String (1 .. 15);
      begin
         for Word of Words (Cons) loop
            Ada.Strings.Fixed.Move (Word, Image);
            Put (Image);
            Column := Column + 1;
            if Column mod 8 = 0 then
               New_Line;
            end if;
         end loop;
      end;
      if not Words (Cons).Is_Empty then
         New_Line;
      end if;
   end loop;

end Most_Consonants;