func find_deranged(Array a) {
for i in (^a) {
for j in (i+1 .. a.end) {
overlaps(a[i], a[j]) || (
printf("length %d: %s => %s\n", a[i].len, a[i], a[j])
return true
)
}
}
return false
}
func main(File file) {
file.open_r(\var fh, \var err) ->
|| die "Can't open file `#{file}' for reading: #{err}\n"
var letter_list = Hash()
fh.words.each { |word|
letter_list{word.sort} := [] << word
}
letter_list.keys \
.grep {|k| letter_list{k}.len > 1} \
.sort {|a,b| b.len <=> a.len} \
.each {|key|
find_deranged(letter_list{key}) && break
}
}
main(%f'/tmp/unixdict.txt')
Output:
length 10: excitation => intoxicate