Task: Find words which contain the most consonants


C++: #include <bitset>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>



size_t consonants(const std::string& word) {
    std::bitset<26> bits;
    size_t bit = 0;
    for (char ch : word) {
        ch = std::tolower(static_cast<unsigned char>(ch));
        if (ch < 'a' || ch > 'z')
            continue;
        switch (ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            break;
        default:
            bit = ch - 'a';
            if (bits.test(bit))
                return 0;
            bits.set(bit);
            break;
        }
    }
    return bits.count();
}

int main(int argc, char** argv) {
    const char* filename(argc < 2 ? "unixdict.txt" : argv[1]);
    std::ifstream in(filename);
    if (!in) {
        std::cerr << "Cannot open file '" << filename << "'.\n";
        return EXIT_FAILURE;
    }
    std::string word;
    std::map<size_t, std::vector<std::string>, std::greater<int>> map;
    while (getline(in, word)) {
        if (word.size() <= 10)
            continue;
        size_t count = consonants(word);
        if (count != 0)
            map[count].push_back(word);
    }
    const int columns = 4;
    for (const auto& p : map) {
        std::cout << p.first << " consonants (" << p.second.size() << "):\n";
        int n = 0;
        for (const auto& word : p.second) {
            std::cout << std::left << std::setw(18) << word;
            ++n;
            if (n % columns == 0)
                std::cout << '\n';
        }
        if (n % columns != 0)
            std::cout << '\n';
        std::cout << '\n';
    }
    return EXIT_SUCCESS;
}



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()
            }
        }
    }
}