Created
January 12, 2010 00:59
-
-
Save joevandyk/274774 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import List | |
import qualified Data.Map as Map | |
-- Given as stdin | |
-- presents | |
-- serpents | |
-- no | |
-- on | |
-- whatever | |
-- Expected Output: | |
-- [["serpents","presents"],["on","no"]] | |
-- This version only displays words that have more than one | |
-- match in the list, and sorts by the words that got the most matches. | |
-- Can we do the map bit better? | |
main = do | |
input <- getContents | |
print $ anagrams $ lines input | |
anagrams words = | |
sorted_anagrams | |
where | |
sorted_anagrams = sortBy sorter filtered_anagrams | |
sorter a b = compare (length b) (length a) | |
filtered_anagrams = Map.elems $ Map.filter filter_function all_anagrams | |
filter_function words = length words > 1 | |
all_anagrams = do_anagrams words Map.empty | |
do_anagrams [] result = result | |
do_anagrams words result = do_anagrams | |
(tail words) | |
(Map.unionWith | |
(++) | |
(Map.fromList [(sorted_current_word, [current_word])]) | |
result) | |
where | |
current_word = head words | |
sorted_current_word = sort current_word |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input = STDIN.read.split("\n") | |
result = Hash.new([]) | |
input.each do |word| | |
sorted_word = word.split('').sort.join | |
result[sorted_word] += [word] | |
end | |
values = result.values.sort { |a, b| b.size <=> a.size } | |
p values[0..3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment