デジタルトライアングルでまとめる


28

まあ、本当にそれを要約します。

10進整数の空でないリスト(0-9)を取り込んで、最初の行の後のすべての数字が2桁の合計である入力リストを上部に持つ数字の下向きの「三角形」を出力するプログラムまたは関数を作成します。 10を法としてその上。

たとえば、入力に[7, 5, 0, 9]は出力があります

7 5 0 9
 2 5 9
  7 4
   1

ためには、2ある(7 + 5) mod 105ある(5 + 0) mod 109ある(0 + 9) mod 10など、すべての方法1であること(7 + 4) mod 10

リストに項目が1つしかない場合、出力は入力と一致します。例えばの入力[4]意志収率

4

追加の例を次に示します。

[0]

0

[1, 2]

1 2
 3

[8, 7]

8 7
 5

[0, 0]

0 0
 0

[1, 4, 2]

1 4 2
 5 6
  1

[0, 1, 0]

0 1 0
 1 1
  2

[1, 0, 0, 0]

1 0 0 0
 1 0 0
  1 0
   1

[1, 2, 3, 4]

1 2 3 4
 3 5 7
  8 2
   0

[1, 2, 3, 5, 8]

1 2 3 5 8
 3 5 8 3
  8 3 1
   1 4
    5

[9, 2, 4, 5, 3, 2, 2]

9 2 4 5 3 2 2
 1 6 9 8 5 4
  7 5 7 3 9
   2 2 0 2
    4 2 2
     6 4
      0

出力では次のことに注意してください。

  • 最初の行には先行スペースがありません。
  • 後続の各行には、前の行よりも先行スペースが1つ多くあります。
  • 数字は単一のスペースで区切られます。
  • 各行には、最大1つの末尾スペースを含めることができます。
  • 単一のオプションの末尾の改行がある場合があります。
  • 通常の10進数(0〜9)の文字を使用する必要があります。

バイト単位の最短コードが優先されます。Tiebreakerは以前の回答です。


1
最初にタイトルを「デジタルトラウマ」と読みました

回答:


24

BrainF ** k、396 391バイト

>+>>++++[-<++++++++>]->,----------[++++++++++.>>++++++++[-<++++<------>>]<.,----------]-<+[-<+]->>+[-<<<<<++++++++++.[-]>[-<+>>.<]<[->+<]>+>>>[[->+]->>+<<<+[-<+]->]>+[-<->[[->+]->+>>+<<<<+[-<+]->]<+>->+[->+]->>[->+<]>+>++++++++++>>-<<[-<-[>>]<]<->>>+[-<<<+>>>[-<->]<+++++++++>>>+]++++++++[-<++++<++++++>>]<<<[-<<<<+[-<+]-<+>>+[->+]->>>>+<]>.>.[-]<[-]<<<[->+<]<<+[-<+]>+]>>[-]<<<-<+[-<+]->>+]

これをやろうという誘惑に抵抗できませんでした。少なくとも三角形は尖った側が下です。

入力は、数字の文字列の後に単一の改行が続くようになります。

出力には、すべての行に単一の末尾スペースが含まれます。

例:

$ bf sd.bf
010
0 1 0 
 1 1 
  2 

$ bf sd.bf
123456
1 2 3 4 5 6 
 3 5 7 9 1 
  8 2 6 0 
   0 8 6 
    8 4 
     2 

$ bf sd.bf
9245322
9 2 4 5 3 2 2 
 1 6 9 8 5 4 
  7 5 7 3 9 
   2 2 0 2 
    4 2 2 
     6 4 
      0 

説明

機能的な観点からコードを説明するのはかなり難しいため、代わりに、さまざまな時点でテープの状態の観点からコードを見ることができます。ここでの核となる考え方は、出力する三角形が、ループの反復ごとにサイズが1ずつ縮小する(BFの場合は)密集した配列として初期化されるということです。別の重要な考えは255、テープで検索できる「プレースホルダー」を示すために使用することです。

初期化

これが最も簡単な手順です。プログラムの開始時に、次を実行します。

>+>>++++[-<++++++++>]->

これにより、テープは次の状態に強制されます(>N<テープ上のポインターの位置を示します)

[ 0 1 32 255 >0< 0 0 ...]

ここの最初の数字は「バッファ」の場所です。長期的に使用するつもりはありませんが、少しの操作を簡単にし、データをコピーするのに役立ちます。
2番目の数字は、各行の最初に出力するスペースの数で、最初の行のに始まります。最初の行には先行スペースはありません。
3番目の数字は、出力するスペース文字です。
4番目の数値はプレースホルダー255であるため、比較的簡単にこの位置に戻ることができます。

入力

この位置から、すべての文字を読み取ります。このステップの終わりに、次の状況になることを願っています。

[ 0 1 32 255 a b c d e f ... >255< 0 0 ... ]

where a b c d e f ...は、入力された数字の文字列を示します(改行ではありません)。

次の方法でこれを実現します。

,----------[++++++++++.>>++++++++[-<++++<------>>]<.,----------]-

これにはいくつかのニュアンスがあります。まず、取得した各文字を出力し、その後にスペースを出力します。次に、ASCII値をテープにコピーするのではなく、実際の数字をコピーします。第三に、改行を入力したときに停止し、その時点で適切な場所に置いておきます。
入力がであるとし6723ます。次に、最初のを読むと、6テープは次のようになります。

[ 0 1 32 255 >54< 0 0 ...]

でこの値が10(ASCII改行)と等しくないことを確認します,----------[++++++++++。次に、値を出力し、入力値から48を減算し、その隣の値に32を同時に加算して続行します(>>++++++++[-<++++<------>>]<)。ここに残ります。

[ 0 1 32 255 6 >32< 0 ...]

このプロセス全体で、入力の右側のすべての数字が0であると仮定できることに注意してください。これは、右側の値を使用して6 * 8and を計算する場合、以前の状態を台無しにする危険がないことを意味します4 * 8
次に、生成したスペース文字を出力し、新しい入力を取得して、そこで計算したスペースを削除します。最終的に、入力は255改行で終了し、ループは終了し、改行があった場所を残します(,----------]-)。これは、テープをナビゲートするために使用する2番目のプレースホルダー文字です。シナリオのこの時点で、テープは次のとおりです。

[ 0 1 32 255 6 7 2 3 >255< 0 0 ... ]

計算

これが機能する方法は、255プレースホルダー間の数字のリストがループの反復ごとに1つずつ縮小することです。1桁しか残っていない場合は、終了してすぐに停止する必要があります(この時点で、そのリストのすべての数字が既に出力されているため、再度出力することを心配する必要はありません)。

次のトリックを使用して、最初の255プレースホルダーに移動します<+[-<+]-。これにより、左側のテープを効果的に検索し、255間に何も変更しません。ポインターを移動したので、終了条件を確認できます。リストに1桁しかない場合、右側の2スペースのセルにはが保持され255ます。したがって、それをチェックしてループを開始します。>>+[-<<

ループの最初のステップは、改行を出力することです。最初のセル(バッファセル)に移動し、10を追加して出力します。次のステップは、すべての先行スペース文字を出力することです。それらを出力した後、先頭のスペースの数のカウントを増やします。これらの手順は、次の手順で実行されます。

-<<<<<++++++++++.[-]>[-<+>>.<]<[->+<]>+>>>

この状態のままになります:

[ 0 2 32 255 >6< 7 2 3 255 0 0 0 0 0 0 ]

次のステップでは、リストの最初の値をコピーし、2番目のプレースホルダーを過ぎ255ます。

[[->+]->>+<<<+[-<+]->]

基本的には、プレースホルダー間を行き来することでこれを行い255、ここに残ります:

[ 0 2 32 255 >0< 7 2 3 255 0 6 0 0 ... ]

ここで、ループを開始し、リストの残りを繰り返し、ヒットすると停止し255ます:>+[-<

この時点では、左端の数字は常に0です。したがって、それらが大好きな255ので、そこにプレースホルダーをポップして、リスト内の自分の場所に戻ることができます。次のステップは、リストの2番目の場所を、2番目のplaceholderを過ぎて、最初の場所を移動した場所を囲む場所に移動すること255です。これらの手順は、次の手順で実行されます。

->
[[->+]->+>>+<<<<+[-<+]->]

ここから離れます:[ 0 2 32 255 255 >0< 2 3 255 7 6 7 0 ] 今、67は両方とも、計算が行われる場所に移動されました。7リストの次の番号にも必要になるため、2つのコピーが必要です。が7直後に255この目的を果たしますが、他方7は計算によって消費されます。

最初に、2桁を追加します。

<+>->+[->+]->>
[->+<]>

ここを離れる:

[ 0 2 32 255 0 255 2 3 255 7 0 >13< 0 ]

次のステップの組み合わせは最も複雑です。指している数値が10より大きいかどうかを確認する必要があります。10より大きい場合は、を減算し10ます。実際には、10を減算0し、減算のいずれかの時点でヒットするかどうかを確認します。存在する場合10、後で追加し直します。この最後に、10を法とする合計が必要です。

Prepare a 10 to the right
+>++++++++++
Leave yet another 255 for a loop condition later
>>-<<
If the number is greater than 10 end up one space to the left
else one space to the right
[-<-[>>]<]<->
Check if the previous 255 is two spaces to the right and if it is
add 10 back to our sum--we've subtracted too much
>>+[-<<<+>>>[-<->]<+++++++++>>>+]

この時点で、目標を達成しました。10を法とする合計があります!また、数が10より大きいかどうかに関係なく、ここで終了します。

[ 0 2 32 255 0 255 2 3 255 7 0 3 0 0 >0< ]

次の目標は、この新しい合計を出力し、それにスペースを空けて、リストに戻すことです。これはすべて、これまでの255ホッピングと48合計への追加手法を使用して行われているため、詳しくは説明しません。

++++++++[-<++++<++++++>>]
<<<[-<<<<+[-<+]-<+>>+[->+]->>>>+<]
>.>.

そして私たちはここにいます:[ 0 2 32 255 3 255 2 3 255 7 0 0 51 >32< ] 追加の2553リスト内の場所を失わないように、新しく注入された後にプレースホルダーしてください。この時点で、合計とそのスペースを出力しているので、クリーンアップして、このループの次の反復が機能する状態に戻す必要があります。私たちは、クリアする必要がある5132、細胞を移動し7、我々は、最初からやり直すことができるように右に一度、そして私たちのリストのプレースホルダに移動します。

[-]<[-]<<<[->+<]<<+[-<+]

今、私たちはここにいます:[ 0 2 32 255 3 >0< 2 3 255 0 7 0 ... ]
次の繰り返しのためにまさにそこにいたいところです。255をチェックして先に進みましょう!(>+]

ループから脱落すると、前のリストの合計で構成されるまったく新しいリストが作成されます。最初は、次のようになります。

 [ 0 2 32 255 3 9 5 0 >0< ]

次に、新しいリストでそのプロセス全体を繰り返したいので255、左に下に進んで最初からやり直します!を少しクリーンアップして>>[-]<<から、プレースホルダーを削除する必要があります<-。その後、入力後とまったく同じ場所にいるので、同じチェックを行うことで逃げることができます:<+[-<+]->>+、およびboom!完全なループができました!必要なのは右角かっこだけで、終了時にはすべてを出力しているので、完了です]


帰りましょう:) 2015年以降回答がありません:o
カルビンの趣味

1
@HelkaHomba知ってるよ!私はまだある程度の頻度で訪問していますが、このコードを書くことに抵抗することはできませんでした。言語に最適です:)
BrainSteel

9
「パーフェクトフォーBF」は私を排除する概念です:-)
ルイスメンドー

7

ゼリー20 19 18 バイト

Ṛ+2\Ṗп%⁵z”@ṚZGḟ”@

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

バックグラウンド

Jellyでは、数値の生成は簡単です。出力はもう少し複雑です。

Jellyには組み込みのグリッドアトム(G)があり、行間の改行と列間のスペースを含む2Dリストを表示します。(各行を逆にして生成された)数値の2D配列を取得し、fill valueで転置します@。結果の配列を崇拝し、再び転置した後、適用Gすると以下が得られます。

9 2 4 5 3 2 2
@ 1 6 9 8 5 4
@ @ 7 5 7 3 9
@ @ @ 2 2 0 2
@ @ @ @ 4 2 2
@ @ @ @ @ 6 4
@ @ @ @ @ @ 0

目的の三角形を取得するには、塗りつぶし値を削除するだけです。

使い方

Ṛ+2\Ṗп%⁵z”@ṚZGḟ”@  Main link. Argument: A (list of integers)

Ṛ                   Reverse A.
    Ṗп             While there's more than one element:
 +2\                  Compute the pairwise sum.
                    Collect all intermediate results in a list.
       %⁵           Take all resulting integers modulo 10.
         z”@        Transpose with fill value '@'.
            Ṛ       Reverse the order of the rows.
             Z      Transpose.
              G     Grid.
               ḟ”@  Filter; remove the fill value.

5

Pyth-18バイト

j.e+*dkjdbP.ueM+Vt

テストスイート

j                                       Join by newlines
 .e                                     Loop over seq in var b and index in var k
  +                                     Concatenate
   *dk                                  Space repeated k times
    jdb                                 Join b by spaces
  P                                     [:-1] to get rid of empty line
   .u             (Q implicit)          Cumulative fixed point over input (var G)
    eM                                  Mod 10 mapped over list
     +V                                 Vectorized addition
      t(G implict)                      G[1:]
      (G implict)                       Vectorize cuts this off to G[:-1]

5

Python 3.5、74 72 71バイト

f=lambda L,*S:f([sum(x)%10for x in zip(L,L[1:print(*S,*L)]or 1)],'',*S)

入力は整数のリスト(例f([1,2,3,5,8]):)、出力はSTDOUTになります。%10実際map返すmapのPython 3でオブジェクトを私たちが行うことができないという意味、少し迷惑ですmap(lambda*x:sum(x)%10,L,L[1:])か類似しました。

関数はエラーになりますが、それまでに出力は完了しています。を貼り付けるのに適した場所を見つけて、-1バイトの@xsotに感謝しprintます。


3
3.5はインストールされていませんが、これは機能するはずですf=lambda L,*S:f([sum(x)%10for x in zip(L,L[1:print(*S,*L)]or 1)],'',*S)
。– xsot

1
@xsotそれは...の驚くべき使用ですNone
Sp3000

どのようにprint何かを返すのですか?print関数が戻ることを知りません。
エリックアウトゴルファー

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀああ、待って、Pythonのprint関数が戻ることを意味する-ええNone、完了時に戻る
-Sp3000

Noneつまり、スライシングにはどれほど役立つのでしょうか?
エリックアウトゴルファー

5

05AB1E20 19 17バイト

コード:

DvNð×?T%Ððý,¦‚ø€O

説明:

D                     # Duplicate the input.
 v                    # Map over it, running the following len(input) times.
  Nð×                 # Multiply the iteration number with a space.
     ?                # Pop and print without a newline.
      T%              # Take all integers modulo 10.
        Ð             # Triplicate the array.
         ðý,          # Join by spaces and print with a newline.
            ¦         # Remove the first element of the array.
             ‚        # Wrap it up, (pop a, b push [a, b]).
              ø       # Zip.
               €O     # Sum up each element.

CP-1252エンコードを使用します。オンラインでお試しください!


4

MATL、32 30 29 28 27 26 25 24バイト

t"X@qZ"y1X9&VhDTTH&Y+10\

@Luisのおかげで1バイト節約

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

すべてのテストケースの修正バージョン

説明

        % Implicitly grab input
t       % Duplicate the input
"       % For each element of the input (really just a shortcut to loop numel(input) times)
  X@q   % Determine the current loop index and subtract 1
  Z"    % Create an array of blanks this size (these are the padding to the left)
  y     % Copy the current array of numbers from the stack
  1X9   % Get the pre-defined output format of %.15g from the clipboard
  &V    % Convert the input to it's character representation with single spacing
  h     % Horizontally concatenate the left padding and the numbers
  D     % Display the result (consumes the string)
  TT    % Create a two-element vector of 1's ([1 1])
  H&Y+  % Convolve [1 1] with the array (computes the pair-wise sum)
  10\   % Mod 10 the result
        % Implicitly close for loop

いいね!主要なスペースを確保する方法を探していました。私は忘れてしまったVフォーマットの仕様を可能にします。Z"代わりに1バイトを保存できますOこのリンクを参照してください(コメントの形式に問題があります)
ルイスメンドー

@LuisMendoヒントをありがとう!うんD、デフォルトでその単一のスペース間の数字を使用するフォーマット仕様を入手しました。
-Suever

2

実際、43バイト

;l;D+#"{:^%d}"%╗W;' j1╟╜f.;pXZ`i+9u@%`MdXWX

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

このプログラムは、出力後に単一の末尾の改行を出力します。

説明:

;l;D+#"{:^%d}"%╗W;' j1╟╜f.;pXZ`i+9u@%`MdXWX
;l;D+                                        calculate the width of the field (2L-1 where L = len(input))
     #"{:^%d}"%╗                             push to a list, make a Python new-style format string for centering text, save in reg0
                W;' j1╟╜f.;pXZ`i+9u@%`MdXW   while the top of the stack is truthy:
                 ;' j1╟                        duplicate list, join with spaces, push string to a list
                       ╜f.                     center string and print  
                          ;pXZ                 zip list with itself shifted forward by 1 (zip(a,a[1:]))
                              `i+9u@%`M        map: add each pair and mod by 10
                                       dX      discard the last value (which was not added with anything)
                                          X  discard the now-empty list after the loop to avoid printing it

2

Mathematica、67バイト

MatrixForm@NestList[Mod[Total/@Partition[#,2,1],10]&,#,Length@#-1]&

例:ここに画像の説明を入力してください


2

CJam、25バイト

q~{_2ew::+Af%}h;]eeSff*N*

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

説明

これは、三角形のレイアウトを生成するためにかなり巧妙なトリックを使用します。

q~      e# Read and evaluate input.
{       e# While the list of digits is non-empty...
  _2ew  e#   Duplicate and get all sublists of length 2.
  ::+   e#   Sum each pair.
  Af%   e#   Take each result modulo 10.
}h
;]      e# Discard the final empty list and wrap the others in an array.
ee      e# Enumerate the array. E.g. for input `[7 5 0 9]` we now get this:
        e# [[0 [7 5 0 9]]
        e#  [1 [2 5 9]]
        e#  [2 [7 4]]
        e#  [3 [1]]]
Sff*    e# Perform S* on each element at depth two. For the integers from the
        e# enumeration this gives a string of that many spaces, i.e. the correct
        e# indentation. For the lists of digits this inserts a space character
        e# between every two digits, thereby spacing out the digits as necessary.
N*      e# Put linefeeds between the pairs of indentation and digit list.

1

JavaScript(ES6)147バイト

a=>{o=0;e=(c=>console.log(' '.repeat(o++)+c.join` `));l=1;while(l){p=0,d=[],e(a),a.map(b=>{p?d.push((v+b)%10):0;p=1,v=b});d.length<2?l=0:a=d};e(d)}

うーん、私はゴルフに、このダウンアイデアのカップルを持っている
バリント


1

Pyke、21バイト

lVDm}R$],FsT%)od*pKDP

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

この方法は少し違うと思います。

[1, 2, 3, 5, 8] - take the input
[2, 4, 6, 10, 16] - double it
[1, 2, 3, 5, 8] - take the input again
[1, 1, 2, 3] - get the deltas
[[2,1], [4,1], [6,2], [10,3]] - transpose them together
[3, 5, 8, 13] - apply addition
[3, 5, 8, 3] - mod 10

1

Perl 6の 65の63 62  61バイト

{put ' 'x my$++,$_ for @_,{.rotor(2=>-1).map(*.sum%10).list}...1}
{put ' 'x my$++,$_ for @_,{@(.rotor(2=>-1).map(*.sum%10))}...1}
{put ' 'x my$++,$_ for @_,{@(map *.sum%10,.rotor(2=>-1))}...1}
{put ' 'x my$++,$_ for @_,{[map *.sum%10,.rotor(2=>-1)]}...1}

説明:

{ # anonymous block that takes the list as input (@_)

  put # print with trailing newline

    ' ' x ( my $ )++, # pad with one more space than previous iteration
    $_                   # the list to print for this iteration
    # The 「.Str」 method on a List puts spaces between elements
    # which is what 「put」 calls on anything that isn't a Str

  for # do that for every list produced from the following

    @_, # the input list

    { # anonymous block that takes the previous list as input ($_)

      [ # turn the following from a Seq into an Array

        map
          *.sum % 10, # sum the values and modulus 10 of the following:

          # take the previous list 2 at a time, backing up one
          $_.rotor( 2 => -1 )

      ]

    }

    ... # repeatedly call that block until:

    1   # the result is only one element long
}

例:

my &digital-triangle-sum = {put ' 'x my$++,$_ for @_,{[map *.sum%10,.rotor(2=>-1)]}...1}

for [7,5,0,9], [0,1,0], [1,0,0,0], [9,2,4,5,3,2,2] -> \List {
   digital-triangle-sum List

   put '';
}
7 5 0 9
 2 5 9
  7 4
   1

0 1 0
 1 1
  2

1 0 0 0
 1 0 0
  1 0
   1

9 2 4 5 3 2 2
 1 6 9 8 5 4
  7 5 7 3 9
   2 2 0 2
    4 2 2
     6 4
      0

5
これは有効ではありません。関数の送信は、頻繁に勝手に再利用可能でなければなりません。
メゴ


1

TSQL、198 194 191バイト

WHILEの代わりにGOTOを使用することで、3人のキャラクターをゴルフできました

ゴルフ

DECLARE @ varchar(100)= '1 2 3 4 5 6 7'

DECLARE @j INT=1,@i INT=LEN(@)a:
PRINT @
WHILE @i>@j
SELECT
@=STUFF(@,@i-1,2,RIGHT(SUBSTRING(@,@i-2,1)+SUBSTRING(@,@i,1)*1,1)+' '),@i-=2SELECT
@=STUFF(@,@j,1,' '),@j+=1,@i=LEN(@)IF @i>0GOTO a

オンラインで試す(2 * WHILEで古いスクリプトを使用)


1

Java 7、230 215 213バイト

int c=0;void m(int[]a){int l=a.length,j=-1,i=-1;if(l<1)return;int[]x=new int[l-1];while(++j<c)p(" ");for(;++i<l;p(a[i]+" "))if(i<l&i>0)x[i-1]=(a[i-1]+a[i])%10;p("\n");c++;m(x);}<T>void p(T s){System.out.print(s);}

これは結局私が思っていたよりも少し長くなってしまいました。たぶん私は少し混乱したので、もう少しゴルフができるかもしれません。

@GiacomoGarabelloのおかげでいくつかのバイトが節約されました。

未ゴルフ&テストコード:

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

class Main{
  static int c = 0;

  static void m(int[] a){
    int l = a.length,
        j = -1,
        i = -1;
    if(l < 1){
      return;
    }
    int[] x = new int[l-1];
    while(++j < c){
      p(" ");
    }
    for(; ++i < l; p(a[i] + " ")){
      if(i < l & i > 0){
        x[i - 1] = (a[i - 1] + a[i]) % 10;
      }
    }
    p("\n");
    c++;
    m(x);
  }

  static <T> void p(T s){
    System.out.print(s);
  }

  static void printAndReset(int[] a){
    m(a);
    c = 0;
    System.out.println();
  }

  public static void main(String[] a){
    printAndReset(new int[]{ 7, 5, 0, 9 });
    printAndReset(new int[]{ 0 });
    printAndReset(new int[]{ 1, 2 });
    printAndReset(new int[]{ 8, 7 });
    printAndReset(new int[]{ 0, 0 });
    printAndReset(new int[]{ 1, 4, 2 });
    printAndReset(new int[]{ 0, 1, 0 });
    printAndReset(new int[]{ 1, 0, 0, 0 });
    printAndReset(new int[]{ 1, 2, 3, 4 });
    printAndReset(new int[]{ 1, 2, 3, 5, 8 });
    printAndReset(new int[]{ 9, 2, 4, 5, 3, 2, 2 });
  }
}

出力:

7 5 0 9 
 2 5 9 
  7 4 
   1 

0 

1 2 
 3 

8 7 
 5 

0 0 
 0 

1 4 2 
 5 6 
  1 

0 1 0 
 1 1 
  2 

1 0 0 0 
 1 0 0 
  1 0 
   1 

1 2 3 4 
 3 5 7 
  8 2 
   0 

1 2 3 5 8 
 3 5 8 3 
  8 3 1 
   1 4 
    5 

9 2 4 5 3 2 2 
 1 6 9 8 5 4 
  7 5 7 3 9 
   2 2 0 2 
    4 2 2 
     6 4 
      0 

関数void p(String s){System.out.print(s);}を作成し、標準印刷を置き換えます。以下のためのprintln使用p("\n")。移動int iint j近くint c=0;int c=0,i,j;)および移動print(a[i]+" ")の内側にforあなたが-11の合計ブラケットを取り外すことができるように条件
ジャコモGarabello

@GiacomoGarabello今日、この短い印刷版を学びました:の<T>void p(T s){System.out.print(s);}代わりにvoid p(String s){System.out.print(s);}
ケビンCruijssen 16

わあ... Pyth and Jellyに2バイト近い!ありがとう!
ジャコモガラベロ16

@GiacomoGarabello「Pyth and Jellyに2バイト近い!」へへ。'常に人生の明るい面を見る。' ;)
ケビンクルーイッセン

1

C#6、125 + 31 125 + 18 = 143バイト

string f(int[] n,string s="")=>s+string.Join(" ",n)+"\n"+(n.Length>1?f(n.Zip(n.Skip(1),(a,b)=>(a+b)%10).ToArray(),s+" "):"");

+18は using System.Linq;

不要なusingステートメントを指摘して、13バイトを節約してくれた@TheLethalCoderに感謝します。


0

JavaScript(ES6)、77バイト

a=a.map((_,i)=>(b=a,a=[a.map((e,j)=>j>i?(e+a[j-1])%10:''),b.join` `)).join`
`

0

C、138バイト

ゴルフ

c,d;main(int a,char**b){b++;while(c++,d=0,--a)while(d<a)printf("%*c%c",!d?c:1,*b[d],(d+2>a)*10),++d<a?*b[d-1]=(*b[d-1]+*b[d]-96)%10+48:0;}

非ゴルフ

c,d;
main(int a,char**b){
b++;
while(c++,d=0,--a)
    while(d<a)
        printf("%*c%c",
          !d?c:1,      //number of blanks in front of digit
          *b[d],       //digit
          (d+2>a)*10), //returns 10(new line) only for d+1 == a
        ++d<a
          ? *b[d-1]=(*b[d-1]+*b[d]-96)%10+48 //update digit 
          :  0;
}

0

C#、167バイト

私は実際にこのソリューションをかなり誇りに思っています、ラムダ式はあなたがそれらのコツをつかめばとても楽しい

void f(List<int> a){int x=a.Count;for(int s=0;s<x;s++){Console.WriteLine(new String(' ',s)+string.Join(" ",a));a=a.Take(x-s-1).Select((v,i)=>(a[i+1]+v)%10).ToList();}}

さらなる改善のためにここにありません:

void f(List<int> a)
{
int x = a.Count;
for (int s = 0; s<x ;s++)
{
    Console.WriteLine(new String(' ',s)+string.Join(" ",a));
    a=a.Take(x-s-1).Select((v,i)=>(a[i+1]+v)%10).ToList();
}
}

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


リストではなく配列を入力に使用することで、2バイトを節約できます。List<int> a-> int[] aint x=a.Count-> int x=a.Length.ToList()->ToArray()
ソク

0

Haskell、139バイト

f=mapM(\(r,s)->putStrLn$r++(s>>=(++" ").show)).zip(iterate(' ':)"").takeWhile(/=[]).iterate g where g(_:[])=[];g(y:p:ys)=mod(y+p)10:g(p:ys)

入力を引数として受け取り、STDOUTに出力します。

ゴルフされていないバージョン:

f = mapM (\(r, s) -> putStrLn $ r ++ (s >>= (++ " ") . show))
    . zip (iterate (' ' :) "")
    . takeWhile (/= [])
    . iterate sumTerms
    where sumTerms (_:[]) = []
          sumTerms (y:p:ys) = mod (y+p) 10 : sumTerms (p:ys)

0

Python 3、97バイト

def f(x):print(*x);x[-1]==''or f(['']+[(x[i]+x[i+1])%10if''!=x[i]else''for i in range(len(x)-1)])

単一の末尾改行を印刷します。

使い方

def f(x):                            function with input of list of numbers
print(*x)                            print the old line of the triangle
x[-1]==''or...                       if no more numbers, stop...
(x[i]+x[i+1])%10if''!=x[i]else''...  ...else compute the next entry in the new line if
                                     possible...
...for i in range(len(x)-1)          ...for all relevant digit pairs...
['']+...                             ...concatenate with empty string to force new leading
                                     space...
f(...)                               ...and pass to function

Ideoneでお試しください



0

Javascript(外部ライブラリを使用)(198バイト)

n=>{a=_.From(n);b=a.Write(" ");c=1;while(++c){a=a.BatchAccumulate(2).Where(y=>y.Count()==2).Select(z=>z.Sum()%10);if(a.Count()==0){break;}b+="\r\n"+_.Range(0,c).Write(" ",x=>"")+a.Write(" ")}return b}

libへのリンク:https : //github.com/mvegh1/Enumerable/

コードの説明:これはライブラリを使用して簡単でした!バイト単位では勝ちませんが、コードは冗長で読みやすいものではありません。したがって、入力「n」は整数の配列です。変数「a」に格納されたライブラリにロードします。「b」は戻り文字列で、「」を区切り文字として結合文字列を格納します。cは現在の反復では、これを使用して挿入するスペースの数を決定します注:これは、入力が0から9の場合にのみうまく機能するようです。現在の列挙可能な「a」のセット、つまり[1,2,3,4,5,6]がある場合、[1,2]、[2,3]、[3,4]、... [ 6] ...次に、サイズ2のバッチのみを持つようにフィルタリングします。次に、それをバッチの合計のコレクション%10にマッピングします。aが空の場合、完了です。それ以外の場合は、改行に新しい行を追加します。最後に戻る...

数分後に来る写真。

ここに画像の説明を入力してください

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