aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/paulo-custodio/awk/ch-2.awk
blob: 93706dff9d740766581135e9509693886821d537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/gawk

# Challenge 005
#
# Challenge #2
# Write a program to find the sequence of characters that has the most anagrams.
#
# create a hash of all words in dictionary where key is sorted list of letters
# therefore two anagrams have the same key

function alen(a, i, k) {
    k = 0;
    for(i in a) k++;
    return k;
}

function join(array, start, end, sep,    result, i) {
    if (sep == "")
       sep = " ";
    else if (sep == SUBSEP) # magic value
       sep = "";
    result = array[start];
    for (i = start + 1; i <= end; i++)
        result = result sep array[i];
    return result;
}

function word_key(word,    i, letters, key) {
    for (i = 1; i <= length(word); i++)
        letters[i] = substr(word, i, 1);
    asort(letters);
    key = join(letters, 1, alen(letters), SUBSEP);
    return key;
}

BEGIN {
    max_anagrams = 0;
    while ((getline word < "words.txt") > 0) {
        if (length(word) >= 2) {
            if (!(word ~ /[^a-zA-Z]/)) {
                key = word_key(tolower(word));
                num_anagrams = ++anagrams[key];
                if (num_anagrams > max_anagrams)
                    max_anagrams = num_anagrams;
            }
        }
    }

    num_found = 0;
    for (key in anagrams) {
        if (anagrams[key] == max_anagrams)
            found[num_found++] = key;
    }
    asort(found);

    print "Maximum of " max_anagrams " anagrams";
    for (i in found)
        print found[i];
    exit 0;
}