行列列の進行


17

無限行列を考えます:

0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1
0  0  2  3  0  0  2  3  0  0  2  3  0  0  2  3
0  0  0  4  5  6  0  0  0  4  5  6  0  0  0  4 ...
0  0  0  0  7  8  9 10  0  0  0  0  7  8  9 10
0  0  0  0  0 11 12 13 14 15  0  0  0  0  0 11
              ...

行列の各新しい行は、zゼロから開始して構築されzます。ここで、その行で使用している正の桁の長さです。正の数字は1、行を反復するたびに、最初に数字を増やして追加することで構成されます。そのパターンは右に無限に繰り返されます。したがって、たとえば、最初の行が開始0, 1, 0, 1...し、2番目の行が開始し0,0, 2,3, 0,0, 2,3...ます。パターンに従って、3行目が始まり0,0,0, 4,5,6, 0,0,0, 4,5,6...ます。

二つの入力として整数、所与nx、出力最初(一番上)xの数n上記行列の列目。(列に0インデックスまたは1インデックスを選択できます。提出するものを指定するだけです。)

たとえば、入力n = 0(インデックスが0)の場合、列は完全に0sであるため、出力はx 0sになります。

入力n = 15およびのx = 6場合、出力はになります[1, 3, 4, 10, 11, 0]

入力n = 29およびのx = 15場合、出力はになります[1, 0, 6, 8, 15, 0, 0, 34, 39, 0, 0, 0, 0, 0, 120]

入力n = 99およびのx = 25場合、出力はになります[1, 3, 4, 0, 15, 0, 0, 0, 37, 55, 56, 0, 87, 93, 0, 0, 151, 163, 176, 0, 0, 0, 0, 0, 325]

I / Oとルール

  • 入力と出力は、任意の便利な方法で指定できます。
  • 入力と出力は、言語のネイティブの数値型に適合すると想定できます。
  • 完全なプログラムまたは機能のいずれかが受け入れられます。関数の場合、出力する代わりに出力を返すことができます。
  • 標準的な抜け穴は禁止されています。
  • これはので、通常のゴルフルールがすべて適用され、最短のコード(バイト単位)が勝ちます。

回答:


4

JavaScript(ES6)、45バイト

カリー化構文の入力を受け取ります(n)(x)

n=>g=x=>x?[...g(x-1),n/x&1&&n%x+x*~-x/2+1]:[]

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

どうやって?

直接式を使用して、列n(0から始まる)と行x(1から始まる)のセルの値を取得します。

n / x & 1 &&     // is this cell zero or non-zero?
n % x +          // column modulo row --> increment for a non-zero value at this position
x * ~-x / 2 + 1  // minimum value of non-zero values for this row:
                 // ∑(i=1...x-1)(i) + 1 = x(x - 1) / 2 + 1

3

R80 76バイト

バグを指摘してくれた@JayCeに感謝します!

function(n,x)for(a in 1:x)print(rep(c(rep(0,a),((y=sum(1:a))-a+1):y),,n)[n])

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

に1ベースのインデックスを使用しnます。ゴルファーアルゴリズムが存在する可能性が非常に高いですが、単純repな解決策を可能にします。


n=1sapplyの結果がマトリックスではなくなるため、エラーが発生します。この修正はコストがかかりますゴルファーがいるのだろうか?
JayCe

ああ、そう、あなたは正しい。まあ、幸いなことにあります!
ジュゼッペ

forループ、ええ!そして、あなたはプロセスで4バイトをゴルフしました:)
JayCe

@JayCeええ、私の最初の考えは、バイトを節約repするn内側ので一番外側のインデックスを作成することでしたが、関数を定義する必要がないのでループがより短いsapplyことを思い出しました。forsapply
ジュゼッペ



2

MATL25 18バイト

x:"@:t~ys:b@-)h1G)

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

6バイトのゴルフをしてくれたLuis Mendoに感謝します!

これは本質的に私のR回答のMATLポートです。

x		 % implicit input, read n and delete
:		 % implicit input, read x and push [1..x]
"		 % for loop with i = 1..x
 @:		 % push [1..i]
   t		 % duplicate
    ~		 % logical negate, turn to array of zeros
		 % stack: [[1..i], [0 .. (i times)]]
     y		 % duplicate from below
		 % stack: [[1..i], [0 .. (i times)], [1..i]]
      s:	 % sum and range
		 % stack: [[1..i], [0 .. (i times)], [1..(i * (i + 1)/2)]]
	b	 % bubble up
		 % stack: [[0 .. (i times)], [1..(i * (i + 1)/2)], [1..i]]
	 @-	 % push i and subtract. This will be used as a modular index to get the last i elements
		 % stack: [[0 .. (i times)], [1..(i * (i + 1)/2)], [1-i..0]]
	   )	 % index into array modularly to get the last i elements
		 % stack: [[0 .. (i times)], [(i-1)*i/2 + 1, .. (i * (i + 1)/2)]]
	    h	 % horizontally concatenate the array
	     1G) % push n and index modularly, leaving the result on the stack
		 % implicit end of for loop. The stack now contains the appropriate elements in order
		 % implicit end. Print stack contents


1

、14バイト

↑!Tzo¢+MRN0CNN

引数n(最初)は1から始まります。オンライン試してください!

または↑!TṠzo¢+†K0CNN、同じバイト数で使用することもできます。

説明

↑!Tz(¢+)MRN0CNN -- example inputs n=4, x=3
            C N -- cut the natural numbers: [1,2,3,4,…]
             N  -- | using the natural numbers
                -- : [[1],[2,3],[4,5,6],[7,8,9,10],…]
        M N     -- map over the naturals
         R 0    -- | replicate 0 that many times
                -- : [[0],[0,0],[0,0,0],[0,0,0,0],…]
   z(  )        -- zip these two lists
      +         -- | concatenate
     ¢          -- | cycle
                -- : [[0,1,0,1,…],[0,0,2,3,0,0,2,3,…],…]
  T             -- transpose: [[0,0,0,0,…],[1,0,0,0,…],[0,1,0,0,…],[1,3,4,0,…],…]
 !              -- index into that list using n: [1,3,4,0,…]
↑               -- take x: [1,3,4]

1

ハスク21 19バイト

↑mȯ!⁰¢§+`R0§…ȯ→Σ←ΣN

引数をn(1-indexed)として、次にを取りますx
BMOのおかげで2バイト節約されましたが、BMOの答えほど短くはありませんでした。
ハスクを使用する私の最初の試み。
オンラインでお試しください!





1

ゼリー、11バイト

ṖS+R¬;$)⁹ịⱮ

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

-1 ジョナサンアランに感謝します。

引数1:x
引数2:n + 1


0ṁ;Ɗ-> ¬;$バイトを保存します。
ジョナサンアラン

@JonathanAllanそして、それだけではまだ十分ではないと思います。少なくとも私は屈辱を取り除きました’R...(つまり、少なくとも私にとって)奇妙なことは、最後の数日間、Thue-Morseシーケンス(ゼリーでは、が含まれています;¬$)を考えていたことです。
エリックアウトゴルファー

1

05AB1E、25バイト

LO©LsL£¹£¹LÅ0s)øε˜®Ì∍}ø¹è

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


05AB1Eは、歯磨き粉やオレンジジュースのようなマトリックスを使用しますが、実装がどれほど悪いかを考慮した悪いバイト数ではありません。私のコードでさえ私を笑っていますLO©L


0

、19バイト

IE…·¹N§⁺Eι⁰EιL⊞Oυωη

オンラインでお試しください!リンクは、コードの詳細バージョンです。説明:

    ¹               Literal 1
     N              First input (`x`) as a number
  …·                Inclusive range
 E                  Map (value `i`, 0-indexed counter `k`)
         ι          Current value
        E           Map over implicit range
          ⁰         Literal 0 (i.e. create array of `i` zeros)
            ι       Current value
           E        Map over implicit range
                 ω  Arbitrary variable
                υ   Predefined array
              ⊞O    Push
             L      Length
       ⁺            Concatenate arrays
      §           η Index by second input (`n`)
I                   Cast to string and implicitly print on separate lines

スニペットEιL⊞Oυωは、iループを通過するたびにダミー値を配列にプッシュし、結果の配列の長さを取得することにより、次の整数を生成します。


0

Java 8、65 63 60バイト

n->x->{for(;x>0;)System.out.println(n/x%2*(n%x+x*--x/2+1));}

n0でインデックス付けされ、x1でインデックス付けされ、改行で区切られて反転された数値を出力します。

@ArnauldのJavaScript(ES6)回答のポート。

オンラインでお試しください。
正しい順序できれいに印刷された結果は8 6バイト長くなります。オンラインで試してください。


0

Haskell、67バイト

i#l=cycle((++)=<<(0<$)$[i..l-1]):l#(l+l-i+1)
n!x=take x$(!!n)<$>1#2

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

i#l                  -- function # builds the infinite matrix
                     -- input: i and l are lowest and highest+1 non-zero number of
                     -- the current line
   = cycle           -- for the current line, repeat infinitely
           [i..l-1]  --   the non-zero numbers of the line appended 
     (++)=<<(0<$)    --   to a list of 0s with the same length
   :                 -- append next row of the matrix
     l#(l+l-i+1)     --   which is a recursive call with i and l adjusted

n!x =                -- main function
              1#2    -- create matrix
      (!!n)<$>       -- pick nth element of each row
  take x             -- take the first x numbers thereof
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.