“アナグラムを探す”を Squeak Smalltalk で

単語辞書から相互にアナグラムになっている単語を集める.
アナグラムとは,(意味のある)単語の文字をならび変えてできた別の(意味のある)単語のこと.

2009-11-10


バッグ(言語によってはマルチセットとも呼ばれる)が使えそうですね。

| words |
words := #('amass' 'assam' 'apple' 'banana' 'amble' 'blame' 'mabel' 'mable').

(words collect: [:each | each asBag]) asBag valuesAndCounts associations
   select: [:assoc | assoc value > 1]
   thenCollect: [:assoc | words select: [:each | each asBag = assoc key]]
=> #(#('amass' 'assam') #('amble' 'blame' 'mabel' 'mable'))


参考までに

'aaabbc' asBag valuesAndCounts associations   "=> {$c->1 . $a->3 . $b->2} "
($a -> 3) key     "=> $a "
($a -> 3) value   "=> 3  "