1

うまくいきそうな関数を書きました。(よく考えてみると、最初の 2 つのグループを接続できていませんでした...もっと考えなければなりません。)

import Data.List (groupBy)

makeGroups xs = 
  map concat $ groupBy (\a b -> head b == last a + 1) (groupBy mGroups0 xs)
    where mGroups0 = \a b -> a == length xs && elem b [1,2] 
                             || b /= length xs && b == a + 1


出力:

*Main> makeGroups [3,4,5,6,7,8,1,9,10,2]
[[3,4,5,6],[7,8],[1],[9,10,2]]

(タブについての回答をありがとうございます...今、私はただの一般的なコーディングの天才を求めています)

並べ替えられた昇順の整数シーケンスをリストにグループ化する (元の順序を維持する) より効率的で便利な方法があるでしょうか?

唯一の追加規定は次のとおりです。

the possible groups [length xs] [1] [2] [1,2] [length xs,1] [length xs,2] 
must be separated, but [length xs, 1, 2] should join any larger sequence.
4

1 に答える 1

1

このようなもの?(Daniel Fischer の提案に感謝します)

makeGroups xs = foldr comb [[last xs]] (init xs) where
  comb a b = if a == head (head b) - 1
                then (a:head b) : tail b
                else [a] : b


*Main> makeGroups [3,4,5,6,7,8,1,9,10,2]
[[3,4,5,6,7,8],[1],[9,10],[2]]


そして、ここに醜いものへの刺し傷があります:

makeGroups xs = foldr comb [[last xs]] (init xs) 
  where n = length xs
        ngroup = [1,2,n]
        comb a b = let (x:xs) = head b in 
          if a == n && isPrefixOf [1,2] (x:xs)
             then if not (null $ tail b) && head (head $ tail b) == 3
                     then ((n:head b) ++ (head $ tail b)) : drop 1 (tail b)
                     else (n:head b) : tail b
             else if a == n && isPrefixOf [2,1] (x:xs)
                     then if null (drop 1 xs)
                             then [n,2,1] : tail b
                             else [n,2,1] : drop 1 xs : tail b
             else if elem a ngroup
                     then if elem x ngroup
                             then if null xs
                                     then [a,x] : tail b
                                     else [a,x] : xs : tail b
                             else [a] : b
             else if a /= n && a == x - 1
                     then if x /= n
                             || isPrefixOf [n,1,2] (x:xs)
                             then (a:x:xs) : tail b
                             else [a] : b
                     else [a] : b
于 2013-03-14T21:34:31.667 に答える