2次元リストとしてのパスカルの三角形


11

ネストされたリストで、未使用のスポットにゼロが含まれるPascalの三角形を作成します。

出力配列では、Pascalの三角形の数はゼロで区切られ、両側にゼロが埋め込まれて中央に配置されます。たとえば、一番下の行(最後のサブ配列)の左右にゼロがない必要があります。最後から2番目のサブアレイには、両側に1つのゼロパディングがあります。

入力の出力は5次のとおりです。

[[0,0,0,0,1,0,0,0,0],
[0,0,0,1,0,1,0,0,0],
[0,0,1,0,2,0,1,0,0],
[0,1,0,3,0,3,0,1,0],
[1,0,4,0,6,0,4,0,1]]

いつものように、バイト数が最も少ないソリューションが優先されます。


5
重複これ。残念ながら、出力形式を変更するだけではチャレンジは変わりません。それでも問題が解決しない場合は、Stack Overflowに投稿してみてください。
GamrCorps

2
まあ、余分なゼロがあります。
CalculatorFeline

このプログラムは、必要なものを出力します(Python 3):print("def pascal(n):\n #make the nested list\n a=[[0 for i in range(2*n+1)] for j in range(n+1)] #make the list\n a[0][n]=1 #add the initial 1\n for i in range(1,n+1):\n for j in range(2*n+1):\n a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(2*n+1)] #the main part\n return a")
CalculatorFeline

1
@CatsAreFluffy余分なゼロは、前回の反復でスペースを置き換えるだけです-これは機能的にはまったく同じ問題です。
-ricdesi

2
自分の言語にネイティブの配列表現構文を使用できますか、それとも形式を交渉できませんか?

回答:


3

Mathematica、70 68バイト

NestList[ListConvolve[{1,0,1},#,2]&,Join[#,{1},#],#2]&[0~Table~#,#]&

MATLソリューションに似ています。


3

Mathematica、48バイト

CellularAutomaton[{#+#3&@@#&,{},1},{{1},0},#-1]&

CellularAutomation 素晴らしい。


2

ゼリー、12バイト

NR¬ṙ-,1S$³Ð¡

ここで試してみてください。

説明

                   This is a list of functions, each operating on the input, n:
NR                 Get the range [-n -n+1 ... 0 ... n-1 n].
  ¬                Logical NOT the entire range: [0 0 ... 1 ... 0 0].
         ³Ð¡       Repeat n times, and cumulate the results:
   ṙ-,1                Rotate by both -1 and 1
       S               Sum the results.
        $              (Joins the above two functions)

1

Haskell、66バイト

q n|d<-0<$[2..n]=scanl(\(s:t)_->zipWith(+)(0:s:t)$t++[0])(d++1:d)d

使用例:q 4-> [[0,0,0,1,0,0,0],[0,0,1,0,1,0,0],[0,1,0,2,0,1,0],[1,0,3,0,3,0,1]]

使い方:

d <- 0<$[2..n]                      -- bind d to a list of (length n)-1 zeros
scanl                               -- build a list
                         (d++1:d)   -- starting with  [d ++ 1 ++ d]
      \(s:t)_                    d  -- by combining the previous element with the
                                    -- elements of d, but ignoring them, i.e.
                                    -- build a list of (length d) by repeatedly
                                    -- modifying the start element by
          zipWith(+)                -- adding element-wise
                    (0:s:t)         -- the previous element prepended by 0  
                           t++[0]   -- and the tail of the previous element
                                    -- followed by a 0 

1

Python 3、172 158 133バイト

def p(n):
 x=2*n+1;y=range
 a=[[0]*x]*n;a[0][n]=1
 for i in y(1,n+1):
  for j in y(x):a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(x)]
 return a

良くなり続ける


1
これはまだ完全にゴルフされていませんよね?

ええ、ええ。これは(やや少ないゴルフ形式で)私が質問にコメントを残したプログラムによって印刷されます。
CalculatorFeline

1

MATL24 22 21バイト

tEq:=Gq:"t5BX+8L)]N$v

編集(2016年5月20日):バージョン18.0.0の言語では、上記のコードを実行するにはいくつかの変更が必要です。以下のリンクにはこれらの変更が含まれています

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

これは、ループを使用して新しい行をスタックにプッシュします。新しい行は前の行から計算され、畳み込みを適用し[1,0,1]、目的のサイズのみを保持します。ループの後、すべての行が2D配列に連結されて表示されます。MATLでは、2D配列は列に整列した数値テーブルとして表示されます。

t           % implicit input n. Duplicate
Eq          % 2*n-1
:           % range [1,2,...,2*n-1]
=           % gives [0,0,...1,...0,0]. This is the first row
Gq:         % range [1,2,...,n-1]
"           % for each. Repeat n-1 times
  t         %   duplicate latest row. This duplicate will become the next row
  5B        %   push array [1,0,1] (5 converted to binary)
  X+        %   convolution
  8L        %   predefined literal [2,-1i]. Used for indexing
  )         %   apply that index: remove one element at each end
]           % end for each
N$v         % concatenate all rows into a 2D array. Implicitly display

0

Javascript、152 146バイト

f=i=>[...Array(i)].map((x,j)=>(z=[...Array(i*2-1)].map((_,k)=>+!!~[i-j,i+j].indexOf(k+1)),y=j?z.map((_,k)=>_||(k&&(k+1 in y)?y[k-1]+y[k+1]:_)):z))


0

真剣に、33バイト

╩╜r`╣;lD0nkdZΣ`M╜rRZ`i0nkd@;)kΣ`M

オンラインで試す

私はこれらのバイトの少なくとも7程度を削ることができると比較的確信しているので、これ以上ゴルフをするまで説明を投稿するのを待つつもりです。


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