这是Pascal的矩阵吗?


25

帕斯卡三角形中,每个数字是其正上方两个数字的和,将空点视为零:

资料来源:https://en.wikipedia.org/wiki/File:Pascal_triangle_small.png

通过旋转三角形,我们可以切出大小和旋转不同的正方形矩阵,我将其称为Pascal矩阵。请注意,这些矩阵始终需要包含前1个。这里有些例子:

1  1  1  1
1  2  3  4
1  3  6 10
1  4 10 20

6  3  1
3  2  1
1  1  1

1  5 15 35 70
1  4 10 20 35
1  3  6 10 15
1  2  3  4  5
1  1  1  1  1

1

1  1
2  1

任务

给定一个包含任何合理格式的正数的方阵,请确定其是否为Pascal矩阵

决定是根据输入是帕斯卡矩阵还是返回真值或伪值,或者确定两个常数值,然后为真输入返回一个常数,为假输入返回另一个常数。

这是,因此请尝试使用您选择的语言使用尽可能少的字节。每种语言中最短的代码将获胜,因此我不会接受答案。

测试用例

真正

[[1, 1, 1, 1], [1, 2, 3, 4], [1, 3, 6, 10], [1, 4, 10, 20]]
[[6, 3, 1], [3, 2, 1], [1, 1, 1]]
[[1, 5, 15, 35, 70], [1, 4, 10, 20, 35], [1, 3, 6, 10, 15], [1, 2, 3, 4, 5], [1, 1, 1, 1, 1]]
[[1]]
[[1, 1], [2, 1]]

[[2]]
[[1, 2], [2, 1]]
[[1, 1], [3, 1]]
[[1, 1, 1, 1], [1, 2, 3, 4], [1, 4, 6, 10], [1, 4, 10, 20]]
[[6, 3, 1], [1, 1, 1], [3, 2, 1]]
[[2, 2, 2, 2], [2, 4, 6, 8], [2, 6, 12, 20], [2, 8, 20, 40]]
[[40, 20, 8, 2], [20, 12, 6, 2], [8, 6, 4, 2], [2, 2, 2, 2]] 
[[1, 5, 15, 34, 70], [1, 4, 10, 20, 34], [1, 3, 6, 10, 15], [1, 2, 3, 4, 5], [1, 1, 1, 1, 1]]

建议的测试用例:[[40, 20, 8, 2], [20, 12, 6, 2], [8, 6, 4, 2], [2, 2, 2, 2]]。对于这个问题,我最初的回答是不正确的,但是对于所有当前的测试用例都是正确的。
凯文·克鲁伊森

@KevinCruijssen谢谢,补充。
Laikoni

Answers:


6

Brachylog28 24 23字节

这感觉很长,但是无论如何都在这里

  • 由于压缩了可选的翻转,DLOSC产生了-4个字节
  • 通过一次执行部分和再次归因于DLosc -1字节

{|↔}\↰₁{k{a₀ᶠ+ᵐ}ᵐ⊆?h=₁}

说明

{|↔}\↰₁{k{a₀ᶠ+ᵐ}ᵐ⊆?h=₁}       # Tests if this is a pascal matrix:
{|↔}\↰₁                       #     By trying to get a rows of 1's on top
{|↔}                          #       Through optionally mirroring vertically
     \                        #       Transposing
      ↰₁                      #       Through optionally mirroring vertically

       {k{a₀ᶠ+ᵐ}ᵐ⊆?h=₁}       #     and checking the following
                  ?h=₁        #        first row is a rows of 1's
        k{     }ᵐ             #        and for each row except the last
          a₀ᶠ+ᵐ               #          calculate the partial sum by
          a₀ᶠ                 #             take all prefixes of the input
             +ᵐ               #             and sum each
               ⊆?             #        => as a list is a subsequence of the rotated input

在线尝试!


4

JavaScript(ES6),114个字节

m=>[m,m,m=m.map(r=>[...r].reverse()),m].some(m=>m.reverse(p=[1]).every(r=>p=!r.some((v,x)=>v-~~p[x]-~~r[x-1])&&r))

在线尝试!


4

MATL,17个字节

4:"Gas2YLG@X!X=va

在线尝试!验证所有测试用例

1Pascal矩阵的输出,0否则。

说明

4:      % Push [1 2 3 4]
"       % For each
  G     %   Push input: N×N
  a     %   1×N vector containing 1 for matrix columns that have at least a nonzero
        %   entry, and 0 otherwise. So it gives a vector containing 1 in all entries
  s     %   Sum. Gives N
  2YL   %   Pascal matrix of that size
  G     %   Push input
  @     %   Push current iteration index
  X!    %   Rotate the matrix that many times in steps of 90 degress
  X=    %   Are they equal?
  v     %   Concatenate with previous accumulated result
  a     %   Gives 1 if at least one entry of the vector is nonzero
        % End (implicit). Display (implicit)

2

R,104字节

function(m,R=row(m)-1,y=nrow(m):1,Z=choose(R+t(R),R))any(sapply(list(Z,Z[,y],Z[y,y],Z[y,]),identical,m))

在线尝试!

讨厌...

创建Z尺寸等于的标准Pascal矩阵m,然后测试输入矩阵m是否identicalany的旋转Z


2

木炭,41字节

F‹¹⌈§θ⁰≔⮌θθF‹¹⌈Eθ§ι⁰≦⮌θ⌊⭆θ⭆ι⁼λ∨¬κΣ…§θ⊖κ⊕μ

在线尝试!链接是详细版本的代码。说明:

F‹¹⌈§θ⁰

如果第一行的最大值大于1,

≔⮌θθ

然后翻转输入数组。

F‹¹⌈Eθ§ι⁰

如果第一列的最大值大于1,

≦⮌θ

然后镜像输入数组。

⌊⭆θ⭆ι

循环输入数组的元素并打印最小结果(即所有结果的逻辑与),

⁼λ∨¬κΣ…§θ⊖κ⊕μ

如果每个值都在第一行上,则将其与1进行比较;否则,将上一行的总和累加到并包括上一个单元格。


1

Python 2,129字节

f=lambda M,i=4:i and(set(M[0])=={1}and all(a+b==c for x,y in zip(M,M[1:])for a,b,c in zip(x[1:],y,y[1:]))or f(zip(*M[::-1]),i-1))

在线尝试!

返回True是否M为Pascal矩阵,否则为0


1

05AB1E,29 个字节

¬P≠iR}DøнP≠ií}¬PΘsü)ε`sηOQ}P*

在线尝试验证所有测试用例

说明:

¬Pi }        # If the product of the first row of the (implicit) input-matrix is NOT 1:
    R         #  Reverse the order of the rows
D             # Duplicate the resulting matrix
 øнPi }      # If the product of the first column is NOT 1:
      í       #  Reverse each row individually
¬PΘ           # Check if the product of the first row is exactly 1
           *  # AND
          P   # And check if everything after the following map is truthy:
sü)ε     }    #  Map over each pair of rows:
    `sη       #   Get the prefixes of the first row
       O      #   Sum each prefix
        Q     #   And check if it's equal to the second row
              # (and output the result implicitly)

1

科特林,269字节

{m:List<List<Int>>->val n=m.size
var r=0
var c=0
fun f()=if(m[0][0]!=1)m[n-r-1][n-c-1]
else if(m[n-1][0]!=1)m[r][n-c-1]
else if(m[0][n-1]!=1)m[n-r-1][c]
else m[r][c]
var g=0<1
for(l in 0..n*2-2){r=l
c=0
var v=1
do{if(r<n&&c<n)g=f()==v&&g
v=v*(l-c)/++c}while(--r>=0)}
g}

在线尝试!



1

Java(JDK),234个字节

m->{int l=m.length,L=l-1,p=1,s=0,S=0,e=l,E=l,d=1,D=1,i,j;if(m[0][0]>1|m[0][L]>1){s=L;e=d=-1;}if(m[0][0]>1|m[L][0]>1){S=L;E=D=-1;}for(i=s;i!=e;i+=d)for(j=S;j!=E;j+=D)p=(i==s|j==S?m[i][j]<2:m[i][j]==m[i-d][j]+m[i][j-D])?p:0;return p>0;}

在线尝试!

学分


1
不错的答案,但是很麻烦。;)哦,和-1:i==s||j==Si==s|j==S
凯文·克鲁伊森

@KevinCruijssen如果您知道更好的算法,我接受!但是旋转是所有变量的原因。有些语言可以用1-2个字节处理它们,在Java中,您必须考虑它们周围的代码。核心算法实际上很短:m->{int l=m.length,i=0,j;for(;i<l;i++)for(j=0;j<l;j++)p=(i<1|j<1?m[i][j]<2:m[i][j]==m[i-1][j]+m[i][j-1])?p:0;return p>0;}(122个字节)
OlivierGrégoire

0

果冻,22字节

Ż€Iṫ2⁼ṖaFḢ=1Ʋ
,Ṛ;U$Ç€Ẹ

在线尝试!

说明

Helper链接,检查矩阵的此旋转是否有效

Ż€            | prepend each row with zero
  I           | find differences within rows
   ṫ2         | drop the first row
     ⁼Ṗ       | compare to the original matrix
              |   with the last row removed
       a      | logical and
        FḢ=1Ʋ | top left cell is 1

主链接

,Ṛ            | copy the matrix and reverse the rows
  ;U$         | append a copy of both of these
              |   with the columns reversed
     ǀ       | run each version of the matrix
              |   through the helper link
       Ẹ      | check if any are valid
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.