爆発させる!


33

入力として正の整数の行列を取り、爆発させます!


マトリックスを分解する方法は、外側の境界を含むすべての要素の周囲にゼロを追加するだけです。

入出力フォーマットはいつものようにオプションです!

テストケース:

1
-----
0 0 0
0 1 0
0 0 0
--------------

1 4
5 2
-----
0 0 0 0 0
0 1 0 4 0
0 0 0 0 0
0 5 0 2 0
0 0 0 0 0
--------------

1 4 7
-----
0 0 0 0 0 0 0
0 1 0 4 0 7 0
0 0 0 0 0 0 0
--------------

6
4
2
-----
0 0 0
0 6 0
0 0 0
0 4 0
0 0 0
0 2 0
0 0 0

回答:


59

Operation Flashpointスクリプト言語、182バイト

f={t=_this;c=count(t select 0);e=[0];i=0;while{i<c*2}do{e=e+[0];i=i+1};m=[e];i=0;while{i<count t}do{r=+e;j=0;while{j<c}do{r set[j*2+1,(t select i)select j];j=j+1};m=m+[r,e];i=i+1};m}

ゴルフをしていない:

f=
{
  // _this is the input matrix. Let's give it a shorter name to save bytes.
  t = _this;
  c = count (t select 0);

  // Create a row of c*2+1 zeros, where c is the number of columns in the
  // original matrix.
  e = [0];
  i = 0;
  while {i < c*2} do
  {
    e = e + [0];
    i = i + 1
  };

  m = [e]; // The exploded matrix, which starts with a row of zeros.
  i = 0;
  while {i < count t} do
  {
    // Make a copy of the row of zeros, and add to its every other column 
    // the values from the corresponding row of the original matrix.
    r = +e;
    j = 0;
    while {j < c} do
    {
      r set [j*2+1, (t select i) select j];
      j = j + 1
    };

    // Add the new row and a row of zeroes to the exploded matrix.
    m = m + [r, e];
    i = i + 1
  };

  // The last expression is returned.
  m
}

で呼び出す:

hint format["%1\n\n%2\n\n%3\n\n%4",
    [[1]] call f,
    [[1, 4], [5, 2]] call f,
    [[1, 4, 7]] call f,
    [[6],[4],[2]] call f];

出力:

挑戦の精神で:


6
未知の; おとこ; 千。
MooseBoys

2
今、私は混乱しています
グレイデアヌアレックス。

@MrGrjコマンドは文字通り何かを爆破します

1
2番目のgif「チャレンジの精神で」の+1 !:)
ケビンクルーッセン

10

ゼリー 12  11 バイト

Erik the Outgolferのおかげで-1バイト(結合にスワップ引数を使用する必要はありません)

j00,0jµ€Z$⁺

オンラインでお試しください!または、テストスイートを参照してください。

リストのリストを受け入れて返すモナドリンク。

どうやって?

j00,0jµ€Z$⁺ - Link: list of lists, m
          ⁺ - perform the link to the left twice in succession:
         $  -   last two links as a monad
      µ€    -     perform the chain to the left for €ach row in the current matrix:
j0          -       join with zeros                [a,b,...,z] -> [a,0,b,0,...,0,z]
  0,0       -       zero paired with zero = [0,0]
     j      -       join                     [a,0,b,0,...,0,z] -> [0,a,0,b,0,...,0,z,0]
        Z   -     and then transpose the resulting matrix

バイトを保存できます:j00,0jµ€Z$⁺
エリックアウトゴルファー

ああ、もちろん、ありがとう!
ジョナサンアラン


6

MATL、12バイト

FTXdX*0JQt&(

入力;は行セパレーターとしての行列です。

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

説明

FT     % Push [0 1]
Xd     % Matrix with that diagonal: gives [0 0; 0 1]
X*     % Implicit input. Kronecker product
0      % Push 0
JQt    % Push 1+j (interpreted as "end+1" index) twice
&(     % Write a 0 at (end+1, end+1), extending the matrix. Implicit display

5

Japt、18バイト

Ov"y ®î íZ c p0Ã"²

オンラインでテストしてください!-Q出力を理解しやすくするためにフラグを使用します。)

ゼリーの答えに似ていますが、ずっと長い...

説明

コードの外側の部分は、Jellyをシミュレートするための単なる回避策です。

  "             "²   Repeat this string twice.
Ov                   Evaluate it as Japt.

コード自体は次のとおりです。

y ®   î íZ c p0Ã
y mZ{Zî íZ c p0}   Ungolfed
y                  Transpose rows and columns.
  mZ{          }   Map each row Z by this function:
     Zî              Fill Z with (no argument = zeroes).
        íZ           Pair each item in the result with the corresponding item in Z.
           c         Flatten into a single array.
             p0      Append another 0.

このプロセスを2回繰り返すと、目的の出力が得られます。結果は暗黙的に出力されます。


5

、12バイト

₁₁
Tm»o:0:;0

2D整数配列を受け取って返します。オンラインでお試しください!

説明

他の多くの回答と同じ考え方:各行にゼロを追加し、2回転置します。行操作はフォールドで実装されます。

₁₁         Main function: apply first helper function twice
Tm»o:0:;0  First helper function.
 m         Map over rows:
  »         Fold over row:
   o         Composition of
      :       prepend new value and
    :0        prepend zero,
       ;0    starting from [0].
            This inserts 0s between and around elements.
T          Then transpose.

5

Mathematica、39バイト

r=Riffle[#,0,{1,-1,2}]&/@Thread@#&;r@*r

Wolframサンドボックスで試してみてください!r=Riffle[#,0,{1,-1,2}]&/@Thread@#&;r@*r@{{1,2},{3,4}}」のように呼び出します。

他の多くの回答と同様に、これは各行のゼロを転置してリフリングし、同じことを再度行うことで機能します。ジョナサン・アランのゼリーの答えに特に触発されましたが、それは偶然その答えを最初に見たからです。


4

Dyalog APL、24バイト

@ZacharyTのおかげで4バイト節約

@KritixiLithosのおかげで5バイト節約

{{⍵↑⍨-1+⍴⍵}⊃⍪/,/2 2∘↑¨⍵}

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


あなたは1,1,⍴⍵、周りに括弧を必要としませんか?
ザカリー


@KritixiLithosのいい使い方1 1+
ウリエル

APLの配列指向なので、1+動作しますか?
ザカリー

私はちょうどそのあなたの答えを見た後...実現@ZacharyT
KritixiのLithos


3

Pythonの3104の101 97 93 86バイト

  • @Zachary Tは3バイトを節約しました:未使用の変数がw削除され、1つの不要なスペースがあります
  • @Zachary Tは、さらに別の4つのバイトを保存:[a,b]としてちょうどa,bリストに追加しているときは
  • @noreは4バイトを節約しました:スライシングの使用
  • @Zachary Tと@ovsは、7バイトの節約に役立ちました:forループ内のステートメントの圧縮
def f(a):
 m=[(2*len(a[0])+1)*[0]]
 for i in a:r=m[0][:];r[1::2]=i;m+=r,m[0]
 return m

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


1
w2バイトを削除して保存できます:repl.it/JBPE
ザカリー

1
ああ、ラインの後に不要なスペースがありますm+=[r,w]
ザカリー

1
また、あなたが変更することで、4つのバイトを保存することができます[j,0]j,0して[r,m[0]r,m[0]
ザカリー

1
配列スライスを使用して、他の4バイトを保存できます。
-nore

1
python2に変換し、forループのインデントを単一のタブに変更することにより、3バイトを節約できます。
ザカリー

3

Python 3、118バイト

def a(b):
    z='00'*len(b[0])+'0'
    r=z+'\n'
    for c in b:
        e='0'
        for d in c:e+=str(d)+'0'
        r+=e+'\n'+z+'\n'
    return r

初めてのゴルフ!最高ではありませんが、自分でそう言うことができれば私は非常に誇りに思っています!

  • 小麦のコメントから-17バイト
  • 2番目のforループのインライン化から-4バイト

こんにちは、サイトへようこそ。ここにはかなりの空白があるようです。たとえば、一部の+==はスペースで囲まれていますが、スペースは削除できます。やってまた+=2回続け例えば、単一の文に単純化することができe+=str(d)+'0'
小麦ウィザード

@WheatWizard:ありがとう、ありがとう。17バイトを保存しました:)
Liren

内側のforループを1行for d in c:e+=str(d)+'0'に折りたたむことができることに気づきましたが、結合map(str、d))+ '0' , in which case it becomes pointless to define e`を使用することもできます。
小麦ウィザード

1
ああ、自分で考えただけです!うーん、.joinとmap()が最初に何であるかを知る必要があります。戻ってきます!
リレン

1
zとの定義をr同じ行に(;間に挿入して)配置して、1バイトのインデントを節約できます。
-nore

3

R、65バイト

非常に貴重なコメントをくださったJarko DubbeldamとGiuseppeに感謝します!

コード

f=function(x){a=dim(x);y=array(0,2*a+1);y[2*1:a[1],2*1:a[2]]=x;y}

関数の入力は、行列または2次元配列でなければなりません。

テスト

f(matrix(1))
f(matrix(c(1,5,4,2),2))
f(matrix(c(1,4,7),1))
f(matrix(c(6,4,2)))

出力

> f(matrix(1))
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    1    0
[3,]    0    0    0
> f(matrix(c(1,5,4,2),2))
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    1    0    4    0
[3,]    0    0    0    0    0
[4,]    0    5    0    2    0
[5,]    0    0    0    0    0
> f(matrix(c(1,4,7),1))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    0    0    0    0    0    0    0
[2,]    0    1    0    4    0    7    0
[3,]    0    0    0    0    0    0    0
> f(matrix(c(6,4,2)))
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    6    0
[3,]    0    0    0
[4,]    0    4    0
[5,]    0    0    0
[6,]    0    2    0
[7,]    0    0    0

一見私が使用して考えるa=dim(x)*2+1のではなく、nrowncol良いだろう。その後、行うことができますy=matrix(0);dim(y)=a2*1:a[1],2*1:a[2]
JAD

1
実際にy=array(0,a)はさらに短くなります。
JAD

1
私は、あなたがすなわちインデックス、周りの括弧を削除することができると信じて2*1:a[1]いるので:より高い優先順位を持つ*
ジュゼッペ・



2

Clojure、91バイト

#(for[h[(/ 2)]i(range(- h)(count %)h)](for[j(range(- h)(count(% 0))h)](get(get % i[])j 0)))

範囲を半ステップで繰り返します。



2

Python 2、64バイト

lambda l:map(g,*map(g,*l))
g=lambda*l:sum([[x,0]for x in l],[0])

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

この関数gは、ゼロの間に入力を散在させます。メイン関数は、を適用しながら入力を転置しg、それから再び転置します。たぶん、メイン関数の繰り返しを避ける方法があります。


2

JavaScript(ES6)、73 72バイト

a=>(g=a=>(r=[b],a.map(v=>r.push(v,b)),b=0,r))(a,b=a[0].map(_=>0)).map(g)

書式設定およびコメント化

水平方向と垂直方向にゼロを挿入する操作は非常に似ています。ここでの考え方は、両方のステップで同じ関数g()を使用することです。

a =>                            // a = input array
  (g = a =>                     // g = function that takes an array 'a',
    (                           //     builds a new array 'r' where
      r = [b],                  //     'b' is inserted at the beginning
      a.map(v => r.push(v, b)), //     and every two positions,
      b = 0,                    //     sets b = 0 for the next calls
      r                         //     and returns this new array
  ))(a, b = a[0].map(_ => 0))   // we first call 'g' on 'a' with b = row of zeros
  .map(g)                       // we then call 'g' on each row of the new array with b = 0

テストケース


2

、49バイト

A⪪θ;αA””βF⁺¹×²L§α⁰A⁺β⁰βA⁺β¶βFα«βA0δFιA⁺δ⁺κ⁰δ⁺䶻β

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

入力は、行をセミコロンで区切る単一の文字列です。


1
現代の炭は24バイトでこれを行うことができます:≔E⪪θ;⪫00⪫ι0θFθ⟦⭆ι0ι⟧⭆⊟θ0しかし、StringMapを回避することさえ、私はまだこれが27バイトでできると思います。
ニール

ああ、いくつかの一般的なヒント:空の文字列には事前定義された変数があり、TimesまたはMoldを使用して、指定された長さのゼロの文字列を作成できます。
ニール

2

C ++ 17 +モジュール、192バイト

cinから文字列の行として入力、coutに出力

import std.core;int main(){using namespace std;int i;auto&x=cout;string s;while(getline(cin,s)){for(int j=i=s.length()*2+1;j--;)x<<0;x<<'\n';for(auto c:s)x<<'0'<<c;x<<"0\n";}for(;i--;)x<<'0';}

2

C#、146バイト


データ

  • 入力 Int32[,] mマトリックス
  • 出力 Int32[,]分解されたマトリックス

ゴルフ

(int[,] m)=>{int X=m.GetLength(0),Y=m.GetLength(1),x,y;var n=new int[X*2+1,Y*2+1];for(x=0;x<X;x++)for(y=0;y<Y;y++)n[x*2+1,y*2+1]=m[x,y];return n;}

非ゴルフ

( int[,] m ) => {
    int
        X = m.GetLength( 0 ),
        Y = m.GetLength( 1 ),
        x, y;

    var
        n = new int[ X * 2 + 1, Y * 2 + 1 ];

    for( x = 0; x < X; x++ )
        for( y = 0; y < Y; y++ )
            n[ x * 2 + 1, y * 2 + 1 ] = m[ x, y ];

    return n;
}

読みやすい

// Takes an matrix of Int32 objects
( int[,] m ) => {
    // To lessen the byte count, store the matrix size
    int
        X = m.GetLength( 0 ),
        Y = m.GetLength( 1 ),
        x, y;

    // Create the new matrix, with the new size
    var
        n = new int[ X * 2 + 1, Y * 2 + 1 ];

    // Cycle through the matrix, and fill the spots
    for( x = 0; x < X; x++ )
        for( y = 0; y < Y; y++ )
            n[ x * 2 + 1, y * 2 + 1 ] = m[ x, y ];

    // Return the exploded matrix
    return n;
}

完全なコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestBench {
    public static class Program {
        private static Func<Int32[,], Int32[,]> f = ( int[,] m ) => {
            int
                X = m.GetLength( 0 ),
                Y = m.GetLength( 1 ),
                x, y,

                a = X * 2 + 1,
                b = Y * 2 + 1;

            var
                n = new int[ a, b ];

            for( x = 0; x < X; x++ )
                for( y = 0; y < Y; y++ )
                    n[ a, b ] = m[ x, y ];

            return n;
        };

        public static Int32[,] Run( Int32[,] matrix ) {
            Int32[,]
                result = f( matrix );

            Console.WriteLine( "Input" );
            PrintMatrix( matrix );

            Console.WriteLine( "Output" );
            PrintMatrix( result );

            Console.WriteLine("\n\n");

            return result;
        }

        public static void RunTests() {
            Run( new int[,] { { 1 } } );
            Run( new int[,] { { 1, 3, 5 } } );
            Run( new int[,] { { 1 }, { 3 }, { 5 } } );
            Run( new int[,] { { 1, 3, 5 }, { 1, 3, 5 }, { 1, 3, 5 } } );
        }

        static void Main( string[] args ) {
            RunTests();

            Console.ReadLine();
        }

        public static void PrintMatrix<TSource>( TSource[,] array ) {
            PrintMatrix( array, o => o.ToString() );
        }
        public static void PrintMatrix<TSource>( TSource[,] array, Func<TSource, String> valueFetcher ) {
            List<String>
                output = new List<String>();

            for( Int32 xIndex = 0; xIndex < array.GetLength( 0 ); xIndex++ ) {
                List<String>
                    inner = new List<String>();

                for( Int32 yIndex = 0; yIndex < array.GetLength( 1 ); yIndex++ ) {
                    inner.Add( valueFetcher( array[ xIndex, yIndex ] ) );
                }

                output.Add( $"[ {String.Join( ", ", inner )} ]" );
            }

            Console.WriteLine( $"[\n   {String.Join( ",\n   ", output )}\n]" );
        }
    }
}

リリース

  • v1.0の - 146 bytes-初期ソリューション。

ノート

  • なし

を必要としないのは、答えが2D int-array intであると述べる(int[,] m)=>だけm=>で十分ですm。また、変更,x,,x=0,て削除することができますx=0 -1バイトのforループ初期化にます。そして、あなたは削除することができますy++変更することにより、内部ループから=m[x,y];=m[x,y++];追加-1バイトのために。しかし、私から+1し、あなたの答えのポートを作成すると、現在のJavaの答えよりも短くなります。:)
ケビンクルイッセン

1

Dyalog APL、24バイト

{{⍵↑⍨¯1-⍴⍵}⊃⍪/,/2 2∘↑¨⍵}

改善があれば歓迎します!




1

JavaScript(ES6)、80 78バイト

a=>[...a,...a,a[0]].map((b,i)=>[...b,...b,0].map((_,j)=>i&j&1&&a[i>>1][j>>1]))


1

APL(ダイアログ)、22バイト

マトリックスのプロンプト、囲まれたマトリックスを返します。

{⍺⍀⍵\a}/⍴∘0 1¨1+2×⍴a←⎕

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

a←⎕ マトリックスのプロンプトとに割り当て、A

a  の次元(行、列)

 2を掛ける

1+ ひとつ追加

⍴∘0 1¨ それぞれを使用してr(周期的)eshape番号0と1を

{}/  2つの数値の間に次の無名関数を挿入することにより削減します。

⍵\a の*列拡張Aを正しい引数に従って

⍺⍀a 左引数に従ってその行を展開*

* 0はゼロの列/行を挿入し、1は元のデータ列/行を挿入します


1

ジャワ8、183の 166 162 129バイト

m->{int a=m.length,b=m[0].length,x=0,y,r[][]=new int[a*2+1][b*2+1];for(;x<a;x++)for(y=0;y<b;r[x*2+1][y*2+1]=m[x][y++]);return r;}

としての入出力int[][]

@auhmaanのC#回答のポートを作成して-33バイト。

説明:

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

m->{                                // Method with 2D integer-array as parameter and return-type
  int a=m.length,                   //  Current height
      b=m[0].length,                //  Current width
      x=0,y,                        //  Two temp integers
      r[][]=new int[a*2+1][b*2+1];  //  New 2D integer-array with correct size
  for(;x<a;x++)                     //  Loop (1) over the height
    for(y=0;y<b;                    //   Inner loop (2) over the width
      r[x*2+1][y*2+1]=m[x][y++]     //    Fill the new array with the input digits
    );                              //   End of inner loop (2)
                                    //  End of loop (1) (implicit / single-line body)
  return r;                         //  Return result 2D integer-array
}                                   // End of method


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