2D分割累積合計


16

チャレンジ

行列の所与のMr個の行とCの列、及び2つのブールリストV長さのR及びH長さのC、分配累積垂直方向と水平方向の和を計算します。

ルール

  • rおよびcは1以上

  • HVは真の値で始まります

  • Mの値は、言語の合理的な数値領域内にあります。

  • パーティション化と加算は左上隅から始まります。

歩く

与えられたM

┌──────────────┐
│ 1  2  3  4  5│
│ 6  7  8  9 10│
│11 12 13 14 15│
│16 17 18 19 20│
└──────────────┘

H1 0 1 0 0

V1 1 0 1

Mを列のグループに分割し、Hの真の値ごとに新しいグループを開始します

┌─────┬────────┐
│ 1  2│ 3  4  5│
│ 6  7│ 8  9 10│
│11 12│13 14 15│
│16 17│18 19 20│
└─────┴────────┘

列の各グループを行のグループに分割し、Vのすべての真の値で新しいグループを開始します。

┌─────┬────────┐
│ 1  2│ 3  4  5│
├─────┼────────┤
│ 6  7│ 8  9 10│
│11 12│13 14 15│
├─────┼────────┤
│16 17│18 19 20│
└─────┴────────┘

各セルを水平方向に累積的に合計します。

┌─────┬────────┐
│ 1  3│ 3  7 12│
├─────┼────────┤
│ 6 13│ 8 17 27│
│11 23│13 27 42│
├─────┼────────┤
│16 33│18 37 57│
└─────┴────────┘

各セルを垂直方向に累積的に合計します。

┌─────┬────────┐
│ 1  3│ 3  7 12│
├─────┼────────┤
│ 6 13│ 8 17 27│
│17 36│21 44 69│
├─────┼────────┤
│16 33│18 37 57│
└─────┴────────┘

結果:

┌──────────────┐
│ 1  3  3  7 12│
│ 6 13  8 17 27│
│17 36 21 44 69│
│16 33 18 37 57│
└──────────────┘

追加のテストケース

M

┌───────────┐
│15 11 11 17│
│13 20 18  8│
└───────────┘

H1 0 0 1V1 0

結果:

┌───────────┐
│15 26 37 17│
│28 59 88 25│
└───────────┘

M

┌─┐
│7│
└─┘

結果(HVはでなければなりません1):

┌─┐
│7│
└─┘

M

┌──┐
│ 3│
│-1│
│ 4│
└──┘

V1 1 0Hでなければなりません1

結果:

┌──┐
│ 3│
│-1│
│ 3│
└──┘

M

┌───────────────────────────────────────────────────────┐
│10    7.7 1.9 1.5 5.4  1.2 7.8 0.6 4.3 1.2  4.5 5.4 0.3│
│ 2.3  3.8 4.1 4.5 1    7.7 3   3.4 6.9 5.8  9.5 1.3 7.5│
│ 9.1  3.7 7.2 9.8 3.9 10   7.6 9.6 7.3 6.2  3.3 9.2 9.4│
│ 4.3  4.9 7.6 2   1.4  5.8 8.1 2.4 1.1 2.3  7.3 3.6 6  │
│ 9.3 10   5.8 9.6 5.7  8.1 2.1 3.9 4   1.3  6.3 3.1 9  │
│ 6.6  1.4 0.5 6.5 4.6  2.1 7.5 4.3 9   7.2  2.8 3.6 4.6│
│ 1.7  9.9 2.4 4.5 1.3  2.6 6.4 7.8 6.2 3.2 10   5.2 8.9│
│ 9.9  5.3 4.5 6.3 1.4  3.1 2.3 7.9 7.8 7.9  9.6 4   5.8│
└───────────────────────────────────────────────────────┘

H1 0 0 1 0 1 1 1 0 1 1 1 0

V1 0 0 0 0 1 0 0

結果:

┌────────────────────────────────────────────────────────────────┐
│10   17.7 19.6  1.5  6.9  1.2  7.8  0.6  4.9  1.2  4.5  5.4  5.7│
│12.3 23.8 29.8  6   12.4  8.9 10.8  4   15.2  7   14    6.7 14.5│
│21.4 36.6 49.8 15.8 26.1 18.9 18.4 13.6 32.1 13.2 17.3 15.9 33.1│
│25.7 45.8 66.6 17.8 29.5 24.7 26.5 16   35.6 15.5 24.6 19.5 42.7│
│35   65.1 91.7 27.4 44.8 32.8 28.6 19.9 43.5 16.8 30.9 22.6 54.8│
│ 6.6  8    8.5  6.5 11.1  2.1  7.5  4.3 13.3  7.2  2.8  3.6  8.2│
│ 8.3 19.6 22.5 11   16.9  4.7 13.9 12.1 27.3 10.4 12.8  8.8 22.3│
│18.2 34.8 42.2 17.3 24.6  7.8 16.2 20   43   18.3 22.4 12.8 32.1│
└────────────────────────────────────────────────────────────────┘

回答:


9

ゼリー、10バイト

Zœṗ@+\€Ẏð/

オンラインでお試しください!そして最後のテストケース (でG読みやすくするための最後)。

入力はリストとして取得されます[M, H, V]

説明

Zœṗ@+\€Ẏð/  Input: [M, H, V]
        ð/  Insert the previous (f) as a dyadic link
            Forms f( f(M, H) , V)
            For f(x, y):
Z             Transpose x
 œṗ@          Partition the rows of x^T at each true in y
    +\€       Compute the cumulative sums in each partition
       Ẏ      Tighten (Joins all the lists at the next depth)

実際のコードを改ざんする必要がないように、このようなフッターを使用できます。
エリックアウトゴルファー

7

APL(Dyalog)、13バイト

引数としてVHMの istを取ります。

{⍉⊃,/+\¨⍺⊂⍵}/

オンラインでお試しください!

{}/ 次の無名関数を挿入(縮小)します。左側の用語はbyで表され、右側の用語はbyで表されます。APL関数は右結合であるため、これはV fH f M)です。

⍺⊂⍵ partitionによるパーティション⍵

+\¨ 各部分の累積合計

,/ 連結による削減(ランクを削減するために結果を囲みます)

 開示する、明らかにする

 転置


6

パイソン2 + numpyの、143の 138 117 115 110 108バイト

Adámのおかげで-21バイト!

lambda M,*L:reduce(lambda m,l:vstack(map(lambda p:cumsum(p,0),split(m,*where(l)))).T,L,M)
from numpy import*

オンラインでお試しください!


1
パーティション、分割、および累積を一度求め、転置、繰り返します。
アダム

@Adámありがとう、何らかの理由でそれを考えていませんでした。
-notjagan

とにかく2つの関数のリストルックアップが好きでした:)
ジョナサンアラン

2
してくださいヘッダー「のPython 3 + numpyの」
漏れ修道女

5

ゼリー 15  14 バイト

œṗ+\€Ẏ
ḢçЀZð⁺

H,V左右を取りM、結果のマトリックスを返すダイアディックリンク。

オンラインでお試しください!

または、14の場合も単一行として: Ḣœṗ+\€Ẏ$¥Ð€Zð⁺

どうやって?

œṗ+\€Ẏ - Link 1: partition and cumSum: list of partition bools, list of values
œṗ     - partition (the values) at truthy indexes (of the bools)
    €  - for €ach part:
  +\   -   cumulative reduce by addition
     Ẏ - tighten (flattens back into a list)

ḢçЀZð⁺ - Main link: list of lists, [H,V]; list of lists, M
      ⁺ - perform this twice:
     ð  - [it's a dyadic chain for the second pass, first pass is dyadic implicitly]
Ḣ       -   head, pop it & modify (so H the first time, V the second)
  Ѐ    -   map across right: (M the first time, the intermediate result the second)
 ç      -     the last link (1) as a dyad
    Z   -   transpose the result (do the rows first time, and the columns the second)

前:

œṗ@+\€Ẏ
ç€Zç€⁵Z

結果の表現を印刷する完全なプログラム。


おっと前のゼリーの回答から-50%!
アダム

なに?ワオ。私は本当にあなたがこれをどのようにしたかを研究する必要があります...私と比較して信じられないほどです!
ハイパーニュートリノ

ああ、これは2つの方向を別々にやっているよね?スマート。
ハイパーニュートリノ

私はそれをやっていると思う大体 ...同じ事を
ジョナサン・アラン

良い方法。私はAPLでこれを打ち負かすことができます。14バイトあります。
アダム

4

MATL、19バイト

,!ix"0GYs@12XQ!g]v!

入力はM(行列)、H(列ベクトル)、V(列ベクトル)です。行区切り記号は;です。

オンラインでお試しください!またはすべてのテストケースを検証:12345

説明

これは、累積合計を水平方向に、次に垂直方向に行います。

,          % Do the following twice
  !        %   First time this inputs M implicitly. Transpose. Second time
           %   it transposes the result of the horizontal cumulative sum
  ix       %   Input H (first time) or V (second time). Delete it; but gets
           %   copied into clipboard G
  "        %   For each column of the matrix
    0G     %     Push most recent input: H (first time) or V (second)
    Ys     %     Cumulative sum. This produces a vector of integer values
           %     such that all columns (first time) or rows (second) of M 
           %     with the same value in this vector should be cumulatively
           %     summed
    @      %     Push current column of M transposed (first time) or M after
           %     horizontal cumulative sum (second time)
    12XQ   %     Cumulative sum. Gives a cell array of row vectors
    !g     %     Join those vectors into one row vector
  ]        %   End
  v        %   Concatenate the row vectors vertically into a matrix
  !        %   Transpose. This corrects for the fact that each column vector
           %   of the matrix was cumulatively summed into a row vector
           % Implicit end. Implicit display

1
最も印象的。Matlabはこのようなもののために作られたと思います。
アダム

@AdámAPLの長さはそれほど変わらないはずです:
ルイスメンドー

テストケースの生成に使用した私のリファレンス実装は26バイトです。
アダム

@AdámDarn!APLがゼリーを破った?これは受け入れがたい!(ゴルフのソリューションが必要です...笑)xD
HyperNeutrino

@HyperNeutrinoさて、JellyにはAPLとJのようなランクと深さの両方はありません。
アダム

3

J、20バイト

;@(<@(+/\);.1|:)&.>/

オンラインでお試しください!

入力は、を含むボックスの配列として取得され[V, H, M]ます。

説明

;@(<@(+/\);.1|:)&.>/  Input: [V H M]
  (     g      )   /  Insert g and reduce (right-to-left)
                      Forms V g H g M = V g (H g M)
                & >     Unbox each
             |:         Transpose the right arg
          ;.1           Partition
      +/\               Reduce each prefix using addition (cumulative sum)
   <@                   Box each partition
;@                      Raze (Concatenate the contents in each box)
                &.>     Box the result

2

Mathematica、212バイト

(T=Transpose;A=AppendTo;J=Flatten;f[s_]:=Block[{},t=2;r=1;w={};While[t<=Length@s,If[s[[t]]==0,r++,w~A~r;r=1];t++];w~A~r];K[x_,y_]:=Accumulate/@#&/@(FoldPairList[TakeDrop,#,f@y]&/@x);d=J/@K[#,#2];T[J/@K[T@d,#3]])&


入力
[M、H、V]

[{{15、11、11、17}、{13、20、18、8}}、{1、0、0、1}、{1、0}]


2

C#(.NET Core)、164バイト

(M,H,V)=>{int a=M.Length,b=M[0].Length,i,j;for(i=0;i<a;i++)for(j=0;j<b;j++)if(!H[j])M[i][j]+=M[i][j-1];for(i=0;i<a;i++)for(j=0;j<b;j++)if(!V[i])M[i][j]+=M[i-1][j];}

オンラインでお試しください!

基本的に、OPで指定されているとおりに実行されます。最初に反復して水平方向に合計し、次に再度反復して垂直方向に合計します。


2

Haskell129バイト 119バイト

s m v=tail$scanl(\a(x,s)->if s then x else zipWith(+)a x)[](zip m v)
t=foldr(zipWith(:))$repeat[]
f m h v=t$s(t$s m v)h

オンラインでお試しください!

@ceasedtoturncounterclockwisのおかげで10バイト節約

t(転置用)行と列を切り替えます。簡単な説明:

foldr(zipWith(:))(repeat[])(r1,...,rn) =
zipWith(:) r1 (zipWith(:) r2 (... zipWith(:) rn (repeat [])))

右から左に読む:行を下から上にブラウズし、各値を目的の列にプッシュします。

s 基本的にはベクトルのローリングサムですが、True値が発生するとリセットされます v

fs次の行を合計し、次の列でv同じことを行いますh


t=foldr(zipWith(:))(repeat[])。短くなるだけでなく、効率も大幅に低下します。
反時計回りに

@ceasedtoturncounterclockwisヒントをありがとう。
jferard

1

JavaScript(ES6)、88バイト

(a,h,v)=>a.map(b=>b.map((e,i)=>t=h[i]?e:t+e)).map((b,j)=>t=v[j]?b:t.map((e,i)=>e+b[i]))

0

ゼリー、31バイト

+\€€
œṗḊZ€⁵œṗ$€Ḋ€Ç€ÇZ€€Z€;/€€;/

オンラインでお試しください!

これはJelly xDには長すぎる

ところで、このプログラムの11/31バイトはユーロ文字で構成されています。プログラムの3分の1以上です!


ユーロが多すぎます。
アダム

私はそれが可能だろうと思って、私は第3レベルのマッピングxDさんに2つ目のレベルをやっているので、二重に分割行列をもつP作業は、楽しいようではありません。まさにアダム私の思考@
HyperNeutrino

€ -なぜ、あなたはこの€のようにあなたのお金を浪費している
V.クルトワ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.