diff --git a/Problems 21-28/problem_21.hs b/Problems 21-28/problem_21.hs new file mode 100644 index 0000000..4f51012 --- /dev/null +++ b/Problems 21-28/problem_21.hs @@ -0,0 +1,4 @@ +insertAt :: a -> [a] -> Int -> [a] +insertAt x ls n = left ++ [x] ++ right + where + (left, right) = splitAt (n - 1) ls diff --git a/Problems 21-28/problem_22.hs b/Problems 21-28/problem_22.hs new file mode 100644 index 0000000..3a2f485 --- /dev/null +++ b/Problems 21-28/problem_22.hs @@ -0,0 +1,2 @@ +range :: Int -> Int -> [Int] +range m n = if m > n then [] else m : range (m + 1) n diff --git a/Problems 21-28/problem_23.hs b/Problems 21-28/problem_23.hs new file mode 100644 index 0000000..2d572e2 --- /dev/null +++ b/Problems 21-28/problem_23.hs @@ -0,0 +1,7 @@ +import Data.List +import System.Random + +rndSelect :: [a] -> Int -> IO [a] +rndSelect ls n = do + gen <- getStdGen + return $ take n [ls !! m | m <- nub $ randomRs (0, length ls - 1) gen] diff --git a/Problems 21-28/problem_24.hs b/Problems 21-28/problem_24.hs new file mode 100644 index 0000000..36fc5cf --- /dev/null +++ b/Problems 21-28/problem_24.hs @@ -0,0 +1,7 @@ +import Data.List +import System.Random + +diffSelect :: Int -> Int -> IO [Int] +diffSelect n m = do + gen <- getStdGen + return $ take n [[1 .. m] !! x | x <- nub $ randomRs (0, m - 1) gen] diff --git a/Problems 21-28/problem_25.hs b/Problems 21-28/problem_25.hs new file mode 100644 index 0000000..17b3f7a --- /dev/null +++ b/Problems 21-28/problem_25.hs @@ -0,0 +1,7 @@ +import Data.List +import System.Random + +rndPermu :: [a] -> IO [a] +rndPermu ls = do + gen <- getStdGen + return $ take (length ls) [ls !! m | m <- nub $ randomRs (0, length ls - 1) gen] diff --git a/Problems 21-28/problem_26.hs b/Problems 21-28/problem_26.hs new file mode 100644 index 0000000..74ba83c --- /dev/null +++ b/Problems 21-28/problem_26.hs @@ -0,0 +1,10 @@ +combinations :: Int -> [a] -> [[a]] +combinations 0 _ = [] +combinations _ [] = [] +combinations n (hd : tl) = with ++ without + where + with = + if n == 1 + then [[hd]] + else [(hd :)] <*> combinations (n - 1) tl + without = combinations n tl diff --git a/Problems 21-28/problem_27.hs b/Problems 21-28/problem_27.hs new file mode 100644 index 0000000..46f5aac --- /dev/null +++ b/Problems 21-28/problem_27.hs @@ -0,0 +1,11 @@ +import Data.List + +group' :: [Int] -> [a] -> [[[a]]] +group' ln ls = map ((([([fst] <*>)] <*>) . groupBy grouper . sortBy sorter) . zip ls) (nub (permutations placers)) + where + placers = concatMap (uncurry replicate) (zip ln [0 ..]) + sorter (_, r1) (_, r2) = r1 `compare` r2 + grouper (_, r1) (_, r2) = r1 == r2 + +group3 :: [a] -> [[[a]]] +group3 = group' [2, 3, 4] diff --git a/Problems 21-28/problem_28.hs b/Problems 21-28/problem_28.hs new file mode 100644 index 0000000..f5d38dc --- /dev/null +++ b/Problems 21-28/problem_28.hs @@ -0,0 +1,9 @@ +import Data.List + +lsort :: [[a]] -> [[a]] +lsort = sortBy (\x y -> length x `compare` length y) + +lfsort :: [[a]] -> [[a]] +lfsort ls = sortBy (\x y -> count x `compare` count y) ls + where + count x = length . filter (length x ==) $ map length ls