Anagrams: Difference between revisions
correction |
Adding Haskell |
||
Line 2: | Line 2: | ||
Two or more words can be composed of the same characters, but in a different order. Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them. |
Two or more words can be composed of the same characters, but in a different order. Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them. |
||
== {{header|Haskell}} == |
|||
<pre> |
|||
import Data.List |
|||
groupon f x y = (f x) == (f y) |
|||
main = do |
|||
f <- readFile "./../Puzzels/Rosetta/unixdict.txt" |
|||
let words = lines f |
|||
wix = groupBy (groupon fst) . sort $ zipWith ((,).sort) words [0..] |
|||
mxl = foldl' ((.length).max) 0 wix |
|||
print . map (map ((words!!).snd)) . filter ((==mxl).length) $ wix |
|||
</pre> |
|||
== {{header|J}} == |
== {{header|J}} == |
||
Revision as of 13:35, 26 September 2008
You are encouraged to solve this task according to the task description, using any language you may know.
Two or more words can be composed of the same characters, but in a different order. Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them.
Haskell
import Data.List groupon f x y = (f x) == (f y) main = do f <- readFile "./../Puzzels/Rosetta/unixdict.txt" let words = lines f wix = groupBy (groupon fst) . sort $ zipWith ((,).sort) words [0..] mxl = foldl' ((.length).max) 0 wix print . map (map ((words!!).snd)) . filter ((==mxl).length) $ wix
J
(#~a:~:{:"1)(]/.~/:~&.>)<;._2 toJ fread jpath'~temp\unixdict.txt' +-----+-----+-----+-----+-----+ |abel |able |bale |bela |elba | +-----+-----+-----+-----+-----+ |alger|glare|lager|large|regal| +-----+-----+-----+-----+-----+ |angel|angle|galen|glean|lange| +-----+-----+-----+-----+-----+ |caret|carte|cater|crate|trace| +-----+-----+-----+-----+-----+ |elan |lane |lean |lena |neal | +-----+-----+-----+-----+-----+ |evil |levi |live |veil |vile | +-----+-----+-----+-----+-----+
Python
Python 2.5 shell input (IDLE) <python>>>> import urllib >>> from collections import defaultdict >>> words = urllib.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split() >>> len(words) 25104 >>> anagram = defaultdict(list) # map sorted chars to anagrams >>> for word in words: anagram[str(sorted(word))].append( word )
>>> count, max_anagrams = max((len(ana), ana) for ana in anagram.itervalues())
>>> for ana in anagram.itervalues():
if len(ana) >= count:
print ana
['caret', 'carte', 'cater', 'crate', 'trace']
['alger', 'glare', 'lager', 'large', 'regal']
['evil', 'levi', 'live', 'veil', 'vile']
['angel', 'angle', 'galen', 'glean', 'lange']
['elan', 'lane', 'lean', 'lena', 'neal']
['abel', 'able', 'bale', 'bela', 'elba']
>>> count
5
>>></python>