Created
June 29, 2014 14:24
-
-
Save shoooe/174bf8865354e54e7246 to your computer and use it in GitHub Desktop.
Simple program that calculates the number of possible phone numbers of length 4 that have 2 or more equal digits, and displays them.
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
import Data.Map (Map) | |
import qualified Data.Map as Map | |
countDistinct :: Ord a => [a] -> Int | |
countDistinct = Map.size . foldr fn Map.empty | |
where fn e = Map.insertWith (+) e 1 | |
validStrings :: [String] | |
validStrings = | |
filter ((`elem` [1..3]) . countDistinct) $ do | |
a <- ['0'..'9'] | |
b <- ['0'..'9'] | |
c <- ['0'..'9'] | |
d <- ['0'..'9'] | |
return $ a:b:c:d:[] | |
main :: IO () | |
main = do | |
putStrLn $ "Total: " ++ (show . length) validStrings | |
putStrLn "List:" | |
mapM_ (putStrLn . (" "++)) validStrings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment