Here is the another combinatorics challenge with a level intermediate on the programming forum. Still I think that they are very easy to solve. Here is the solution in Haskell, where not real combinatorics are required to supply a solution, instead word elements are used as numbers to find word matches in the text file, without too many libraries and lines of code.
Your challenge today is to write a program that can find the amount of anagrams within a .txt file. For example, “snap” would be an anagram of “pans”, and “skate” would be an anagram of “stake”.
module Main where import Data.Char import Data.List import Data.Maybe import Data.String.Utils import System.Environment liftLine :: String -> [Int] liftLine l = fmap ord $ strip l matchWord :: [Int] -> [Int] -> Maybe [Int] matchWord w1 w2 = if sort w1 == sort w2 then Just w2 else Nothing findMatches :: [Int] -> [[Int]] -> [String] findMatches x xs = fmap (fmap chr . fromJust) $ filter (/= Nothing) $ fmap (matchWord x) xs main :: IO () main = do [n, w] <- getArgs f <- readFile n let words = fmap liftLine $ lines f wmatch = liftLine w in mapM_ putStrLn $ findMatches wmatch words
Where the code above can be compiled using ghc --make prog.hs -o prog or can be executed using runghc prog.hs text-file word. With the text file bellow:
snap pans skate stake takes kates tekas ketas
Its output is as follows:
13:02 [dmw@www:0 exercises]$ ./challenge20120213 challenge20120213.txt kates skate stake takes kates tekas ketas
Enjoy…
Solution in C#:
Appears LLVM makes a Haskell/GHC program faster than Mono.
You may try with LLVM+Mono also:
Mono/LLVM
I will run a benchmark later.
I tested with AOT but it’s still low. I think it’s the IO layer :(
Hi,
I come here because I was googling about Haskell as I’m trying to learning it.
I really don’t understand your the aim of your solution, why do you “instead word elements are used as numbers to find word matches in the text file”?
The sort function of Data.List can sort a string (because they are list, of course).
So a simpler solution based in your code is:
In fact I’ve tested the performance of these two versions and the former gives a time of 643.943 seconds when is tested against a 2GiB wordlist file (with “anything” as search word), and the later solution takes only 312.493 seconds.
Well, I’ve solved that problem in few minutes… your solution can also be expressed as follows.
Which is simpler than yours. But not all people understands Haskell and all its tricky notations.