Anagrams: Difference between revisions
Content deleted Content added
added perl |
No edit summary |
||
Line 275:
>>> anagram = defaultdict(list) # map sorted chars to anagrams
>>> for word in words:
anagram[
Line 284:
['
['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']
Line 300:
anagram = Hash.new {|hash, key| hash[key] = []}
for word in words
anagram[word.split('').sort
end
Line 310:
end</ruby>
Output:
▲ ["caret", "carte", "cater", "crate", "trace"]
["evil", "levi", "live", "veil", "vile"]
["elan", "lane", "lean", "lena", "neal"]▼
["abel", "able", "bale", "bela", "elba"]
["
["alger", "glare", "lager", "large", "regal"]
|