Re: Graphical Partition
>というわけで
>この問題
>、問3をやってみた(そんなやつばっかしか)。
>
>というか nobsun の解答はわからないので自分でやってみたというか。無駄に長いなあ。
>
>perm :: [a] -> [[a]]
perm [] = []
perm l@(x:xs) = concat $ map perm_main
$ map (\n -> take (length l) $ drop n $ cycle l) [0..length l - 1]
where perm_main [x] = [[x]]
perm_main (x:xs) = map (x:) $ perm xs
をみつつ、やっぱり comb を作った方が速いような気がしていた。なんで permutation にしたんだったかなあ。 List に標準で備わっているような気がしたからだろうか。というわけでその版。 > >mkComb :: Int -> Int -> [[Int]]
mkComb l 0 = [repeat 0]
mkComb l n
| l == n = [repeat (-1)]
| otherwise = map (-1:) (mkComb (l-1) (n-1)) ++ map (0:) (mkComb (l-1) n)
isGraphical :: [Int] -> Bool
isGraphical [] = True
isGraphical (x:xs)
| x < 0 = False
| length xs < x = False
| otherwise =
or $ zipWith (\ comb ys -> isGraphical $ filter (/=0) $
zipWith (+) comb ys) (mkComb (length xs) x) (repeat xs) > >こんなところでしょうか。こっちの方がやっぱ圧倒的に速いなあ。しょぼーん。 > >解説は略。というか本質的には同じです。 >
perm [] = []
perm l@(x:xs) = concat $ map perm_main
$ map (\n -> take (length l) $ drop n $ cycle l) [0..length l - 1]
where perm_main [x] = [[x]]
perm_main (x:xs) = map (x:) $ perm xs
isGraphical :: [Int] -> Bool
isGraphical [] = True
isGraphical (x:xs)
| x < 0 = False
| length xs < x = False
| otherwise =
or $ map ( \(ys, zs) -> isGraphical $ filter (/=0)
$ map (subtract 1) ys ++ zs) $ map (splitAt x) $ perm xs
>
をみつつ、やっぱり comb を作った方が速いような気がしていた。なんで permutation にしたんだったかなあ。 List に標準で備わっているような気がしたからだろうか。というわけでその版。 > >mkComb :: Int -> Int -> [[Int]]
mkComb l 0 = [repeat 0]
mkComb l n
| l == n = [repeat (-1)]
| otherwise = map (-1:) (mkComb (l-1) (n-1)) ++ map (0:) (mkComb (l-1) n)
isGraphical :: [Int] -> Bool
isGraphical [] = True
isGraphical (x:xs)
| x < 0 = False
| length xs < x = False
| otherwise =
or $ zipWith (\ comb ys -> isGraphical $ filter (/=0) $
zipWith (+) comb ys) (mkComb (length xs) x) (repeat xs) > >こんなところでしょうか。こっちの方がやっぱ圧倒的に速いなあ。しょぼーん。 > >解説は略。というか本質的には同じです。 >
