Anagrams: Difference between revisions
added J |
→{{header|J}}: shorter solution |
||
Line 4: | Line 4: | ||
== {{header|J}} == |
== {{header|J}} == |
||
(#~a:~:{:"1)(]/.~/:~&.>)<;._2 toJ fread jpath'~temp\unixdict.txt' |
|||
+-----+-----+-----+-----+-----+ |
+-----+-----+-----+-----+-----+ |
||
|abel |able |bale |bela |elba | |
|abel |able |bale |bela |elba | |
Revision as of 19:36, 25 September 2008
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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.
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 >>> acronym = defaultdict(list) # map sorted chars to acronyms >>> for word in words: acronym[str(sorted(word))].append( word )
>>> count, max_acronyms = max((len(acr), acr) for acr in acronym.itervalues())
>>> for acr in acronym.itervalues():
if len(acr) >= count:
print acr
['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>