Last active
August 29, 2015 14:10
-
-
Save Pitometsu/92f5ec486c4b370ae3df to your computer and use it in GitHub Desktop.
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
-- Code challenge. | |
{-# LANGUAGE NPlusKPatterns #-} | |
import Control.Monad (guard, liftM) | |
revert :: Int -- | digit | |
-> Int -- | with radix | |
-> Int -- | reverse digit | |
revert d r = revert' 0 d | |
where | |
revert' i 0 = i | |
revert' i d = let i' = mod d r + i * r | |
d' = div d r | |
in revert' i' d' | |
palindromic :: Int -- | digit | |
-> Int -- | with radix | |
-> Bool -- | is palindrome | |
palindromic p r = p == revert p r | |
procRep :: (Int -> Bool) -- | filter condition | |
-> (Int -> [Int] -> [Int]) -- | processing | |
-> Int -- | number of repetitions | |
-> [Int] -- | list of digits | |
-> [Int] -- | process with repetitions | |
procRep c s = procRep' c s $ \i l -> do | |
e <- s i l | |
guard $ c e | |
return e | |
-- TODO: add multithreading | |
procRep' :: (Int -> Bool) -- | filter condition | |
-> (Int -> [Int] -> [Int]) -- | processing | |
-> (Int -> [Int] -> [Int]) -- | condition process | |
-> Int -- | number of repetitions | |
-> [Int] -- | list of digits | |
-> [Int] -- | process with repetitions | |
procRep' _ _ _ 0 _ = [1] | |
procRep' _ _ _ _ [] = [] | |
procRep' c s p n'@(n + 1) all@(x : xs) = current ++ remaining | |
where | |
remaining = procRep' c s p n' xs | |
current = p x $ procRep' c s s n all | |
main :: IO () -- | evaluate largest palindrome | |
main = print largest | |
where | |
digits = 3 | |
radix = 10 | |
count = 2 | |
min = radix ^ (digits - 1) | |
max = radix ^ digits - 1 | |
numbers = [min .. max] | |
process = liftM . (*) | |
condition = flip palindromic radix | |
variants = procRep condition process count numbers | |
largest = maximum variants |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment