new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
33
Task/Best-shuffle/Haskell/best-shuffle-1.hs
Normal file
33
Task/Best-shuffle/Haskell/best-shuffle-1.hs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import Data.Function (on)
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
import Data.Array
|
||||
import Text.Printf
|
||||
|
||||
main = mapM_ f examples
|
||||
where examples = ["abracadabra", "seesaw", "elk", "grrrrrr", "up", "a"]
|
||||
f s = printf "%s, %s, (%d)\n" s s' $ score s s'
|
||||
where s' = bestShuffle s
|
||||
|
||||
score :: Eq a => [a] -> [a] -> Int
|
||||
score old new = length $ filter id $ zipWith (==) old new
|
||||
|
||||
bestShuffle :: (Ord a, Eq a) => [a] -> [a]
|
||||
bestShuffle s = elems $ array bs $ f positions letters
|
||||
where positions =
|
||||
concat $ sortBy (compare `on` length) $
|
||||
map (map fst) $ groupBy ((==) `on` snd) $
|
||||
sortBy (compare `on` snd) $ zip [0..] s
|
||||
letters = map (orig !) positions
|
||||
|
||||
f [] [] = []
|
||||
f (p : ps) ls = (p, ls !! i) : f ps (removeAt i ls)
|
||||
where i = fromMaybe 0 $ findIndex (/= o) ls
|
||||
o = orig ! p
|
||||
|
||||
orig = listArray bs s
|
||||
bs = (0, length s - 1)
|
||||
|
||||
removeAt :: Int -> [a] -> [a]
|
||||
removeAt 0 (x : xs) = xs
|
||||
removeAt i (x : xs) = x : removeAt (i - 1) xs
|
||||
2
Task/Best-shuffle/Haskell/best-shuffle-2.hs
Normal file
2
Task/Best-shuffle/Haskell/best-shuffle-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
bestShuffle :: Eq a => [a] -> [a]
|
||||
bestShuffle s = minimumBy (compare `on` score s) $ permutations s
|
||||
Loading…
Add table
Add a link
Reference in a new issue