-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.awk
56 lines (51 loc) · 1.44 KB
/
anagram.awk
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
# anagram.awk --- An implementation of the anagram-finding algorithm
# from Jon Bentley's "Programming Pearls," 2nd edition.
# Addison Wesley, 2000, ISBN 0-201-65788-0.
# Column 2, Problem C, section 2.8, pp 18-20.
#
# This program requires gawk 4.0 or newer.
# Required gawk-specific features:
# - True multidimensional arrays
# - split() with "" as separator splits out individual characters
# - asort() and asorti() functions
#
# See http://savannah.gnu.org/projects/gawk.
#
# Arnold Robbins
# arnold@skeeve.com
# Public Domain
# January, 2011
BEGIN {
IGNORECASE = 1
}
/'s$/ { next } # Skip possessives
{
key = word2key($1) # Build signature
data[key][$1] = $1 # Store word with signature
}
# word2key --- split word apart into letters, sort, and join back together
function word2key(word, a, i, n, result)
{
n = split(word, a, "")
asort(a)
for (i = 1; i <= n; i++)
result = result a[i]
return tolower(result)
}
END {
sort = "sort"
for (key in data) {
# Sort words with same key
nwords = asorti(data[key], words)
if (nwords == 1)
continue
# And print. Minor glitch: trailing space at end of each line
line = words[1]
for (j = 2; j <= nwords; j++)
line = line " " words[j]
lines[++id] = line
}
asort(lines)
for (i = 1; i <= id; i++)
print lines[i]
}