-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #393 from rbasso/anagram-generalize-test-suite
anagram: Make the test suite as flexible as possible
- Loading branch information
Showing
4 changed files
with
44 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Hints | ||
|
||
To complete this exercise you need to implement the function `anagramsFor`, | ||
that takes a *word* and a group of *words*, returning the ones that are | ||
anagrams of the given *word*. | ||
|
||
If it is your first time solving this exercise, it is recommended that you | ||
stick to the provided signature: | ||
|
||
```haskell | ||
anagramsFor :: String -> [String] -> [String] | ||
``` | ||
|
||
Later, it may be a good idea to revisit this problem and play with other data | ||
types and libraries: | ||
|
||
- `Text`, from package *text*. | ||
- `Sequence` and `Set`, from package *containers*. | ||
- `MultiSet`, from package *multiset* | ||
|
||
The test suite was intentionally designed to accept almost any type signature | ||
that makes sense, so you are encouraged to find the one you think is the best. |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
module Anagram (anagramsFor) where | ||
import Data.List (sort) | ||
import Data.Char (toLower) | ||
|
||
anagramsFor :: String -> [String] -> [String] | ||
anagramsFor word = filter (isAnagram . normalize) | ||
import Data.Function (on) | ||
import Data.MultiSet (fromList) | ||
import Data.Set (Set, filter) | ||
import Data.Text (Text, toLower, unpack) | ||
import Prelude hiding (filter) | ||
|
||
anagramsFor :: Text -> Set Text -> Set Text | ||
anagramsFor x = filter (\w -> x `isDistinctOf` w && x `isAnagramOf` w) | ||
where | ||
normalize xs = let nxs = map toLower xs in (nxs, sort nxs) | ||
(nw, sw) = normalize word | ||
isAnagram (w, s) = nw /= w && sw == s | ||
isDistinctOf = (/=) `on` toLower | ||
isAnagramOf = (==) `on` fromList . unpack . toLower |
This file contains 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