Last active
August 29, 2015 14:26
-
-
Save nobsun/1b4c7373b4804d28391d to your computer and use it in GitHub Desktop.
順列の索引付け ref: http://qiita.com/nobsun/items/aba3bda2bd8a2f9b5f88
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
permNth :: [a] -> Int -> [a] |
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
permNth xs = (perms xs !!) |
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
permNth :: [a] -> Integer -> [a] |
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
permNth xs idx = snd $ mapAccumL f (xs,idx) $ reverse $ take n facts |
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
f (ys,jdx) fact = case divMod jdx fact of | |
(d,m) -> case splitAt (fromIntegral d) ys of | |
(zs,w:ws) -> ((zs++ws,fromIntegral m),w) |
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
permNth :: [a] -> Integer -> [a] | |
permNth xs idx = snd $ mapAccumL f (xs,idx) $ reverse $ take n facts | |
where | |
n = length xs | |
f (ys,jdx) fact = case divMod jdx fact of | |
(d,m) -> case splitAt (fromIntegral d) ys of | |
(zs,w:ws) -> ((zs++ws,fromIntegral m),w) | |
facts :: [Integer] | |
facts = scanl (*) 1 [1..] |
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
permIndex :: Eq a => [a] -> [a] -> Integer |
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
permIndex [] [] = 0 | |
permIndex xs@(_:_) ys@(_:_) = sum $ zipWith (*) zs $ snd $ mapAccumL f xs ys | |
where | |
zs = reverse $ take (length xs) facts | |
f ps q = case elemIndex q ps of | |
Just i -> (delete q ps,fromIntegral i) |
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
import Data.List (mapAccumL,elemIndex,delete) | |
permNth :: [a] -> Integer -> [a] | |
permNth xs idx = snd $ mapAccumL f (xs,idx) $ reverse $ take n facts | |
where | |
n = length xs | |
f (ys,jdx) fact = case divMod jdx fact of | |
(d,m) -> case splitAt (fromIntegral d) ys of | |
(zs,w:ws) -> ((zs++ws,fromIntegral m),w) | |
permIndex :: Eq a => [a] -> [a] -> Integer | |
permIndex [] [] = 0 | |
permIndex xs@(_:_) ys@(_:_) = sum $ zipWith (*) zs $ snd $ mapAccumL f xs ys | |
where | |
zs = reverse $ take (length xs) facts | |
f ps q = case elemIndex q ps of | |
Just i -> (delete q ps,fromIntegral i) | |
facts :: [Integer] | |
facts = scanl (*) 1 [1..] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment