39 lines
1.3 KiB
Plaintext
39 lines
1.3 KiB
Plaintext
\( Find anagrams (words with the same set of letters)
|
|
Words are the lines of an input file.
|
|
Sorting the letters of the word gives the key for a map
|
|
having a list (row) of all words so far.
|
|
\)
|
|
\+ stdlib
|
|
main(parms):+
|
|
parms[1] =~ 'unixdict.txt' \ default input file
|
|
wordmap =: get words from parms[1]
|
|
maxw =: wordmap.maxlen
|
|
?# idx =: map wordmap give keys ascending
|
|
words =: wordmap{idx}
|
|
? maxw = words.count
|
|
print words::join values by ', '
|
|
|
|
\ read the file (infn) and return a map
|
|
get words from (infn):
|
|
res =: new map \ create the result map
|
|
res.maxlen =: 0
|
|
?# line =: file infn give lines \ or: infn::give lines
|
|
add line to res
|
|
:> res
|
|
|
|
\ add a word to the map
|
|
add (word) to (amap):
|
|
idx =: sort characters of word::case to upper
|
|
row =: amap{idx} \ get indexed words
|
|
? row = () \ new entry if void
|
|
row =: new row
|
|
amap{idx} =: row
|
|
row[] =: word \ add new entry
|
|
amap.maxlen =: maximum of amap.maxlen, row.count
|
|
|
|
\ Sort the characters of a string
|
|
sort characters of (s):
|
|
sr =: string s as character row
|
|
row sr sort ascending
|
|
:> row sr join values as string \ without separator
|