new: Solved problems 21 to 28
This commit is contained in:
parent
2345e43a15
commit
a45b84ee9c
8 changed files with 57 additions and 0 deletions
4
Problems 21-28/problem_21.hs
Normal file
4
Problems 21-28/problem_21.hs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
insertAt :: a -> [a] -> Int -> [a]
|
||||||
|
insertAt x ls n = left ++ [x] ++ right
|
||||||
|
where
|
||||||
|
(left, right) = splitAt (n - 1) ls
|
2
Problems 21-28/problem_22.hs
Normal file
2
Problems 21-28/problem_22.hs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
range :: Int -> Int -> [Int]
|
||||||
|
range m n = if m > n then [] else m : range (m + 1) n
|
7
Problems 21-28/problem_23.hs
Normal file
7
Problems 21-28/problem_23.hs
Normal file
|
@ -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]
|
7
Problems 21-28/problem_24.hs
Normal file
7
Problems 21-28/problem_24.hs
Normal file
|
@ -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]
|
7
Problems 21-28/problem_25.hs
Normal file
7
Problems 21-28/problem_25.hs
Normal file
|
@ -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]
|
10
Problems 21-28/problem_26.hs
Normal file
10
Problems 21-28/problem_26.hs
Normal file
|
@ -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
|
11
Problems 21-28/problem_27.hs
Normal file
11
Problems 21-28/problem_27.hs
Normal file
|
@ -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]
|
9
Problems 21-28/problem_28.hs
Normal file
9
Problems 21-28/problem_28.hs
Normal file
|
@ -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
|
Loading…
Reference in a new issue