module Exercise_12 where import System.IO import qualified Data.Map as Map import Data.List (nub, partition, sortBy) import Data.Functor ((<$>)) import Data.Ord (comparing) import Data.Bifoldable (biconcat) isPrime :: Int -> Bool isPrime n | n <= 1 = False | otherwise = primeAux (n-1) where primeAux 1 = True primeAux i = n `mod` i /= 0 && primeAux (i-1) -- returns all primes up to the passed number primes :: Int -> [Int] primes n = [i | i<-[2..n], isPrime i] {-WETT-} -- Move prime numbers to front by splitting number sequence accordingly -- [a] -> [a] ordering = map snd . biconcat . partition fst . zip (isPrime <$> enumFrom 1) encrypt :: String -> String encrypt = ordering -- Retrieve ordering in terms of indices and sort chars back accordingly -- Example : [(2,'x'), (3,'a'), (1,'e'), (4,'m')] -> [(1,'e'), (2,'x'), (3, 'a'), (4,'m')] decrypt :: String -> String decrypt ws = fst <$> (sortBy (comparing snd) . zip ws . ordering . enumFromTo 1 . length) ws {-TTEW-}