C # , 146 Bytes
Daten
- Eingabe
Int32[,]
m
Die zu zerlegende Matrix
- Ausgabe
Int32[,]
Die aufgelöste Matrix
Golf gespielt
(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;}
Ungolfed
( 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;
}
Ungolfed lesbar
// 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;
}
Vollständiger Code
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]" );
}
}
}
Releases
- v1.0 -
146 bytes
- Anfangslösung.
Anmerkungen